Shelly scripting - detached mode

Does anyone have the brains to make this work for OpenHAB?

This should easily be adopted for OH. The script on the Shelly should be exactly the same, just replace the http://YOUR_HOME_ASSISTAN_URL_OR_IP:8123 with http://YOUR_OPENHAB_URL_OR_IP:8080 in the script.

All the script does is try to get to some web URL and if it fails it changes the mode of the Shelly. It could be any URL.

I’m trying to use the rest API to check if my zigbee hub thing status is ok using

http://IP:8080/rest/things/deconz%3Adeconz%3A00212E06ECA0/status

but get {“error”:{“message”:“Authentication required”,“http-code”:401}}

How can I add authorisation to this URL?
It doesn’t work with

username:password@IP:8080/rest/things/deconz%3Adeconz%3A00212E06ECA0/status

How do I format the URL to include an API token instead of username and password?

You either need to enable basic auth under Settigns → API Security → Show Advanced → Allow Basic Authentication or Click on your user account (lower left corner) → and Create new API Token.

If you do the former you can use http://username@password… . If not you need to add the API token as a header property. There are tutorials on the forum for doing this.

I tried this but still get the 401

Just checking, is the http://username@password my OpenHAB login details?

Yes, and to access the part of the REST API you need to that login needs to be an admin user.

hmmm

Seems I have everything in order but it still doesn’t work.

we had this topic a few weeks ago:

This solution does what you are looking for.

Btw: It’s the second link if you search for “Shelly detached” :wink:

Thanks.

That is another slightly different approach again, although it still doesn’t answer my question about getting a response from the API about a specific Thing status to verify that the Shelly should stay in detached mode.

I think I prefer the event based mode to the looping time check.

I suppose I could query the deCONZ Zigbee hub as well as the OpenHAB URL, as it has it’s own IP address, but it’s still not as ‘definitely working’ as getting a Thing status response from the OH API.

You can use a simple rule triggered by Thing status changes and update an Item, right? That’s probably going to be way easier than mucking with the REST API form a rule.

You could also use executeCommandLine to interact with the karaf console.

If you have a lot of Things to map to Items, you can use Thing Status Reporting [4.0.0.0;4.9.9.9] to make handling that a little easier.

Finally, in JS Scripting and jRuby (perhaps others) you have access to the ThingRegistry. If you have the ThingUID (which you need to use the REST API too) you can get at the status directly.

if( things.getThing("shelly:my:thing").getStatus().toString() != "ONLINE" )

This processing and scripting is done on the Shelly, not OpenHAB.

But that does not tell you if openhab is running.

You have to loop anyway, either REST API or the html-page.

I think the use case is covered pretty well if you can make sure that openhab is running and can be reached.
I don’t think the Shelly has a full authentication in it’s espruino-implementation. The http-methods are a cut-down subset of node, don’t expect too much. But always willing to learn, so if you want to find out…

No it wouldn’t, it would be a secondary check. Both OH and the deCONZ need to be working for the Shelly to work in detached mode.

I don’t believe so. There is more than one version of the script on the HA post. The more recent one uses Shelly.addEventHandler(function (event) {…

Not true. If OH is running, but either the binding is offline or the zigbee hub itself is offline, the switch wouldn’t work.

This is exactly why I am enquiring about creating a URL link that doesn’t need anything more than a username and password.