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.

9 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!!