As many know, the REST API has changed significantly between openHAB 1 and openHAB 2. This tutorial is for those who are migrating to OH 2 or trying to get OH 2 to work for the first time with Tasker.
These instructions will tell you how to send an ON command to a Switch Item using my.openhab to access your installation. This should be enough to get anyone who is looking to do it some other way or send other commands to get started.
For details about the REST API, see the REST API docs that come with and/or is available for installation on openHAB 2.
The setup described below is a little more involved than the bare minimum required. Only the Create Triggering Task and choosing come action to trigger this task are truly required. The rest of the steps add checks in place so you can confirm the action and deal with working with different URLs (e.g. when on your LAN) easier.
Assumptions
This tutorial assumes you have a working openHAB 2 instance with the my.openhab binding installed and working. It also assumes you have Tasker installed on your Android phone. Sorry Apple folks, Tasker is not available for your platform.
It assumes you have a Switch Item that works the way you want it to.
Finally, this is not a full Tasker tutorial. It assumes you have a working knowledge of Tasker. As a result, many steps and explanations will be skipped.
Limitiations
- Unfortunately you must store your username and password within Tasker. NOTE, Tasker does not encrypt this data, use at your own risk.
Create a Triggering Task
To start we want to create some global variables, one for the URL and the other for the port. We will use these to construct the URL that will be used to trigger the Switch. This is a good opportunity to create different versions of these URLs if you plan on having Tasker use a different URL when on your LAN (i.e. talk direct to openHAB).
I’ve not figured out how to create a variable in Tasker except though executing a task (if anyone knows how please respond with how and I’ll update up here).
Create your task, I called mine Garage Direct as this switch controls my garage door opener. Add two Variable Set tasks, one named %OHSERVER and the other named %OHPORT.
- %OHSERVER - set this to
<user>:<password>@my.openhab.org
where<user>
is the email address you use to log into my.openhab.org and<password>
is your password for my.openhab.org. - %OHPORT - set this to 443, the default port for HTTPS.
Run this task to set these two global variables. Once set, because they are named in all caps, these variables are globally available and will persist across restarts of Tasker and the phone.
If you are not using my.openhab, use your locally set username and password and port (by default 8443).
Next edit this task and remove the two variable sets, we don’t need them any longer. Now add a Flash with a suitably appropriate message (e.g. I use “Attempting to open the garage”)
Now add an HTTP Post command with the following settings:
- Server:Port -
https://%OHSERVER:%OHPORT
- Path -
rest/items/<item name>
where<item name>
is the name of your Item which will receive the command - Data / File -
ON
or what ever the command to be sent should be for your Item - Trust Any Certificate - checked if using your own server and self signed certs
- Content Type -
text/plain
- Continue Task After Error - checked
Next add a confirmation message so you know whether the command was successfully issued or not.
Add a Flash with
- Text - an appropriate success message (I used “Successfully issued command to open the garage”)
- if - %HTTPR eq 200
Add another Flash with
- Text - an appropriate error message (I used “Failed to issue command to open the garage: Code %HTTPR”)
- if - %HTTPR neq 200
Now you are basically done. However, there are some additional checks that can be put in place which can be helpful, particularly if this is an action that takes place automatically which you may not (e.g. open the garage when the phone things you have just come home).
Confirmation Dialog
Create a new task, I called mine “Garage Opener”
In this task add a Menu:
- Title - an appropriate dialog title, I used “Open the Garage?”
- Timeour - choose an appropriate amount of time before the menu choose the default Item.
- Items - First Item
Perform Task' and choose the task created in the previous step, Second Item
Actionand choose
Stop`. Label the second Item with “Cancel” and choose an appropriate icon. Set this as the default action by putting a check next to it.
If you have any automated action that triggers this option but you don’t want it to occur without a confirmation, have that trigger execute this Task rather than the “Direct” tasks.
For example, I have AutoLocation trigger this action when I enter my home geofence. This means that this dialog pops up and I can confirm or cancel the action. Thus, the garage door only opens when I’m driving home and not in the back yard near the border of the geofence. I implemented this fix because one day the garage door opener kept going up and down as I mowed the lawn.