How to implement Variable for time delay (Check Network Status of Items)

I created some easy rules to get a Telegram Message, if a device is offline or online again. Now I noticed that some devices have shutdowns (only 1second) and go online again. How to use variables to send only when a device is longer than e.g. 5seconds offline? My rules are very simple because I just started to learn it. Perhaps anybody can show me how it would look like with an IF - Rule.

Thank you very much for your help!

rule "Heizungstemperaturen 192.168.188.32 ausgefallen"
    when    Item Heizungstemperaturen_Online changed to OFF
    then    sendTelegram("bot1", "Heizungstemperaturen 192.168.188.32 ausgefallen")
end
rule "Heizungstemperaturen 192.168.188.32 wieder online"
    when    Item Heizungstemperaturen_Online changed to ON
    then    sendTelegram("bot1", "Heizungstemperaturen 192.168.188.32 wieder online")
end
//-----------------------------------------------------------------

rule "Rollo Wohnzimmer 192.168.188.106 ausgefallen"
    when    Item Rollo_Wohnzimmer_Online changed to OFF
    then    sendTelegram("bot1", "Rollo Wohnzimmer 192.168.188.106 ausgefallen")
end
rule "Rollo Wohnzimmer 192.168.188.106 wieder online"
    when    Item Rollo_Wohnzimmer_Online changed to ON
    then    sendTelegram("bot1", "Rollo Wohnzimmer 192.168.188.106 wieder online")
end
//-----------------------------------------------------------------

Something along the lines of

var Timer HeizungTemps = null

rule "Heizungstemperaturen 192.168.188.32 ausgefallen"
    when    Item Heizungstemperaturen_Online changed to OFF
    then
        HeizungTemps?.cancel
        HeizungTemps = createTimer(now.plusMinutes(5))[|
            if (Heizungstemperaturen_Online.state == OFF){ // check if still OFF
                sendTelegram("bot1", "Heizungstemperaturen 192.168.188.32 ausgefallen")
            }
        ]
end

And then basically the same for the ON version. That idea you can basically copy for all devices you want to monitor like this. Just make sure that for every Item you define a new Timer variable.

Edit: I forgot to mention I just typed this out on my phone so there might be some syntax errors or alike that I’m missing from this small screen.

the createTimer is one way, but depending on what you want it to do I often use Thread::sleep(5000) in a rule. This just pauses the process, (in this case for 5 seconds).

It works perfectly for me when I’ve run a refresh or update command and it takes a second or two to register with whatever I’m updating…for example, one of my presence detection rules refreshes my iPhone location to see if I’m home but the system hasn’t updated it yet. I put the sleep function in a rule to make it wait for 10 seconds so everything else sorts itself out before continuing…

As @rlkoshak explained in his post: Why thread::sleep is a bad idea using a thread::sleep for more than 0,5 second is a bad idea.

Well, every day is a school day…thanks for that…I’m off to go and update my rules!

Thanks for the help. Good to know, because I also thought of using sleep.

I guess a version of this using a group could be applied to save writing this for every lan item to be monitored

Well that becomes tricky if device 1 changes to offline, and while waiting 5 seconds, device 2 changes (either to online or offline). To make sure it is robust, I’m afraid you cannot rely on “member of” kind of rules here. You basically will want to monitor each device individually.

And if you think there is a way, please let me know as I can’t think of one right now.

You could also consider using the expire binding as a simple mechanism.

If using createTimer methods, you would need to maintain a “group” of timers.
That can actually be done, by storing the timer references in a Map (“array”) keyed to Item names. For a Member of group triggered rule, that is available as triggeringItem.name

Example of that technique here

1 Like