Triggering Items Using openHAB 2's REST API from Tasker

tasker
openhab2
Tags: #<Tag:0x00007f182738d448> #<Tag:0x00007f182738d290>

(Rich Koshak) #1

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 ItemActionand chooseStop`. 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.


Tasker with myopenHAB.org (Screenshots for tasker)
Control openHAB via Workflow App and Notification Center in iOS
Transform android phones in a new sensor
Is there a way to notify openHAB just before a phone dies?
Nfc tag
Another presence method - Android specific
Phone ringing detector for the hearing impaired (land-line & mobile (Android))
Presence arrival using DHCPlisten possible?
Mobile Sensors OH
Can I get a Phone location within my house?
How to send a command to openhab (from Arduino doorbell)
(Michael “Dot”) #2

to create a variable within tasker. Go to the variable tab and then hit the + sign and name it. if you have multiple projects loaded within tasker, it will show up on the first projects variable tab. Then click the variable and input the information that you want. You can however create an interface that would prompt to enter all of the information that is missing in given variables. Check those variables to see if set, if not, prompt for input.
There is a very good tasker variable backup task that will allow you to export all of your variables to an external file to recover easily.


(Rich Koshak) #3

Links to tutorials and/or examples, particularly the backup task, would be appreciated.


(Michael “Dot”) #4

Yeah, sorry. I got pulled away at the end of my response and forgot to add more before posting. I basically did what I hate seeing in forums.
Anyway. Here is the link to the reddit post that has the xml files to import for the backup, restore functionality. Also, you can encrypt the backup information with autotools encrypt. It explains that in the reddit post as well.

Here is a link to the xml files to import from Google Drive:
https://drive.google.com/open?id=0B2qpJ_atYNy4SEJVVmFId0p2S28

I do not have a link to a tutorial for the tasker add variable steps. but it is really straight forward. I use tasker in the advanced mode. not the beginner mode. so maybe that is the issue for you, i dont know because I have been building my tasker setup for years. 5 or 6 at least.

1: open tasker
2: open variables tab
3:click the plus sign in the lower right hand corner
4: enter the name of the variable.It needs to contain an uppercase letter at the beginning, or the whole thing if you want.
5: check the vars tab in the main project. usually the one with the house icon. Your variable should be blank, click on it and it will prompt for input. Input the value.
6: profit

Now as to prompting for input on sets of variables that are empty. That will take me longer to put together. I would upload my tasks to allow others to use. but I need to vet them for sensitive information. My setup has gained complexity beyond the ability to just look at it and understand what is happening without a deep dive in to all of the linked tasks. If I get a chance soon, i will try and scrub those and upload them.

Right now i am working towards populating variables using the REST interface to pull all available items and states so that I can act on them dynamically instead of having to create new variables whenever items are added.

I have been using rotini as an interface for a couple of months now, and the project is going a different direction than I want to go with my system. I do not like the new interface for the app or the web version available. Especially creating individual setups for each device.
Ultimately, i want a tasker plugin, or setup that pulls in everything and easily allows setup of tasker without having to know the name of every item off hand. Just need to parse the xml file that returns fro the REST request.

Do you have any links, or understand how to parse the returned info easily? what would be the best way to do that? I want to be able to create variables with each item, and a variable for the current state of that item. it is easy enough to share tasker setups between devices. they are able to link in to almost everything you can think of and they are not going to change randomly because of a developers individual taste changing.


(Rich Koshak) #5

I’ve been using Tasker since it was beta, but I haven’t kept up with all the changes that have been made to it over the years. They keep adding stuff to it but I’ve been satisfied with my automations so haven’t looked into them. That Vars tab is one of those features.

I’d love it if there were an MQTT plugin that lets you publish AND subscribe (there is a publish plugin). Then one could just subscribe to the event bus topic and Tasker would have access to everything. Right now I don’t even have time to keep up with my other OH commitments to even think about writing a plugin myself. Sigh…

Unfortunately XML parsing is still listed as a TODO.

http://tasker.dinglisch.net/todo.html

You might be able to cobble something together using regular expressions:

http://tasker.wikidot.com/regexcapturegroups

It’s going to be ugly though.


(Jay) #6

Hi,

I just tried this but I keep getting the following error in tasker for the HTTP Post:

17.37.52/Variables doreplresult: |https:%OHSERVER:%OHPORT| -> |https:<email>:<password>@my.openhab.org:443|
17.37.52/Variables doreplresult: |https:%OHSERVER:%OHPORT| -> |https:<email>:<password>@my.openhab.org:443|
17.37.52/E prot: http:// serverport: my.openhab.org:443 contenttype: text/plain
17.37.52/E method: POST url: http://my.openhab.org:443/rest/items/mobile_jay timout: 10000 dataisfile false save null
17.37.52/WakeLockManager acquired partial lock for M flags: 1 autorelease: true warn: true
17.37.52/WakeLockManager setClearAlarm: not setting, last set 18ms ago
17.37.52/WakeLockManager setClearAlarm: not setting, last set 19ms ago
17.37.52/E body isfile: false cont: ON
17.37.52/E set content-length: 2
17.37.52/E write postBody string
17.37.52/E done post body
17.37.52/E Input/Output error for http://my.openhab.org:443/rest/items/mobile_jay: java.io.IOException: unexpected end of stream on Connection{my.openhab.org:443, proxy=DIRECT hostAddress=151.101.17.147 cipherSuite=none protocol=http/1.1} (recycle count=0).
17.37.52/E result: stop task (error)
17.37.52/E Error: 1
17.37.52/MacroEdit action finished exeID 2 action no 1 code 116 status: Err next 1

Any ideas?


(Fastcar550) #7

Are you using 2.x or 1.8? If you are using 2.x then it should be https://myopenhab.org


(Rich Koshak) #8

Actually it should be myopenhab.org for both versions. The old my.openhab.org services were shut down months ago.


(Jay) #9

Hi thanks for your reply. I am using 2.x and already had it changed to that. I was trying different things, which is why my logs say my.openhab

I got it to work eventually. I can’t remember exactly what the problem was though


(SiHui) #10

How did you do it? A little guide with the basic steps would be great and could be published in the tutorial section:v:


(Jay) #11

Sorry I actually can’t remember what I ended up changing that made it work. It may have been because it was a new myopenhab account and hadn’t verified my email address yet or the java version I was running (I vaguely remember coming across a Java issue while looking at this)


(Tim) #12

This initially didn’t work for me but I just had to add “//” after the HTTPS:

Server:Port

https://%OHSERVER:%OHPORT

Thanks Rick for the great tutorial!


( ) #13

The double slashes are needed by almost all address schemes (see URI). http and https are always followed by ://. Anyhow, to clarify it @rlkoshak I’ve corrected your OP.