Using thing status in rules THEN clause

Hello everyone,

I have a rule that is triggered when thing get onlie or offline. It’s used for some shelly that like to go offline sometime and I want to be notified about it. The rule itself work really great. Problem is, sometime, they go offline for like 1 sec (due to wifi connectivity) and because of that, I get a notification that it’s offline. Is there a way to check for the thing status in the THEN clause? Like, my goal would be to put a global variable to true saying “I already fired” and to start a timer. When the timer end, if the thing is still offline, it will notify me, put the offline variable to true and put the “trigger” var to false else, do nothing and reset the trigger var. Here’s my current rule without the needed THEN clause.

rule "shelly_piscine_status"
when Thing "shelly:shellydevice:e8db84ace7fe" changed to OFFLINE
then
    shellyPiscineOnline.sendCommand(OFF)
    sendNotification("email","Perdu connexion piscine")
end

I can’t answer your question, but with things like this I generally use the Debounce rule template from the Markplace. Debounce [3.2.0;3.4.9]

You can decided how long to wait before the virtual debounced switch is triggered.

You’re overthinking it. You just need to have your ShellyPiscineOnline item change state to reflect the status of the thing. That’s two rules:

rule "shelly_piscine_status_offline"
when Thing "shelly:shellydevice:e8db84ace7fe" changed to OFFLINE
then
    shellyPiscineOnline.sendCommand(OFF)
end

rule "shelly_piscine_status_online"
when Thing "shelly:shellydevice:e8db84ace7fe" changed to ONLINE
then
    shellyPiscineOnline.sendCommand(ON)
end

Then it’s just the same as any other item, so you can set a timer when it changes to OFF and cancel the timer if it changes to ON within a few seconds.

@rpwong I already have these 2 rules. Problem here is I get the “offline notification” like 20 times an hours right now. Reason is the shelly device is far and either switch AP or just lose connection for a split second, resulting in the thing going offline then online, then offline then online. My goal is to reduce the number of rule trigger and make a “timeout” to say it’s really offline and not just flickering wifi (right now it sit between -70 and -80db which is very low). The reason for that is my device (the one I had before) used to really freeze. When this happen, I would lose control of the pool pump. At this time, this rule was enough to alert me that the pool is no longer controlled.

Now, the new device doesn’t freeze but because of poor wifi, openhab see the device offline upon his check because I have many timeout.

That’s why I want a timer that will validate if it’s really offline before sending the notification and putting the variable saying it’s offline. Unless there’s a way to stop the timer started in the offline rules from the online rules?

I think I found out the way I need it to work

rule "shelly_piscine_offline"
when Thing "shelly:shellydevice:e8db84ace7fe" changed to OFFLINE
then
    if (shellyPiscineOfflineTimerLoaded == false){
        var delayLoadTime = now.plusSeconds(5)
        shellyPiscineOfflineTimer = createTimer(delayLoadTime, [|
            shellyPiscineOnline.sendCommand(OFF)
            sendNotification("email","Perdu connexion piscine","pump","warning")
        ])
		shellyPiscineOfflineTimerLoaded = true
	}

end

rule "shelly_piscine_online"
when Thing "shelly:shellydevice:e8db84ace7fe" changed to ONLINE
then

    if (shellyPiscineOfflineTimer !== null) {
        shellyPiscineOfflineTimer.cancel()
    }
    shellyPiscineOfflineTimerLoaded = false
    if (shellyPiscineOnline == OFF){
        sendNotification("email","Connexion piscine rétabli","pump","info")
    }
    shellyPiscineOnline.sendCommand(ON)
    
end

this way, the timer is cancel if the online rule is fired and the timer is running. I haven’t tested it yet (well, haven’t got any event for now but it can be quiet sometime for hours). I also added a check in the online part where it will send notification only if it was offline. This will prevent multiple on/off trigger when what I want to know is real down signal. I’m trying 5 sec for now. Tomorrow, I’ll go flip the breaker to see if the rule properly fire.

If using the current stable version (OH3.4) you can use an implicit var:

// create global vars upwards of the first rule
var Timer shellyPiscineOfflineTimer = null                                       // global var for a timer

rule "shelly piscine thing status"
when
    Thing "shelly:shellydevice:e8db84ace7fe" changed                             // thing status has changed
then
    if(newThingStatus == OFFLINE && shellyPiscineOfflineTimer === null)          // is OFFLINE and no timer started yet
        shellyPiscineOfflineTimer = createTimer(now.plusSeconds(5), [|           // create timer
            sendNotification("email","Perdu connexion piscine","pump","warning")
        ]
    }
    if(newThingStatus == ONLINE) {                                               // is ONLINE
        if(shellyPiscineOfflineTimer.hasTerminated) {                            // timer already fired
            shellyPiscineOfflineTimer = null                                     // clear the var
            sendNotification("email","Connexion piscine rétabli","pump","info")
        } else {
            shellyPiscineOfflineTimer.cancel                                     // stop running timer
            shellyPiscineOfflineTimer = null                                     // clear the var
        }
    }
end
1 Like

Oh nice, didn’t knew about “newThingStatus” var, will make the code much cleaner.

THanks!

I’m getting this error in log:

2023-05-07 13:15:38.607 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'custom.rules' has errors, therefore ignoring it: [53,27]: no viable alternative at input 'OFFLINE'
[58,5]: missing ')' at '}'

Should I put “offline” and “online” in quotes?

edit: I fixed the missing ) and {, but still getting the OFFLINE and ONLINE error

edit: Ok, I put quotes around ONLINE and OFFLINE and it work. I did had to do some tweaking for it to properly worked, but you sent me on the right path. Thank you!

That’s because your notification triggered immediately in your offline rule. Hence:

That’s how the notification would be ignored.

It’s basically what Udo has shown you. I just used the item you had already created, instead of newThingStatus (which I didn’t know about).

Hm, maybe ('cause it’s a implicit var) it has to be a String

 if(newThingStatus == "OFFLINE" && shellyPiscineOfflineTimer === null) 

 if(newThingStatus == "ONLINE") { 

Speaking of rule templates, the Open Door rule template was made just for this specific case. Keep your rules that set the Switch item to ON/OFF. But then you can use a rule instantiated from the template to call a rule you write that sends the alert when the Switch remains OFF for a specified amount of time.

And with the template you have the option to repeat the alert until it comes back ON and define a do not disturb period (e.g. who wants an alert at midnight when nothing can be done about it until morning anyway.

The great thing about rule templates is all you have to write is the code that sends the alert. Someone else wrote the complicated timer stuff for you.

I guess I should check template. I didn’t even knew it existed, my openhab installation date from 2.0 so most rules are old stuff (and most have been copied over to nodered).