Sending HTTP commands on OH shutdown / changing detached Shelly modes on OH shutdown

I hope this topic fits this category.
I’m using some Shelly devices in detached mode behind wall switches to have some special actions when switches are pressed two times in a row and so on. The lights are switched on/off via rules in Openhab. So far so good.

When my Openhab server is down (for whatever reason) I’d like to change the shellies back to “Edge” button mode. This can be achieved by sending http commands to the Shellys.
e.g. for a Shelly Dimmer:

 sendHttpGetRequest("http://" + IP_ADDRESS_SHELLY + "/settings/light/0?btn_type=edge") 

Now my question:
What would you suggest to run these commands?

I thought of:

  • finding a way to have a Openhab Server shutdown rule
  • writing a script and execute it on a new Shelly plus to ping my server and if the server is unavailable send http commands to all the detached shellys. (I didn’t write/ test this yet)

I don’t want to setup a 2nd server. Hope to get your ideas, thx

There’s no built-in trigger to start a rule on system shutdown, because the rule engine gets killed pretty quickly. If you’re shutting down with an openHAB rule, then you can add the HTTP command to it (before your shutdown/reboot command) and set up a separate rule that runs on startup to revert.

Of course, this only works if you intentionally shut down your server, and not if it crashes or loses power.

I can’t help you with this specific idea, but really what you’re talking about is monitoring openHAB. You can find discussions about that in the community, such as this one. They’re likely more than you want/need, but you might get some ideas.

Good luck!

Use the systemd functionality and define your script to be executed in dependency of the openhab.service.
See manual: systemd.service

This requires e.g. bash script instead of an OH rule.

2 Likes

We use to have a system shut down trigger

1 Like

I’d forgotten about that. I seem to think that it was removed due to being unreliable?

EDIT: Yep, unreliable. Here’s a relevant discussion from a couple of years ago.

1 Like

thanks for all your advice.
I implemented a rule on one of my Shelly Plus - should someone ever have the same need as I:

function checkServerHealth(){
  Shelly.call("HTTP.GET", {url: "MY_SERVER_IP"}, processHttpResponse);
};
function resetDetachedShellys(){
  Shelly.call("HTTP.GET", {url: "SHELLY_1_IP/settings/relay/0?btn_type=edge"}, null);
  Shelly.call("HTTP.GET", {url: "SHELLY_DIMMER_IP/settings/light/0?btn_type=edge"}, null);
  MQTT.publish("my/topic/watchdog", JSON.stringify("reset of shelly IPs: X and Y to edge"), 0, false);
};

Timer.set(
  /* number of miliseconds */ 60000,
  /* repeat? */ true,
  /* callback */ checkServerHealth
);
function processHttpResponse(result, error_code, error) {
  if (error_code === 0 && result.body !== undefined && result.body.indexOf("html") > 0) {
      print("alright");
      
      MQTT.publish("my/mqtt/watchdog", JSON.stringify("server check succesful"), 0, false);
      
  } else {
      resetDetachedShellys()
  }
}



1 Like