[SOLVED] Keep having trouble with a rule containing a timer

I have a rule that supposed to send me a mail when a door stays open too long(testing it with 2 min). It looks like this:

var Timer garageTimer
rule "Garagedeur telang open"
when
  Item Garage_Door changed to OPEN or
  Item Garage_Door changed to CLOSED
then
logInfo("Garagedeur", "Garagedeur "+Garage_Door.state)
  if (garageTimer != null){
      garageTimer.cancel
      garageTimer=null
}
  if (Garage_Door.state == OPEN){
	
	garageTimer=createTimer(now.plusMinutes(2)) [ |	sendMail("xxxxx@gmail.com","PASOP deur open","Deur is "+Garage_Door.state) ]
}
end

However, it seems to do nothing.
Now this rule has a bit of a history: months ago it ran perfectly, but then suddenly it would only run for 2 days after starting openhab and then no longer.
I then ggot the advise that it must be a damaged SD card on my raspberry 3 and that I should reinstall everything. Took a while, but I made a fresh install…everything OK except this rule not doing anything.
I did install the mail binding and configured my mail.cfg and I tested I can send mail from a rule, So what is the problem with this one, it used to work, have I forgotten something?

Any advice is appreciated

Thought I had a solution but realized the rule wouldn’t cancel the message if the door was closed.:thinking:

appreciated. It is OK if the message is cancelled before the length of the timer is passed.
The odd thing is that once it worked

I was just looking at one of Rich’s examples for canceling a timer in a rule. Here’s the link:
https://community.openhab.org/t/how-to-createtimer-and-timer-cancel-when-item-change-state/10781

I put your rule in VSCode and it complained about the != when null is an argument.

if (garageTimer != null) should be replaced with
if(garageTimer !== null)

Thanx, Seemed to solve it. I am baffled as it was just the same old rule that had worked before.
In the mean time I had found a method that works with expire, though it acts a bit different (repeats messages).Design Pattern: Recursive Timers

1 Like

@Kees_van_Gelder I changed the items to work with my setup using this rule.

var Timer garageTimer
rule "Garagedeur telang open"
when
  Item CouchLight changed to ON or
  Item CouchLight changed to OFF
then
logInfo("Garagedeur", "Garagedeur "+CouchLight.state)
  if (garageTimer !== null){
      garageTimer.cancel
      garageTimer=null
}
  if (CouchLight.state == ON){
	
	garageTimer=createTimer(now.plusMinutes(2)) [ |	
	OfficeLight.sendCommand(ON) ]
}
end

Everything worked:

BTW, VSCode didn’t like the sendMail part of the rule.

yes, VScode and also the Eclipse editor give warnings about the sendmail, yet as far as I know it is correct.

You do realise that months from now you are going to wonder why you called the timer ‘Garage’ for your couch light :wink:

:laughing:Yea I need to setup a spare OH to test on, I’ve got tons of weird entries in my logs. Just glad you got it working.

1 Like

This could be rewritten as:

    garageTimer?.cancel
    garageTimer = null

The ? says only call .cancel if garageTimer isn’t null.

1 Like

Thanks Rich, I will give that a try, eventhough it all seems to work now