Simple counter question


I am (slowly) making progress on setting up a security alarm system. I would like openhab to send me a notification when my wife and I are not home and we forgot to turn the alarm on, we can then turn it on via the sitemap.
I have come up with this but there seems to be an error in how I set up the counter I think.
I originally had it set up without the local counter and of course (which I didn’t think of when writing it) each minute it sent us a notification, frowned upon by my wife!

rule "GW & NW not home but alarm off"
	Time cron "1 * * * * ?"
var Number counter
var Timer nttimer = null
	if(PresenceGW.state==OFF && PresenceNW.state==OFF && AlarmON.state!=ON && counter < 3 ) {
		sendMail("", "No one home, set alarm?", "")
		sendBroadcastNotification("No one home, set alarm?")
		counter = counter + 1	
		nttimer = createTimer(now.plusMinutes(90)) [|
		counter = 0	

Could someone point me in the right direction with the counter or timer.

Kind Regards,

Two things:

var Number counter

If this were above the rule, not in the body, it would continue to hold its value across rule triggers. As it is currently in the then body of the rule, it comes into existence only when the rule is run. Also, it ought to be assigned an initial value, like

var Number counter = 0

Same advice applies to nttimer.

Also, if you instead triggered on

  Item PresenceGW changed to OFF or
  Item PresenceNW changed to OFF

then you would get the notifications once, at the time they happened, and there would be no risk of multiple notifications. In which case counter and nttimer would be unneeded.



I was keen for a reminder notification only if both of us were not present. Would the or function in the rule trigger do that? Can you have an ‘and’ in the rule trigger?


Your “if” in the body of the rule should continue to check if both are not present. The “or” in the “when” clause will trigger the rule when either presence item changes to OFF, but the “if” in the body will only process if both presence switches are OFF.

There is no “and” in the “when” clause because that section only defines what events would cause the rule to trigger. It doesn’t examine existing state – only events over time.

Ah of course. Thanks for that, much appreciated.