Page 1 of 1

Motion and light timer

Posted: Friday 21 April 2017 14:44
by chrispazz
I have a script (blocky currently) that triggers a lamp on when motion is detected.
In the blocky I have "turn on lamp for 5 minutes".

What I need is that if someone cause again a motion within that 5 minutes, the timer should set again to 5 minutes from now, ignoring the previously cosumed minutes.....

In other words, I should have a timer that refresh the 5 minutes everytime someone cause motion and the light should remain ON all the time the timer is not expired....

How can I do this in LUA?

Thank you

Re: Motion and light timer

Posted: Friday 21 April 2017 18:31
by DanM
I do this by having a timer variable for each room. All you then need is a timer script that runs every minute: this simply gets the variable and deducts one. If the new value is zero it turns off the light.

For setting the initial timer you can either track when the light it switched on via LUA, or you can use the 'on action' for the switch. I'd share my code directly but my actual setup is a lot more complex than this so wouldn't work for others.

Re: Motion and light timer

Posted: Saturday 22 April 2017 7:50
by mrf68
I do this by checking the "otherdevices_lastupdate[sensor]". When the sensor is Off and more than 5 minutes have passed since the last update, it turns off the light. This script runs every minute. With only this it would turn the light off every minute after reaching the 5 minute limit. You can bypass that problem by adding to the statement "more than 5 minutes" : "but no more than 6 minutes".

Re: Motion and light timer

Posted: Saturday 22 April 2017 8:26
by Egregius
mrf68 wrote:You can bypass that problem by adding to the statement "more than 5 minutes" : "but no more than 6 minutes".
Why not just check the state of the light at that moment?
If pir = off and lasttime > 300 and light = on do switch light off.

Re: Motion and light timer

Posted: Saturday 22 April 2017 9:53
by mrf68
Egregius wrote:
mrf68 wrote:You can bypass that problem by adding to the statement "more than 5 minutes" : "but no more than 6 minutes".
Why not just check the state of the light at that moment?
If pir = off and lasttime > 300 and light = on do switch light off.
And so we learn something every day. Thanks, Egregius. I can optimize my code now. ;)

Re: Motion and light timer

Posted: Thursday 27 April 2017 11:45
by chrispazz
Thank you for all answers.

Now I am working to distinguish when the light is switched on by a motion sensor (and it should switch off with a timer) and when it is switched on by an explicit action (and it should remain switched on until someone switch it off)....