Openhab2 rule advise please

Dear Community,

i have an issue with my rule. i want my frontdoor to be locked 30 min after absensce has been detected, but if within those 30 min of absense detection the absense detection is back to ON, the rule should not be executed, the 30 min counter being stopped, and the frontdoor not being locked. It seems this part of the rule does not work as i was expecting. When the absense detection is off ( or my iphone is not detected for 2 min ), the counter starts and does not stop after my iphone logs in again.
Could you please advise on possible changes to the script and counter abort so that my rule works as expected? Thanks so much

rule "Lock door in absence"
when
    Item Iphone_Sw changed from ON to OFF 
then	
	if (HaustuerAlarmonoff.state == OFF) return false {
	createTimer(now.plusMinutes(30)) [|
			if (Iphone_Sw.state == OFF ) 
		{ 	var SimpleDateFormat df = new SimpleDateFormat( "HH:mm, dd.MM.YY " )
			var String Timestamp = df.format( new Date() )			
    		HaustuerMEQxxxxxxx_1_State.sendCommand(OFF)
			sendTelegram("xyzbot", "Die Haustuer wurde um " + Timestamp.toString() + "abgeschlossen, da niemand zu Hause ist!")
			logInfo( "------Haustuer automatisch verschlossen----", Timestamp )   
     } ] }
end
10:20:02.569 [ItemStateChangedEvent     ] - Iphone_Sw changed from ON to OFF
10:22:54.173 [ItemStateChangedEvent     ] - Iphone_Sw changed from OFF to ON
...
10:50:07.677 [ItemStateChangedEvent     ] - HaustuerMEQxxxxxx_1_State changed from ON to OFF

Hi Jan,

I think the following should work:

var Timer haustuerTimer = null

rule "Lock door in absence"
when
    Item Iphone_Sw changed from ON to OFF 
then	
	if (HaustuerAlarmonoff.state == OFF) return false

	haustuerTimer = createTimer(now.plusMinutes(30)) [|
		if (Iphone_Sw.state == OFF) 
		{
		 	var SimpleDateFormat df = new SimpleDateFormat( "HH:mm, dd.MM.YY " )
			var String Timestamp = df.format( new Date() )			
	    		HaustuerMEQxxxxxxx_1_State.sendCommand(OFF)
			sendTelegram("xyzbot", "Die Haustuer wurde um " + Timestamp.toString() + "abgeschlossen, da niemand zu Hause ist!")
			logInfo( "------Haustuer automatisch verschlossen----", Timestamp )   
     		}
		haustuerTimer = null
	 ]
end

rule "Cancel lock door in absence"
when
    Item Iphone_Sw changed from OFF to ON 
then	
	if (haustuerTimer != null)
	{
		var SimpleDateFormat df = new SimpleDateFormat( "HH:mm, dd.MM.YY " )
		var String Timestamp = df.format( new Date()
		logInfo( "------Abbrechen Haustuer automatisch verschließen----", Timestamp )
		haustuerTimer.cancel
		haustuerTimer = null
	}
end

This creates a global variable to keep track of the timer. Then when presence is detected again, the timer is cancelled.

Hi Marcel,

thanks so much for your suggestion, i give it a try and let you know.

Jan

Seem to work perfectly fine! Thanks so much Marcel

1 Like