Hi all,
New to the openHAB automation scene, and I am in the midst of setting up my custom openHAB cloud with IFTTT, since it doesn’t seem to be working on the openHAB cloud hosted at myopenHAB. There doesn’t seem to be a guide on how to integrate IFTTT with a custom openHAB cloud, so I figure I’d pay it forward by writing down the steps I took.
First things first, in case you are confused, you cannot use the well-known openHAB IFTTT service. That is hardcoded to use myopenHAB. Instead, you’re going to have to create a new unpublished service at IFTTT Platforms, much like you have to do with integrating a custom openHAB cloud instance with Google Assistant or Amazon Alexa.
Before creating the service, there are some setup steps to make testing everything easier. ssh
into your openHAB cloud, and follow the following steps:
-
Generate a Client ID and Client Secret that’s shared between IFTTT and your custom openHAB cloud. I used Python 3’s “secure” library to generate two custom strings. Run the
python
command, then, in the shell, type in>>> import secrets >>> secrets.token_hex(32) >>> secrets.token_hex(32)
You can use the first as the Client ID, and the second one as the Client Secret.
-
Optional: Create a new registered demo user on your custom openHAB cloud so that you can test IFTTT’s integration. This may involve changing
"registration_enabled": true
in your config.json. Fill out the openHAB cloud’s sign-up form, and set the openHAB UUID and secret to something random (it won’t be used).Technically, you can use an existing account, but I prefer to create a new one since the credentials will end up on IFTTT and I can use the provided custom openHAB cloud tests.
-
Optional: Create fake items in mongodb so that IFTTT tests can change the item states successfully. Open mongodb’s CLI with
mongo
, and follow the steps:- Run
use openhab
to change the database. - Run
db.openhabs.find()
to find the list of openHABs registered in the cloud. Find the openHAB that you created by comparinguuid
andsecret
fields to the random openHAB UUID and secret you used. Copy the_id
associated with that object. - Run the following, replacing
_id
Note that we got the item names from some sample code in the custom openHAB code (see>>> db.items.insert({ openhab: ObjectId("_id"), name: "Light_GF_Kitchen_Table", status: "OFF" }) >>> db.items.insert({ openhab: ObjectId("_id"), name: "DemoSwitch", status: "ON" }) >>> db.items.insert({ openhab: ObjectId("_id"), name: "Temperature", status: "21" })
routes/ifttt.js
in the GitHub repository. These are items that our custom openHAB cloud will make available to IFTTT for the IFTTT tests.
- Run
Go ahead and create a new service at IFTTT Platforms. There’s a “General” tab in the UI that asks you information about the “Service name”, “Description”, etc… None of that really matters because the service will not be submitted. What you really want to do is go to the “API” tab and follow these steps:
- Fill out the “IFTTT API URL” field to “https://[your openHAB cloud URL]”.
- Note the IFTTT Service Key. You’ll need it further down.
- In the Authentication page, mark the radio button “My API has users with non-expiring OAuth2 access tokens”
- Set the “Client ID” and “Client Secret” to what you generated above.
- Set the “Authorization URL” to “https://[your openHAB cloud URL]/oauth2/authorize”
- Set the “Token URL” to “https://[your openHAB cloud URL]/oauth2/token”
- Optional: Add the demo credentials that you created previously.
Now, we need to make IFTTT aware of all of the possible Triggers and Actions offered by our custom openHAB cloud. This involves basically recreating the capabilities that main IFTTT openHAB service offers, except using our own endpoints…
- Go to “Triggers”, remove the default “A new thing was created” Trigger, and add the following 3 Triggers:
- “Item state changes” with description “This Trigger fires every time an item changes its state to a value you specify.” and API endpoint
itemstate
. - “Item state raises above” with description “This Trigger fires every time an item’s state raises above a value you specify and works with any number item.” and API endpoint
item_raised_above
. - “Item state drops below” with description “This Trigger fires every time an item’s state drops below a value you specify and works with any number item.” and API endpoint
item_dropped_below
.
- “Item state changes” with description “This Trigger fires every time an item changes its state to a value you specify.” and API endpoint
- Go to “Actions”, remove the default “Create a new thing” Action, and add the following Action:
- “Send a command” with description “This Action will send a command to one of your openHAB items.” and API endpoint
command
.
- “Send a command” with description “This Action will send a command to one of your openHAB items.” and API endpoint
- Go back through all of the Triggers and Actions you added, and add a new field (i.e., “Trigger field” and “Action field”). When creating IFTTT applets, this allows IFTTT to know what openHAB Item to respond to (for Triggers) or to act on (for Actions). The field should have the label “Which item?” with “Key name” set to
item
, “Input Type” set to “Dropdown list”, and “Data source for dropdown list” set to “Retrieve list items from my service”. - Go to the “Item state changes” Trigger and add another field with label “What status?”, “Key name” set to
status
, and “Input type” set to “Text input.” - Go to the “Item state raises above” Trigger and add another field with label “Above what value?”, “Key name” set to
value
, and “Input type” set to “Text input.” - Go to the “Item state drops below” Trigger and add another field with label “Below what value?”, “Key name” set to
value
, and “Input type” set to “Text input.” - Go “Send a command” Action and add another field with label “Command to send”, “Key name” set to
command
, “Action field type” set to “Text input”, and the option “Short text (140 characters or fewer)” checkmarked.
Now, you’ll need to make some changes in your custom openHAB cloud. ssh
into it, and follow these steps:
-
Open the mongodb CLI by running
mongo
and enter these commands>>> use openhab >>> db.oauth2clients.insert({ clientId: "<CLIENT-ID>", clientSecret: "<CLIENT SECRET>"}) >>> db.oauth2scopes.insert( { name : "ifttt", description: "Access to openHAB Cloud specific API for IFTTT", } )
-
Open your cloud’s
config.json
and add the IFTTT service key:"ifttt" : { "iftttChannelKey" : "[IFTTT SERVICE KEY GOES HERE]", "iftttTestToken" : "" },
Note that IFTTT “channel key” and “service key” are interchangeable. Also, you will optoinally fill out “iftttTestToken” later below.
-
Restart the custom openHAB cloud server.
Optional: Now, go back to the “API” tab, and follow the steps:
- Go to “Authentication Test” and run “Begin test”. You should be led to your custom openHAB cloud logged in as your demo user asking you if you want to grant IFTTT access. This means that it is successful.
- In the successful results, find the “Request access token” step. Look at the Response Body, and find the value for the key
access_token
. You will need to go back to your custom openHAB server, add that value as the value for “iftttTestToken”, and restart the server. Thanks to Setup your own openhab-cloud (myopenhab) server/instance for the tip. - Go to “Endpoint Tests” and run “Begin test”. Note that the triggers and actions tests should fail because the custom openHAB cloud did not reformat the responses properly, but if you through the failed tests, you’ll at least see that every requested returned a successful response (i.e., 200).
Well, that’s it! Now you can go create your own IFFTT at ifttt.com/create, using the “Service name” to find your service. If this was helpful or if you run into any problems, please comment