[SOLVED] How can i controll an item when this goes offline or online? And how do i loop a rule?

rule “Test2”
when

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?

Not clear what your Item is, a string perhaps?

That’s usually the wrong approach in OpenHAB. If you want something to happen when a change takes place, trigger the rule by the change.

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.

Ok thanks i tryed and i made a loop and it works great, but how can i detect if my relay is online or offline or is that not possible?

1 Like

Tell us more about your “relay”. How is it bound to OpenHAB?

Is in the items folder

//Relay

Switch RELAY “RELAY” (LR,gLight)
{mqtt=">[mosquitto:cmnd/sonoff1/POWER:command:*:default],
<[mosquitto:stat/sonoff1/POWER:state:default]"}

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?

Yes its tasmota i think it does it periodically not sure.

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

{“Time”:“2018-07-04T22:39:06”,“Uptime”:27,“Vcc”:3.144,“POWER”:“OFF”,“Wifi”:{“AP”:2,“SSId”:“Batatas_EXT 2.4GHz”,“RSSI”:58,“APMac”:“E8:DE:27:4D:CB:11”}}

Yep the devices send this stuff after a while

I will try that i am gonna install that binding.

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

So create an item:

String Sonof1Online "Sonof 1 online status" { mqtt="<[mosquitto:tele/sonoff1/LWT:state:default]" }

Stick it in your sitemap and then unplug your sonoff, then plug it back in.
You should see the item going to OFFLINE and the ONLINE

Then a rule:

rule "sonoff offline"
when
    Sonof1Online changed to "OFFLINE"
then
    // Do what you want
end
1 Like

Out of interest, how does the MQTT broker “know” the subject has died? I guess there is some kind of keep-alive TCP traffic.

The sonoff client instruct the broker to leave a LWT (last will and testament) when disconnected
It’s a very nice feature of MQTT

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.

That’s great, please like if you wnat and tick the solution mark. Thanks

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 :slight_smile:

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 :smile: