Problems Cancelling Timer

Hi There!

I’m trying to figure out an issue where I create a timer, and the timer works, however I can’t seem to cancel the timer. Any thoughts on what I’m doing wrong?

Timer Code (Which works):
var lightsOutRand = (new java.util.Random).nextInt(20) var Timer timerBedtime = createTimer(now.plusMinutes(lightsOutRand)) [| sendCommand(LampLivingRoom, OFF) sendCommand(LampBedRoom, OFF) timerBedtime = null ]

Cancel Timer Code (Doesn’t work, no errors):
if(timerBedtime != null){ timerBedtime.cancel timerBedtime = null }

Thanks!

Are you declaring your timer outside of the rule, i.e. as a global variable?

It’s a different rule, but within the same .rules file.

yep - so the cancel code has no access to the timer created in the first rule, since the timer is local to that first rule.

Ah, I see what you’re saying. So the solution would be to declare the timer prior to using it in any rules?

var Timer timerBedtime = null

Exactly :wink:

Excellent! I’ll give it a go. Thanks!