Trouble with createTimer

Hi,

I have a bunch of rules like this, whose purpose is to notify me if I haven’t received any updates from the device for a given time. The problem is that I get a bunch of notifications, even though I can verify that the item is actually updated around every 30 seconds. I have not been able to see clear pattern for how often I get these notifications, but the point is that I shouldn’t get them at all. Every time the item is updated I see “resetting power timer” in the log, so it should work, but it doesn’t. Any ideas?

var Timer powerTimer

rule alert_power
when Item CurrentTotalPower received update
then
    if (powerTimer!=null) {
        powerTimer.cancel()
        logInfo("timers","resetting power timer")
	powerTimer=null
    }
    powerTimer  =createTimer(now.plusMinutes(20), [|
        sendNotification("frankose@ifi.uio.no","Stopped receiving power reports")
        logInfo("power","power")	
    ])
end

var Timer powerTimer

?

Sorry, that was actually what I have in my rule. I just forgot to write it in my post :-/ Updated my post to reflect this.

I do not see anything obviously wrong with this but there are a couple of other approaches you could try:

#Reschedule

var Timer powerTimer

rule "Alert Power"
when
    Item CurrentTotalPower received update
then
    if(powerTimer!=null && !powerTimer.hasTerminated) {
        logInfo("timers", "resetting power timer")
        powerTimer.reschedule(now.plusMinutes(20))
    }
    else {
        powerTimer = createTimer(now.plusMinutes(20), [|
            sendNotification("frankose@ifi.uio.no","Stopped receiving power reports")
            logInfo("power", "frankose@ifi.uio.no","Stopped receiving power reports")
        ]
    }
end

#Cron

// Assumes persistence
rule "Alert Power"
when
    Time cron "0 0/1 * * * ?"
then
    if(CurrentTotalPower.lastUpdate.before(now.minutsMinutes(20)) {
        sendNotification("frankose@ifi.uio.no","Stopped receiving power reports")
        logInfo("power", "frankose@ifi.uio.no","Stopped receiving power reports")
    }
end
1 Like