[SOLVED] Trying to avoid "debounce" in a rule

All of this seems really complicated for the stated problem.

Let’s assume Expire binding. Create a new Item Switch Room_Timer {expire="10m,command=OFF"}

Then the Rules are simply:

rule "rooby wifi status changed"
when
    Item RooWifi_Roomba871_Status changed
then
    if(previousState == NULL) return; // ignore changes from NULL

    // Roomba is online
    if(RooWifi_Roomba871_Status.state == ON) {
        // If we have a Timer running it means Roomba is back online after being offline for less than 10 minutes
        // cancel the timer
        if(Roomba_Timer.state == ON)  Roomba_Timer.postUpdate(OFF) 

        // If the Timer isn't running it means the Roomba has been offline for at least 10 minutes and an alert email was sent
        // send a new alert informing that the roomba is back online
        else {
            var String tmp = RooWifi_Roomba871_LastSeen.state.format("%1$td %1$tb %1$tl:%1$tM %1$tp")
            var message = "Description: rooby is back online starting from " + tmp
            sendMail("<myemailaddress>", "SOLVED - rooby is now back online", message)
        }
    }

    // Roomba is offline, set a timer
    else if(RooWifi_Roomba871_Status.state == OFF) Roomba_Timer.sendCommand(ON) // start/reschedule the timer
end

rule "rooby offline"
when
    Item Roomba_Timer received command OFF
then
    // Rule only triggers when Roomba has been offline for 10 minutes
    var String tmp = RooWifi_Roomba871_LastSeen.state.format("%1$td %1$tb %1$tl:%1$tM %1$tp")
    var message = "Description: rooby is unreachable starting from " + tmp
    sendMail("<myemailaddress>", "ATTENTION - rooby is unreachable", message)
end

This is an implementation of [Deprecated] Design Pattern: Motion Sensor Timer and is basically the Design Pattern: Expire Binding Based Timers implementation of Sebastian’s solution in post 7.

The above code will send the email only 10 minutes after the last time that the Roomba goes offline. Only if the Roomba has been offline for 10 minutes or more and an email was sent will a new email be sent when it comes back online.

Steve’s solution in post 19 is a good approach to implement this without the need for persistence. Instead of relying on lastUpdate and database queries. I’d recommend that over the persistence approaches. Those approaches end up being just too complicated to solve this particular problem. But I really do think that Timers is the best approach for this.

1 Like

@rikoshak beat me to it with an easier solution :slight_smile:
I forgot about the expire binding despite using it myself

2018-07-31 09:10:48.710 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'rooby wifi status changed': The name 'NULL' cannot be resolved to an item or type; line 77, column 25, length 4

it’s referring to

if(previousState == NULL) return;

edit: please ignore. After second reboot the log disappeared. If I well understood, it’s a known issue

THANK YOU all for your support!!! :slight_smile: