Sharing Gmail script to trigger Hubitat maker-api from received emails

Here is a link to the more complex version of the script I wrote. This one works to keep a maker-api connected switch up to date with the current on/off or locked/unlocked status based on notification emails from a third party. I am using it to track the current state of 5 door locks connected to an alarm.com system, in order to display their state on a hubitat dashboard. Since the script can only be run once a minute, there is a risk of the device getting out of sync if there are multiple state change notifications since the last check (i.e. a lock is locked, unlocked, and locked again within a few seconds). To account for that, this script calls notification emails for both states, and then runs three different checks:

  • If there are only new notifications for one condition (i.e. locked), then only that api is called. First one exclusive condition is checked, then the other.
  • If there are notifications for both events present (i.e. lock and unlock), the script calls for the time of receipt of the most recent message for each event, then calls the appropriate api based on the most recently received email.

This script is set to delete the examined emails after running, but can easily be configured to remove the labels instead.

The script template includes a section for 2 different locks (front door and back deck in this example). You can easily scale it up or down for any number of locks, or other on/off devices by simply copying and pasting the routine multiple times, and updating the trigger label name and corresponding webhook api for each device. Or delete the second routine if you only have one lock to manage.

I've only had it running for a day, but so far it works very smoothly. Display status almost always updates in less than a minute. It is most dependent on the reliability of the alarm.com notifications being successfully sent, and in the proper order in the event of someone rapidly flipping a lock. :slight_smile: I'm completely new to google script writing, so had to figure out what I was doing from scratch and a few similar online scripts I found. For that reason I've tried to make the instructions as simple as possible for someone else to try. If you can set up maker-api, this is pretty much just copying and pasting to get going. I hope it's helpful for someone else.

https://script.google.com/d/1R5xG8bPGeVEAn02nXnW79VbiY-zPoI57rp6XJk2ub9tK1F_cu5_e2q3F/edit?usp=sharing

2 Likes