Item SETUP2 changed to OFF
then if (LED1.state == OFFLINE) {{
SETUP.sendCommand(OFF)}
createTimer(now.plusSeconds(5)) [
LEDSETUP1.sendCommand(OFF)]}
else if (LED1.state == ONLINE) {
SETUP3.sendCommand(OFF)}
end
That rule is basically what i want but is there a way to test if an item is online or offline?
rule “Test3”
when
Item SETUP3 changed to OFF
then if (LED1.state == ONLINE)
createTimer(now.plusSeconds(5)) [
LOOP ]} (loop until it goes offline)
else if (LED1.state == OFFLINE) {
SETUP.sendCommand(OFF)}
end
And how can i loop a rule so its always trying and when the result changed from online to offline it sends the command to the device i want?
Your rule triggers each time the item SETUP2 changed to OFF, if I’m not mistaken that behaviour is exactly what you are referring to by " loop a rule so its always trying…".
Such behaviour is called event-based programming.
Okay, presumably it’s a Sonoff device with maybe Tasmota software, communicating over MQTT.
I don’t know much about that, but if you want to detect the device going offline or unavailable, you would need it to be reporting in to OpenHAB regularly - so that you can look for the reports stopping. Does Tasmota do updates periodically, or only when the relay changes?
If it does periodically update your Item, you can use the expire binding to set the Item to NULL if no update comes in for a time. Then trigger a rule from the change to NULL
You’ll probably want also to set autoupdate=“false” for that Item, so that it’s state doesn’t get updated automatically by rules or UI trying to send commands to it.
When a Tasmota item goes offline (Disconnects with the wifi) the mqtt broker generates a Last Will and Testament message on the topic: tele/sonoff1/LWT
What… you are right it actually does it holy potatoes thats awesome the rule i have created actually works with the offline and even tested it with online commands just needed to create that string very nice thanks a lot man.
Yes, I understood the Sonoff lodges a LWT with MQTT broker. I was wondering what triggers the MQTT broker to execute the LWT (apart from an obvious comms error).
Googling around it seems there is a keep-alive watch kept by MQTT broker, so detecting e.g. WiFi failure, as well as actual Sonoff failure. The keep-alive is actioned by the Sonoff client, I guess there’s a configurable timeout in there somewhere.
Note: this LWT method won’t detect any problems between OpenHAB and MQTT, or the broker itself failing. I’d expect there’d be other clues about that, though
If you really needed checks on that as well, the periodic update + expire combination would be a way to do it within OpenHAB. An obvious way to do that would be to combine both methods - add expire=“5m,state=OFFLINE” or something like to the mqtt online Item you already described.
Yes that’s a possibility.
I have had my mosquitto running on a standalone pizero now for 236 days without failure and the same goes for wifi apart for a couple of resets of the router I did myself.
The UPS helped with a couple of brief power cuts too