Sending requests into my.openhab from a web application

Is there any documentation on sending requests into my.openhab from an application? I was hoping for a simple put of some kind like:

https://my.openhab.com/requests/request?uuid=1234567a-f14d-446f-837a-a9cce68eb5fd&secret=NNPl1uf3Wkn2FulhP1HW&item=Bedroom_Light&value=ON

I assume calls like this are made within the local openHAB instance to communicate with my.openhab.

1 Like

Based on the stated intend of my.openhab by Kai I don’t think sending rest requests this way is possible. If you want to use the REST API you will either need to punch a hole in your firewall, connect via a VPN, or use IFTTT as your integration point.

1 Like

How does the local instance of openHAB talk to my.openhab? How about the HabDroid or iPhone or iPad applications, they both have to use the uuid and the secret as part of some http request don’t they?

My.openhab is a persistence type binding. Anything that is configured to be persisted via my.openhab gets published to the cloud through some sort of authenticated mechanism that uses the uuid and secret. I don’t know the details of this but I know it uses commercial grade SSL certificates and other infrastructure (per the post that linked to previously). There is no guarantee that this communication is REST nor is there a guarantee that it is two way (note to self, I need to try and see if I can get historical data from it to see if it is two-way). They may only use the uuid and secret when setting up a unique session and not include it as part of every URL request (which make it not be REST). I’ve never captured the web traffic so I can’t say how the comms work.

I don’t think so. When you configure the Android or iPhone apps you only enter your username and password, not the uuid and secret. I think the uuid and secret are only used for the communication between the my.openhab binding and the cloud service.

When I tried to access the REST API using my.openhab the same way I would do it communicating to openhab directly:

https://user:password@my.openhab.org/rest

it just redirects me to http://my.openhab.org.

I do not know the mechanism used by IFTTT to communicate with my.openhab but the uuid and secret were not part of the information that I entered there either. Instead it initiated some sort of OAuth approval from my.openhab.

So in short, I still think it is not possible to interact with my.openhab through a REST API without some additional type of authentication (e.g. IFTTT’s OAuth like approval).

Rich

Thanks for the extensive reply. I admit to not understanding some of it but my programming skills are old, as I am. That said here is what I want to do and my progress so far.

I just got an Amazon Echo and it’s pretty cool. I have looked at this openHAB to Echo interface and I guess it would work fine for me but I had other ideas. I want to pass the entire text spoken to the rules, to VoiceCommand, so I can parse the text as I see fit. Anyway I just wrote a AWS Lambda Alexa Skill program which I have successfully deployed to my instance of the Echo. I say “Alexa, ask open hab”. She replies “OK, what do you want?” Then I reply with my command. So I have a variable in my program that has “Turn on the bedroom light” or “Set the thermostat to 75 degrees” or “Set the at home theme”, which is the Echo voice to text. At this point I want to send the entire text to my local instance of openhab, which I thought was going to be a breeze via my.openhab.

I guess I could write something similar to this guys idea. Basically I would write a headless web server in java (his code is ruby I believe) that would just sit there on my local machine and constantly get the last command that was said on the echo. He does this with some JavaScript that pulls off the last command from a stack on a webpage. I’m not fond of that idea because I would have to process all of the commands, and you have to fake out the Echo by ending each command with “Stop” so it isn’t executed on the Echo end. Anyway it’s slightly clunky and doesn’t really interface with openHAB.

If all else fails I could write a relay service of some kind. I wrote one some years ago using Googles development environment. I’ll have to dig out that code and dust it off big time if I don’t run out of energy.

Bottom line it sure would be a piece of cake at this point if I could just send an http request like my example above to my.openhab. Since it’s a free service I guess they don’t want to flood it with yahoos like myself sending it all kinds of crap. Damn I thought the hard part was going to be figuring out how to write the AWS Alexa Skills Kit stuff. That turned out to be rather easy.

I admit that I don’t know much about Echo but based on the tutorial you link to you don’t need my.openhab or to write some bridge at all. The purpose of my.openhab is to expose your openHAB to IFTTT and clients that are external to your LAN without the knowledge of how to open up a port in your firewall or setting up a secure connection (e.g. VPN).

Since your Echo is on the same LAN as your openHAB, there is no need for this, Echo can talk to openHAB directly through the native REST API as documented here. If your openhab.cfg is configured for security=external you don’t even need to supply a username and password.

If I’m reading the tutorial correctly, you just need to use the LAN IP address of your openHAB instance in the onURL or offURL JSON config you send to the Echo and you should be good to go. It really is as easy as that and there is no need for my.openhab in the mix.

You might also be interested this thread and this thread which is related to VoiceCommand.

Rich

It looks like I need to read some of those materials a bit better. Thanks for your feedback it is much appreciated. Now I just have to figure out what to do with my newly acquired AWS Alexa Skills Kit knowledge :smile:

@rlkoshak

Kai confirmed in the other thread that you can use the REST API through my.openhab.

Not sure if you tested it through command line or browser but I’ve also confirmed it worked using the following URL from a browser (had to manually log in the first time):

https://my.openhab.org/rest/items/bedroom_light

And received the following response:

{"link":"https://my.openhab.org:443/rest/items/bedroom_light","state":"OFF","type":"StringItem","name":"bedroom_light","tags":[],"groupNames":["bedroom","lights"]}
1 Like

@danielwalters86, I just saw that.

I did test it through the browser and curl but I now realize that I didn’t escape the “@” and “.” in the username part of my URL (since it uses an email address for the username). Once I did that it seemed to work, in Firefox at least. But when I tried the same thing in Chrome I get an “Cannot GET rest” returned instead of the XML.

For the record, an escaped “@” is “%40” and the escaped “.” is “%2E”.

The problem with interacting with the API from another program (e.g. Tasker) is there isn’t always a way to manually log in so if it doesn’t work with putting the username and password on the url it isn’t all that useful.

I updated the REST wiki page with this information.

Rich

@rlkoshak were you able to do a post/get with Tasker to my.openhab? I seem to be able to do it from browser or curl but not Tasker.

I’ve never been successful with HTTP POST in Tasker. I was able to get HTTP GET to work. It took some experimentation to get it right with my.openhab.org though. The trick was to NOT escape the special characters in the username or password. Tasker handles the conversion for you and it treats “%” as the start of a variable name so things get all messed up when you try to escape it yourself. I only use REST interface to post commands from Tasker so you will have to research how to do the rest.

Here are my settings.

  • Server:Port = https://[username]:[password]@my.openhab.org:443
  • Path = CMD
  • Attributes = Itemname=ON

I doubt the port number is required but I haven’t tested without it.

Rich

I have just started to use IFTTT and so far I haven’t been very happy with it. It seems to take too long for the messages to get to my openHAB at home. I have set up a Recipe that is suppose to trigger when I’m approaching or leaving the area around my home. I have been having to increase the size of the area in order for the message to get to my house before I get there. The point is to turn on some outside lights but on a many occasions they never came on or they came on after I arrived home.

Anyway what is everyones experiences with Tracker? It sounds like @rlkoshak has figured out how to send a message to OH.

I’ve had the same experience with IFTTT. I sometimes responds immediately and other times can take a minute or two to respond. If my.openhab happens to be down (check your openHAB logs) the message just gets lost. It is particularly annoying with IFTTT Do button because I get no error when the command is lost so press it again. Pretty annoying when trying to raise the garage door. I still use it right now though as I need something ultra simple and quick to get to for my wife’s iPhone to open the garage and so far IFTTT Do is the closest I’ve come to that on iPhone.

The problem which I’ve seen posted by the developers on a thread I can’t find right now is that IFTTT works by polling my.openhab and the polling period is on the order of a minute in my experience.

Since I got it to work with Tasker I’ll post my experience.

First of all, Tasker is an Android only thing, sorry iPhones (it is also the ONLY thing keeping me from getting an iPhone for myself). Secondly it is a tad expensive for an app ($7 I think the last time I looked). Thirdly, to get voice and geofencing support you need to get the AutoVoice and AutoLocation plugins. Finally, like openHAB itself, Tasker has a fairly steep learning curve. However it works fairly similarly to openHAB (i.e. rules that get triggered by events) so what you have already learned should help you get up to speed on Tasker.

What I’ve done is put some widgets on my home screen that trigger the garage doors and the lights for quick access. These widgets work great (I hesitate to call them widgets since they are really just icons), though I communicate with openHAB directly as opposed to through my.openhab in my day to day usage. They respond instantly and unlike IFTTT I get an error if the web request fails for some reason. In my testing to figure out how to get it to work with my.openhab it also seemed to have no noticeable delay between widget press and action.

I’ve also created voice triggers using AutoVoice so I can say “Listen Up Moto X, Open the garage” and AutoVoice will capture the command and execute the call to trigger the garage.

Finally, I use AutoLocation to create geofences (similar to IFTTT’s location recipes) which trigger when I enter or exit a certain area. I use this to silence my phone when I’m at work automatically and when I’m coming home it will bring up a confirmation dialog asking if I want to open the garage door when I get close. Unfortunately my phone took a dive in the tub while I was giving the little one a bath and the GPS has become spotty since then so AutoLocation doesn’t work all that well for me anymore. But it worked very solidly for awhile. My only caution would be to either set your geofence pretty wide or to add a confirmation dialog before activating something automatically because if you spend a lot of time near the border of your geofence it might trigger the item several times. I learned that one day when playing in the back yard with the kid and my wife asking me why the garage door kept opening and closing for no apparent reason.

Tasker with AutoLocation or IFTTT are not the only ways to achieve geofencing. You can also set up geofencing within openHAB using the Mqttitude binding and apps. This has the advantage of working on both Android and iPhone, though it might drain your battery a little faster (I found it to be quite light weight personally, but I couldn’t train my wife to restart it on her iPhone and it wouldn’t stay running in the background for more than a day before needing to be relaunched). @sipvoip has also hired a developer to create an iPhone native binding that will get the location information from Apple’s servers rather than the phone (if I understand his postings correctly) which will help with the battery on iPhone but not help Android users. Follow him, I’m sure he will post something when it is completed.

Finally, since I’m plugging joaomgcd’s other apps, AutoInput is another great Tasker plug-in that lets you basically screen scrape and simulate presses in your apps. I use it to scrape my ETA from Waze and text it to my wife when I get in the car and to automatically play when I connect to my car’s bluetooth and back up 30 seconds in Smart Audiobook Reader, Audible, and LibriVox when I disconnect from the car’s bluetooth. I haven’t found a need for it in home automation yet though.

Hi @rlkoshak

I’ve not long got back from holiday so haven’t fully caught up on your quite comprehensive post. However following your earlier comment on unescaping the username/password I can do a HTTP POST from Tasker via my.openhab :smile:

Regarding AutoVoice…do you need to say “Ok google…listen up Moto X, open the garage”? Or is the Moto X a bit different in terms of voice triggering? I’m trying to find a good way to trigger voice recognition whether from Moto 360 or my phone but either one seems to require me to say “Ok Google” or to turn on continuous listening on my phone at the expense of battery.

Yes, you have to say your trigger phrase. Before this latest update to Lollypop you had to say “OK Google…open the garage”. Now you can make it any phrase you want. But in either case you say the trigger and then say the AutoVoice trigger. AutoVoice intercepts the intent that voice search sends and if it matches a profile trigger executes it, otherwise it gets passed through so you don’t break everything else. It can also be triggered through text searching through Google Now (e.g. search “open the garage” and it will trigger).

The Moto X 2013 (which is what I have) was the first phone to have the co-processor that lets you trigger the phone while the screen is off without chewing up your battery.I believe that feature has rolled out to other models. I can’t speak to how the Moto 360 or other models will work. I suspect you would have to turn on continuous listening if you want it to work without any other interaction.

To make long story short.
IFTTT can lag sometimes, cause their API has a polling nature. They poll my.openHAB every several minutes.
my.openHAB is intended to provide secure access to your openHAB via standard HTTP requests. So any REST API call to openHAB will work through my.openhab.org.

Sorry for bringing up an old thread…

To use Tasker and my.OpenHAB, i use custom URI scheme instead of http post/get… (w/ HabDroid installed)

E.g
> openhab://sitemaps/home/home?item=TV&command=TOGGLE

@Farhanito

I am very interested in your Tasker implementation. I use Tasker quite a bit on my phone but haven’t ventured into the Sent Intent actions yet. Is your example “openhab://sitemaps…” in the Action field or one of the other fields?

Thanks.

Hi Mark,

I just use Browse URL action and put whole URI there, not Send Intent action.
I found easiest way to get the URI is by writing an NFC tag from habdroid, and have NFC Tools app read the tag.

@rlkoshak

Thank you for the example tasker to my.openhab settings. It worked like a charm for me. It works perfect for using “OK Google” to send voiceCommands to openhab!