See Design Pattern: Looping Timers. Take a timestamp then create a looping timer. In the timer send the command and then check the timestamp. If five minutes haven’t passed, reschedule the timer for another 30 seconds. If it has set the timer variable to null.
I now have this rule, but it doesn’t work. Can you tell me what’s wrong?
var Timer ZigbeeTimer = now
rule "Light 3 min. ON"
when
Item ZigbeeSensorOneOccupancy changed from "true" to "false"
then
if(ZigbeeTimer !== null) return;
ZigbeeTimer = createTimer(now, [ |
if(ZigbeeTimer < now.plusMinutes(3))
{
Badkamer_lmp_Brightness.sendCommand(80)
ZigbeeTimer.reschedule(now.plusSeconds(30))
}
else
{
if(ZigbeeTimer > now.plusMinutes(3) || ZigbeeSensorOneOccupancy.state == "false")
{ Badkamer_lmp_Brightness.sendCommand(0) }
ZigbeeTimer = null
}
])
end
Within the 3 minutes of the state change of the sensor from ‘true’ to false, it will send a command to the light to keep it ON (80).
After the 3 minutes it checks to see if the sensor state is ‘false’ and then send a command to put the light OFF (0).
This will probably not work. I guess now is some kind of DateTime like type, so you can’t assign it to a Timer type value.
Even if it would work, then if(ZigbeeTimer !== null) return; would always return since ZigbeeTimer is no longer null because you already assigned it a value.
What you really want is a light that stays on a certain amount of time after the last sensor reading. This is a common problem and well solved problem. For one example see Design Pattern: Motion Sensor Timer.
This approach is very similar to what you have. Perhaps comparing them will reveal the problem. I don’t see anything besides the problem Marcel pointed out. You should be seeing errors on that line from VSCode and in openhab.log on that line.