Turn on lights on motion, turn off on next motion


#1

I recently picked up a Gen 2 Zooz motion sensor, and I would like to use it in a manner that’s different from my other sensors. I’m placing the sensor right inside the bathroom door, and I would like it to turn on the lights when it’s first triggered, and to turn off when it’s triggered next, or it’s been 5 minutes. Since this particular sensor fires every 15 seconds and I’m only checking for motion by the door, I think this should be possible.

I’ve only ever written simple pistons that checked for motion, and end of motion. How would you write a piston that counts initial motion, and turns off at the next motion? Anything I’m not considering?

The bathroom is an L-shape.


#2

Define
Boolean toggleVar

IF motion sensor’s motion IS active
THEN
IF toggleVar IS true
turn on light
set variable toggleVar = false
ELSE
turn off light
set variable toggleVar = true
end if
end if

IF light stays on for 5 minutes
THEN
turn off light
set variable toggleVar = true
end if