Sending commands to REST API via Siri Shortcuts whilst outside of LAN

I was able to successfully send a command to the REST API using Siri Shortcuts whilst outside my LAN.

I knew it was possible to do it from within the LAN but it’s taken hours to figure out how to do it from outside the LAN (Google was very much my friend here). In short, the key was to encode the credentials using Base64 and to not escape the @ symbol in the username. Only figured it out when I compared the output of a cURL to what I was generating using the Shortcuts app.

Below is a step-by-step guide as well as some screenshots. I couldn’t work out how to publish a sample shortcut without removing my credentials, so hopefully this guide will be enough to get others going.

  1. Add a “Text” action. In the action enter the command you wish to send: ie OFF

  2. Add another “Text” action. In this action, enter your credentials for myopenhab.org as such: username:password . If your username contains any symbols, eg the ‘@’ symbol, do not escape these.

  3. Add a “Base64 Encode” action. This should be after the “Text” action which contains the credentials. A line should connect the “Text” and the “Base64 Encode” actions. I selected “None” in Line Breaks (this can’t be seen in the screenshot) but I don’t think it matters.

  4. Add a “URL” action. In this action, add the url to your item’s REST endpoint ie https://myopenhab.org/rest/items/item_to_switch

  5. After the “URL” action, add a “Get Contents of URL” action. This action contains a few entries which require configuring.
    Change the Method to POST and add the following 3 headers:

    • Content-Type: text/plain
    • Accept: application/json
    • Authorization: Basic
      In the Authorization header, after the word Basic, insert a link to the “Base64 Encode” action (check out the screenshot). A space is required between the word Basic and the “Base64 Encode” action. eg: Basic Base64 Encode

    Finally select File in the Request Body section and insert the Text action which contains the command you wish to send to the item.

After this press the play button. A successful response will (other than switching your item) result in a blank page in the quick view under the final step.

Hopefully this is useful to others

Good luck

1.

2.

3.

10 Likes

Rest API is the only way to send commands?

Thank you sir, this saved me a lot of effort and time as getting this to work was on my to do list :raised_hands:

1 Like

I’m not really in a position to say so definitively, but I believe your options to control OpenHAB remotely are either the Android or iOS apps, the native iOS Home app (you need an ipad or apple TV permanently at home), possibly Alexa or Google Home (don’t know anything about those) or Rest. It’s possible that some of those solutions already use the Rest API. Somebody who knows more might be able to correct me

To my knowledge the openHAB uis including the apps and basic ui / classic ui also use the rest api to communicate with openhab. The Same is true for all external services like the node red openhab nodes or habapp.
Johannes

You can use a modified version of the mqtt2 eventbus rules to send commands over Mqtt.
The examples are quite hard to find right now as they are only on github, so here is a link:

This is exactly what I was looking for with using NFC and shortcuts !

Thank You!!

Hi,

first, thanks for sharing this.
I have been trying to achieve that for quite some time.

Unfortunately, even though I followed all your steps, my shortcut does not work.
I can send a command to openHAB using the REST API from my local network, but it does not work from outside of LAN.
I think the problem is the authentication, but I don’t know how to solve it?
Has it changed in newer versions of iOS?

Hi @sercasyr

I think there were changes in authentication between OH2 and OH3 (others may be able to advise more accurately). I stopped using the shortcuts as I purchased a homepod so I can now access the items remotely through the home app, which works much better.

Since then I also upgraded to OH3. However what hasn’t changed is the shortcuts scripts I created, which were based on my OH2 installation. I just tried them for the first time in ages (both cinnected to the lan and on mobile data) and they seem to still work without any amendments whatsoever.

I also checked my OH3 installation and I have basic authentication turned off, and I’m definitely not using an api token for this.

In short I’m not sure why this isn’t working for you

So,

you have OH3 and the REST-based script still works, right?

I also have OH3 (mine is OH 3.2) and it doesn’t work. I tried basic authentication off and also on, with no success. It doesn’t seem to make a difference.

Can it be a question of the iOS version?
I use iOS 15.2
Which one is yours?

Thanks anyway.

I have iOS 15.4.1. Also OH 3.3 milestone 3

I developed the shortcuts in a much older iOS version, maybe 12 I think. And yeah they just seem to work with no changes

I just stumbled upon the same problem. I couldn’t replicate the proposal from @Lukie . But what did work is to make it happen with a token.

In the Headers of “Get contents of” change the “Authorization” header to:

Bearer {your token}

There seems no Base64 encoding necessary.

The token can be created in the UI after clicking on the home screen on the logged in user (bottom left). (I created a dedicated user “apple” to get presence information from homekit via an AppleTV.)

This might be helpful: 4 Most Used REST API Authentication Methods