Skip to content

Enhance HomeKit automations with your own JSON REST API

Enhance HomeKit automations with your own JSON REST API

Enhance HomeKit automations with your own JSON REST API

Good morning homekit.blogors! I come to you with a guide born from a pet project that can really improve HomeKit automation. INTRODUCTION: using your own JSON REST API server with json-server (https://www.npmjs.com/package/json-server) if you are like me and use HomeKit and have a Homebridge instillation, this is not a brainer in the conditions an installation. It installs in about 30 seconds and works with a pretty flawless Raspberry Pi. Its simple installation: `sudo npm install -g json-server` With this, you now need to create a db.json file in your user / home directory. You can use jsonlint to check it. But you have to have a few keys. Here is an example of what I use. (Not everything, just a taste 🙂 “” {“Roomba”: [
  {
     “lastRan”: “Jan 20, 2021 at 12:06 PM”,
     “lastFinish”: “Jan 20, 2021 at 12:39 PM”,
     “id”: 1,
     “running”: false
  }
], “bedtime”: [
  {
     “id”: 1,
     “lastRan”: “Jan 19, 2021 at 8:08 PM”,
     “morningLock”: false
  }
]
} „„ Here are 2 matrices I use. One contains information about my last Roomba operation, when it was last completed and IF it is running. Each array needs an “Id” key. This key is used in a PATCH call that you will see in a minute. This will be our example. Now that we have created db.json, you will run this command in the terminal. `json-server -H YOURIPHERE -p 3000 db.json` This will start the service and extract the database. If you ever make a change with this outside of API calls, you’ll need to stop the script and reload it. It will then extract some examples of API calls. For our Roomba, our URL will be as such For GET requests, it will be http: //MY.IP.HERE.PLS: 3000 / Roomba For PATCH http: //MY.IP.HERE.PLS: 3000 / Roomba / 1 Now, for the PATCH call, you need to specify the “id” key in the URL, which in this case is 1. if you want to play with DELETE or POST, you can check the NPM page because it has a lot of documentation. With this operation, we will reach automation for our Roomba! Each of these automations will be shared as a “shortcut” link. To turn them into HomeKit actions, you’ll need to do advanced HomeKit automation and add actions (sorry). The shortcut below contains some examples of calls you can make. You can use one action for each automation. https://www.icloud.com/shortcuts/66300ea4cae9441cb2b768cddc5e783c Now, when Roomba starts, it records the date it ran, as well as its running status as TRUE. When stopped, it will record the date it ended, and set the running status to FALSE. These booleans can be used for a multitude of things that are not exclusive to HomeKit. But it can give you more to work with. However, it’s important to note that we’re using a PATCH request, so we don’t create duplicate entries under new IDs. Now that we have a recording system when Roomba worked, what can we use it for? For me, I want Roomba to light up when my wife and I leave, but I also want it to disappear at noon. but not ENVIRONMENT in one day. So we can use the date it last ran or was completed to tell both automations to quit if Roomba has been running for the last 12 hours! Here is an example of automation. https://www.icloud.com/shortcuts/ca55b0652bf84073a5eed0d1c1bc1998 This is a simple example that you can apply to a vacation and time-based automation. But actually, the technique is the same. You can even work harder to make it more elegant. Personally, I use Pushcut notifications (which are entered as API calls) to let me know if Roomba couldn’t start or what you have. And that’s it! That’s all you have to do! You can become very fancy with this if you want too! Allowing you to create “Global Variables” or make shortcuts that can call this data like this! https://www.icloud.com/shortcuts/fca4f5d419494df7bf41bf476ba43349 Or you can make one as my key “night of sleep”. Which allows me to perform one scene a day as I enter the bedroom. (It’s more advanced than that and I can detail in the comments) Anyway, happy automation! Tell me what you think! EDIT: Hey folks! This post receives prizes! Thank you very much! It means a lot! However, if anyone else would like to do this, I would love if you could send a donation to either of these two charities on my behalf! It would mean a lot 🙂 https://autisticadvocacy.org/get-involved/ https://www.lpaonline.org/donate