[SOLVED] createTimer help to prevent rule triggering again and again for a period of time


(eviltechmonkey) #1

This is what I have:

rule “phone1 online”
when
Item phone1_Online changed to ON
then
MyEchoDot_Speak.sendCommand(‘Good day master. I am ready to do your bidding’)
end

and I’m trying to keep it from triggering again for a set amount of time. I tried to do:

MyEchoDot_Speak.sendCommand(‘Good day master. I am ready to do your bidding’)
createTimer(now.plusMinutes(600))

but that just spits out the occasional error in the console and obviously doesn’t keep it from firing again. Any pointers appreciated.


(Rich Koshak) #2

createTimer is used to schedule a bit of code to run at sometime in the future.

You can’t prevent a Rule from running when an event occurs. So you need to use if statements to decide what code the Rule runs when it is triggered.

There are lots of ways to do this but createTimer isn’t one of them. Using a timestamp is probable the safest.

var lastRun = now.minusMinutes(601) // set to the past

rule "phone1 online"
when
    Item phone1_Online changed to ON
then
    if(now.minusMinutes(600).isAfter(lastRun)) return; // if it has been less than 600 minutes since we last ran exit

    MyEchoDot_Speak.sendCommand('Good day master. I am ready to do your bidding')
    lastRun = now
end

NOTE the use of How to use code fences


(eviltechmonkey) #3

Awesome! Thank you. Thought I might need a variable but didn’t know how to implement. Hopefully it helps others!


(Vincent Regaud) #4

You could also have used another switch with the expire binding

Switch timerSwitch { expire="10h, OFF" }

The rule

rule "phone1 online"
when
    Item phone1_Online changed to ON
then
    if (timerSwitch.state == ON) return; // The timer is already running so we do nothing
    MyEchoDot_Speak.sendCommand('Good day master. I am ready to do your bidding')
    timerSwitch.sendCommand(ON) // start the timer
end

(eviltechmonkey) #5

That’s a better option. Seems the first one with “var lastRun” stopped ALL my rules from running. Thank you.


(Rich Koshak) #6

Unless you after using the var in all off your rules either there is a syntax error so the .rules file never loaded or something else is going wrong. There is nothing about that rule that can cause that to happen.


(eviltechmonkey) #7

I will play around with it some more then. Needs fine tuning.