Motion Sensor: Timer reschedule rule question

I have created a situation where a regular motion detector sensor triggers the input of a Shelly1 device which is configured in Detached Switch Mode. I successfully followed the rule in the design pattern for motion detection, which is a good start. I have 2 issues:

  1. When motion continues, the state of the motion detector remains ON, without changing. The result is, that 30 seconds after the first motion is detected, the lights switch OFF.
  2. When motion stops, the sensor will report Shelly1MotionTuin192168133_Relay_Input to be OFF, but if motion starts again within the 30 second timout timer, I would like the timer to reset as well so the light remains on. Is this posible?

I tried to invert the rule below to look at the OFF state of the Shelly input. This solves the first point, but not the rescheduling of the timer at point 2.

My rule is:

var Timer MotionTuinTimer = null

rule "Shelly1MotionTuin192168133_Relay_Input received ON"
when
    Item Shelly1MotionTuin192168133_Relay_Input received update ON
then
	if (Buiten_Donker.state == ON && Away_Mode_Enabled.state == ON && Dummy_schakelaar_tuinverlichting.state == OFF) {
	ZWaveNode014TuinverlichtingGarage_Switch1.sendCommand(ON)  \\Garden Lights
			if(MotionTuinTimer === null || MotionTuinTimer.hasTerminated()) {
				MotionTuinTimer = createTimer(now.plusSeconds(30), [|
					ZWaveNode014TuinverlichtingGarage_Switch1.sendCommand(OFF)
					MotionTuinTimer = null
					])
				}
			else {
			MotionTuinTimer.reschedule(now.plusSeconds(timeoutSeconds ))
			}
	}
end

Just separate the tasks out.

When motion changes to on, turn on the light.
Also look to see if there is a timer running, and cancel it.

When motion changes to off, start the timer.

When timer expires, turn off the light.

Thanks for your response @rossko57. Since I use Openhab for over 1,5 years now and I know that you are the guru on this forum, are you willing to help me with the rule?

The inverted rue I currently use is the following, but I can’t seem to find a way with my knowledge to cancel or reschedule the timer when new motion is detected.

var Timer MotionTuinTimer = null

rule "Shelly1MotionTuin192168133_Relay_Input received OFF"
when
    Item Shelly1MotionTuin192168133_Relay_Input received update OFF
then
	if (Buiten_Donker.state == ON && Away_Mode_Enabled.state == ON && Dummy_schakelaar_tuinverlichting.state == OFF) {
	Thread::sleep(10000)
		if (Shelly1MotionTuin192168133_Relay_Input.state == OFF) {
			if(MotionTuinTimer === null || MotionTuinTimer.hasTerminated()) {
				MotionTuinTimer = createTimer(now.plusSeconds(30), [|
				ZWaveNode014TuinverlichtingGarage_Switch1.sendCommand(OFF)
				MotionTuinTimer = null
				])
			}
			else {
			MotionTuinTimer.reschedule(now.plusSeconds(timeoutSeconds ))
			}
		}
}
end

rule "Shelly1MotionTuin192168133_Relay_Input received ON"
when
    Item Shelly1MotionTuin192168133_Relay_Input received update ON
then
	if (Buiten_Donker.state == ON && Away_Mode_Enabled.state == ON && Dummy_schakelaar_tuinverlichting.state == OFF) {
	ZWaveNode014TuinverlichtingGarage_Switch1.sendCommand(ON)
		}
end

I think I got it. I ended up with the rule below. Thanks for your help.

var Timer MotionTuinTimer = null

rule "Shelly1MotionTuin192168133_Relay_Input received OFF"
when
    Item Shelly1MotionTuin192168133_Relay_Input received update OFF
then
	if (Buiten_Donker.state == ON && Away_Mode_Enabled.state == ON && Dummy_schakelaar_tuinverlichting.state == OFF) {
		if(MotionTuinTimer === null || MotionTuinTimer.hasTerminated()) {
			MotionTuinTimer = createTimer(now.plusSeconds(30), [|
			ZWaveNode014TuinverlichtingGarage_Switch1.sendCommand(OFF)
			MotionTuinTimer = null
			])
		}
		else {
		MotionTuinTimer.reschedule(now.plusSeconds(timeoutSeconds ))
		}
	}
end

rule "Shelly1MotionTuin192168133_Relay_Input received ON"
when
    Item Shelly1MotionTuin192168133_Relay_Input received update ON
then
	if (Buiten_Donker.state == ON && Away_Mode_Enabled.state == ON && Dummy_schakelaar_tuinverlichting.state == OFF) {
	ZWaveNode014TuinverlichtingGarage_Switch1.sendCommand(ON)
		if(MotionTuinTimer !== null) {
				logInfo("MotionTuinTimer","Canceling the Timer")
				MotionTuinTimer.cancel()
				MotionTuinTimer = null
		}
	}
end
1 Like