Skip to content

Turn off the thermostat if the doors are open for more than 10 minutes (using homebridge, SSH, JSON REST API, AppleScript)

Turn off the thermostat if the doors are open for more than 10 minutes (using homebridge, SSH, JSON REST API, AppleScript)

I’m fairly new to HomeKit, but I have a fairly large amount of hardware for home automation (ecobee thermostat, Insteon door, motion and water sensors, Insteon light switches, Logitech Harmony hubs, etc.). Migrate everything from Indigo in recent weeks, including upgrading to an eero wifi network (great!) And using [Homebridge](https://homebridge.io) to bring the Insteon hardware (with [Homebridge Platform InsteonLocal](https://github.com/kuestess/homebridge-platform-insteonlocal#readme) plugin). Bringing the hardware and controlling it with basic steps was pretty straightforward. However, a major obstacle was my desire for Homekit / Homebridge to turn off the oven / air conditioning if a door was open for more than 10 minutes. And to put the thermostat back on when the last door was closed. This seemed like an easy task, but it seems that for many reasons it is not that simple. After experimenting (mixed with some frustration) I got to work. First of all, many contributors owe a lot of credit – we took many of your ideas and put them together for a specific solution. Two that particularly helped me on the path to a solution were: 1. How to integrate [HomeKit and JSON REST API](https://www.homekit.blog.com/r/HomeKit/comments/l1z82e/beef_up_your_homekit_automations_with_your_own/) 2. [How to work with SSH-Automations](https://www.homekit.blog.com/r/HomeKit/comments/f35ez9/got_a_homepod_then_start_working_with/) # Starting / stopping ecobee based on door condition ** Requirements ** * homebridge (running on a Mac Mini 2014, which also runs AppleScript ) * json-server – follow the excellent installation instructions and examples [here](https://www.homekit.blog.com/r/HomeKit/comments/l1z82e/beef_up_your_homekit_automations_with_your_own/) * [homebridge-dummy plugin](https://github.com/nfarina/homebridge-dummy#readme) * [Eve app](https://apps.apple.com/en/app/eve-for-homekit/id917695792) – exposes more of the HomeKit system than the Apple Home app * Home app – for configuring automation steps ** Accessories ** Create an accessory with the Homebridge Dummy plugin called “10 minutes open door check”, by adding it to the config.json plugin. Set the “time” value to 1000 (ie 1 second) so that it stops immediately after starting. ** json-server and db.json ** Install the json server and familiarize yourself with its configuration using [examples](https://www.homekit.blog.com/r/HomeKit/comments/l1z82e/beef_up_your_homekit_automations_with_your_own/). For each door, you will need to create an entry in the db.json file (for me, located in ~ / .homebridge / db.json). Here’s an example of mine (two-door): {“frontDoorState”: [
{
“id”: 1,
“lastOpen”: “Feb 24, 2021 at 2:22 PM”,
“lastClose”: “Feb 24, 2021 at 2:22 PM”,
“currentState”: “Closed”,
“doorName”: “Front Door”
}
], “sideDoorState”: [
{
“id”: 1,
“lastOpen”: “Feb 24, 2021 at 3:55 PM”,
“lastClose”: “Feb 24, 2021 at 3:55 PM”,
“currentState”: “Closed”,
“doorName”: “Side Door”
}
]

I use the latest LastOpen entries, lastClose as a kind of log entries that I can look at if needed. The current state values ​​are the basis of AppleScript logic. I used [BBEdit](http://www.barebones.com) to create / edit this and you can check the integrity of any JSON file at [jsonlint.com](https://jsonlint.com). JSON can be ugly. Instead of typing the above (and possibly making mistakes), copy and paste them, then replace your information with things like “frontDoorState” and “doorName”. All other values ​​will be updated using the automations below. Before manually editing the db.json file, you should first shut down the json server (Ctrl-C). Edit it, then restart it with “json-server -H -p 3000 db.json ”. ** Scenes ** Create a scene (in the Home application) called “Scene – Door Alias ​​ON”. This scene is called by the 10-minute timer (see below) that you will create in Eve. Eve does not allow you to directly control HomeKit accessories, only scenes. This scene has only one task – turn on the “10-minute open door check” accessory. ** Automation ** 1) Create a stopwatch, using the Eve application, called “Start Door Alias ​​every 10 minutes”. In Eve, tap Automation, then Stopwatch, then Add Stopwatch. Set the Repeat mode and the Repeat period to 10 minutes (or whatever you want). Tap Next and select the Scene you created above. Touch Next provide a name for the timer. 2) For each door that has a sensor, you will need to create an automation for the moment it opens. For example, I have “When the door – the front is activated” and “When the door – the front lights up”, etc. Here’s how to build each. In the Home application, tap Automation, then tap +. Touch “An accessory is controlled” [note: my Insteon door sensors look like switches to homebridge. If your door sensors act like sensors, you would tap ‘A Sensor Detects Something’]. Select the appropriate door sensor and tap Next. Select “On” and let Time = Anything and People = Off, then tap Next. Scroll to the end of the list and tap “Convert to shortcut”. Delete the default step and add a “Get URL Content” step. Touch the URL and enter http: // / frontDoorState / 1 [note that the “frontDoorState” text in the URL must match the “frontDoorState” text in your db.json, and the ‘1’ matches the id parameter]

Touch Show more. Set the method to PATCH. Touch “Add a new field”. In the left column, enter “lastOpen” and tap CurrentDate (in the keyboard header bar) to add it to the right column. Add another field with the “currentState” key and the value “Open”. Touch Next. Repeat for each door you want to monitor 3) For each door that has a sensor, you will need to create an automation for when it closes Very similar to the “open” setting above, only: it triggers when the sensor closes; uses “lastClose” as the key instead of “lastOpen”; and the value for “currentState” should be “Closed” You can use a web browser to monitor whether open and closed automations work. Use the URL http: // / frontDoorState to see JSON. If everything works, the values ​​will change as you test your automations. 4) Create an automation for when the “10-minute open door check” accessory stops. This trigger runs an AppleScript and returns a value from AppleScript (via the “return” function of AppleScript). Create an automation and trigger it when the “10-minute open door check” accessory stops. Scroll to the end and choose “Convert to shortcut”. The first step should be “Run the script over SSH”. Enter the IP address, username and password for the Mac where AppleScript will live. In the Script area, enter “osascript /Users/admin/Desktop/HomeKit_Scripts/allDoorState.scpt” [replacing the file path with the one that leads to your AppleScript]

AppleScript (see below) will evaluate the opening / closing status of all doors and return either “Open” or “Closed” as a result. To use the returned value, after the “Script over SSH” step, add an If-Otherwise statement. It should be: If “Shell script result” contains “Open”, then turn off ecobee, otherwise turn ecobee into auto [Note: you will have to coerce the shell script result to be text. Tap on ‘Shell Script Result’ in the If statement. In the window that slides up from the bottom, choose ‘as Text’ from the popup menu]

** AppleScript ** AppleScript overcomes a number of limitations in the structure of the HomeKit in terms of decision making, etc. In essence, it goes through a list of all the doors and, for each door, extracts the JSON value for the current state of the door. If one or more doors are open, it returns the “Open” result to the speed dial command. If all the doors are closed, it returns “Closed”. Create it in the Script Editor and save it to the path you named above. on run args set doorNames to {“sideDoorState”, “frontDoorState”} set baseURL to “http: // : 3000 / “set allDoorsState to” Closed “repeat with i from 1 when counting items in doorNames set Text to (do shell script” curl “& baseURL and item 1 in doorNames) set subText to (characters (offset of” curr ”In Text) up to (number of Text characters) in Text) as a set of doorState strings to word 2 in subText if doorState =” Open “, then set allDoorsState to” Open “return allDoorsState end if end repeat return allDoorsState end run ** Overall result ** Every 10 minutes, the timer created by Eve executes the scene.The scene does its singular job of turning on the dummy accessory.After 1 second, the dummy accessory automatically shuts off, which triggers an automation to run AppleScript. uses AppleScript result to set thermostat status Door states are stored in JSON and are updated with automations running on opening and closing for each door JSON data is the basis of the decision process pleScript. ** Logs, etc. ** In the Homebridge.ui log section, you will see similar to this when the timer starts …
[2/25/2021, 2:17:15 PM] [10 minute door open check] The setting switches to true
[2/25/2021, 2:17:17 PM] [10 minute door open check] The setting switches to false
[2/25/2021, 2:17:18 PM] [InsteonLocal] Setting the power status of the ecobee to the car … In the Terminal window where json-server is running, you will see entries for all JSON GET requests and commands PATCH PATCH / frontDoorState / 1 200 3,050 ms – 154 …… GET / frontDoorState 200 2,633 ms – 172 I hope it helps you!