Need advice to simplify rule (extend Xiaomi motion sensor timeout)

Hello!

I purchased the Xiaomi Smart Home kit. Works great but the motion sensors have a 2 min timeout only (can’t be changed) that I would like to extend to say 5 min for example. They only trigger motion to ON every 2 min so, between this time, I need something that tells me that motion is active to reset my new timer (MotionTimer).

These rules work, but it would be nice to have these two rules into one.

rule “Motion Sensor extends timeout”
when
Item MotionSensor3 changed
then
if (MotionSensor3.state == ON && MotionTimer.state == OFF) {
sonoff08.sendCommand(ON)
}
while (MotionSensor3.state == ON) {
MotionTimer.sendCommand(ON)
Thread::sleep(5000)
}
end

I need to add this rule to turn the sonoff08 OFF which seems unnecessary.

rule “Turn of Sonoff wallplug”
when
Item MotionTimer changed to OFF
then
sonoff08.sendCommand(OFF)
end

The MotionTimer has a Expire tag on it (this is where I set the new timer).

There might be a better and more complicated way to write this type of rule but I’m really a beginner when it comes to coding.

Any help would be much appreciated!

Thanks in advance!

//Victor

Your using Expire so try this rule.

rule “Motion Sensor extends timeout”
when
Item MotionSensor3 changed
then
if (MotionSensor3.state == ON && MotionTimer.state == OFF) {
MotionTimer.postUpdate(OFF) // cancel the timer if needed
sonoff08.sendCommand(ON)
}
else if(MotionSensor3.state != OFF && MotionTimer.state != ON){
MotionTimer.sendCommand(ON) // start the timer
}
end

rule "Turn off wallplug"
when
Item MotionTimer received command OFF
then
sonoff08.sendCommand(OFF)
end

@Victor3D
Did that solve your problem?

H102
Your using Expire so try this rule.

Thanks! But it didn’t work as I expected.

vzorglub
Did that solve your problem?

No, but I made it even simpler now. Since the Motion Sensors only trigger ON every 2 min when there’s motion. I added a expire timer on the sonoff08 (wallplug for light) directly. It’s a 5 min expire timer so when it detects motion, every 2 min it will reset the expire timer, and it starts to count down from 5 min again. This works for me, I found out today that when an item with a expire timer gets triggered, the timer starts over.

With motion sensors that is usually desired behavior. When you are in a room and moving around, you don’t want the light to turn off until X minutes after the last motion detection.

Please don’t use Thread::sleep. And using a Thread::sleep in a while loop is just asking for trouble. Please see (OH 1.x and OH 2.x Rules DSL only] Why have my Rules stopped running? Why Thread::sleep is a bad idea.

I can absolutely see this Rule with the way the motion sensor works totally killing your Rules.

I know you found a solution but as a beginner, I want to warn you against this sort of coding practice in the future. It will cause problems.

And I always recommend, if you can implement something like this at the device, that is the best place to implement it.

I started to consider how to implement this in Rules but it isn’t all that straight forward and it isn’t clear what you are trying to accomplish by extending the motion timer to 5 minutes? Are you after the standard behavior most users are after where the motion sensor keeps a light on for 5 minutes after the last motion detection?

Or do you have some more unique requirement?

I ask because to achieve the 5 minutes after the last motion detection behavior it frankly doesn’t matter that the MotionSensor3 resets in 2 minutes. You want that Timer to get rescheduled every 2 minutes.

rlkoshak
Are you after the standard behavior most users are after where the motion sensor keeps a light on for 5 minutes after the last motion detection?

Thank you for your input.
Yes, that is correct!
I remade the rule and I’m now using a expire timer on an item called sonoff01expire. The rules now look like this:

rule "Turn ON sonoff01expire"
when 
    Item MotionSensor1_MotionStatus changed or
	Item MotionSensor2_MotionStatus changed
then	
    if (MotionSensor1_MotionStatus.state == ON || MotionSensor2_MotionStatus.state == ON)	{
	sonoff01expire.sendCommand(ON)			//expire="5m,state=OFF"
		}
end	


rule "Turn ON sink light"
when 
    Item sonoff01expire changed
then	
	if (sonoff01expire.state == ON)		{
	sonoff01.sendCommand(ON)
	}
	else if (sonoff01expire.state == OFF)	{
	sonoff01.sendCommand(OFF)
	}
end

I tried to add a expire timer directly on the sonoff01 item but couldn’t get the sonoff01 switch to physically trigger, it just turned ON/OFF in the log/basic UI. Like this:

Switch		sonoff01		"Sink"		<poweroutlet>		["Switchable"]		{ mqtt=">[broker:cmnd/sonoff01/POWER:command:*:default],		<[broker:stat/sonoff01/POWER:state:default]", 		expire="5m,state=OFF" }

So I added the sonoff01expire to be the middleman between motion sensors being triggered and the sonoff01 switch. Maybe it’s not the best way but yeah, it works for me.

Set the expire of 5 minutes to the sonoff wallplug. Every motion sends the ON command to the wallplug. This will effectively turn off the wallplug 5 minutes after the last motion.

That is how I use it.

expire="5m,command=OFF"

Ahaaa, that works!
Thank you so much! Really appreciate it.