Page 1 of 1

.afterMin not retriggerable?

Posted: Sunday 31 December 2017 14:18
by IanDury
I wrote the following script to switch on a light for a duration of 2 minutes when the motion detector switches on:

Code: Select all

return {
    active = true,
    on = {
        devices = {'Motion Kamer'}
    },
    execute = function(domoticz, device)
        if device.state == 'On' then
                -- domoticz.devices('Lamp Kamer').setState(device.state)
                domoticz.devices('Lamp Kamer').switchOn().forMin(2)
        end
    end
}
This works fine in case the motion detector switches on only once every 2+ minutes. However if a new motion is detected just once within 2 minutes the lamp will never switch off again although no further motion is detected. This could be prevented by using the .checkFirst() method but what I want is that the Lamp "On" time gets extended with 2 minutes each time the motion detector is switched on again. I would expect that the .forMin timer would restart like a retriggerable one-shot. In some earlier posts I read that cancelling a queued timer is not yet implemented with an API in dzVents but any new command to the switch should cancel the timer. I tried this specifically with the setState(device.state) command as suggested but this does not work.

Re: .afterMin not retriggerable?

Posted: Tuesday 02 January 2018 17:42
by dannybloe
Well, the thing is this: at first you do a switchOn().forMin(2). At that moment, domoticz checks the current state of the device (Off), switches the device On and queues a command to restore the current state (which is Off). Then, after 1 minute, new motion is detected and a new switchOn().forMin(2) command is sent to Domoticz. Domoticz receives the command, sees that there is a queued command (switch off) and ditches the command from the queue. Then it checks the current state of the device (On), executes the switchOn() and queues the command to bring the device back to the current state. At this point however, the current state is On so it queues the On-command. Which exactly causes the lamp never to go off again.

The logic behind the forXXX() command in Domoticz is that it restores the current state of the device after the given time. So, in your case it is clear what should happen however Domoticz cannot guess this. You will have to do it yourself in this case.

What you can do is check the current state. If the device is On then you do a switchOff().afterMin(2) and otherwise you do a switchOn().forMin(2).

That should work. (famous last words).

Re: .afterMin not retriggerable?

Posted: Wednesday 03 January 2018 17:19
by IanDury
Thanks for the explanation dannyblue. I understand your logic but I'm not sure if this is intuitive behaviour. I guess this is debatable.

Thanks for your great work on dzVents. It makes my lua scripts much more readable.

Re: .afterMin not retriggerable?

Posted: Wednesday 03 January 2018 17:39
by dannybloe
Well, the thing is that there can be a lot of items in the command queue for the same device and the state that you want to restore isn't always just on or off. It can be dimlevels, colors etc. But feel free to improve the algorithm in Domoticz code :-p. It's a challenge, believe me.

Re: .afterMin not retriggerable?

Posted: Wednesday 03 January 2018 17:40
by dannybloe
And thanks for the compliment. Glad you like it. Do you have prior programming experience? Or do you consider yourself as a beginner?

Re: .afterMin not retriggerable?

Posted: Thursday 04 January 2018 12:25
by dannybloe
This should work too:

Code: Select all

light.switchOff().checkFirst().afterMin(2)
light.switchOn().checkFirst().forMin(2)
Only one will be sent to Domoticz.

Re: .afterMin not retriggerable?

Posted: Monday 08 January 2018 21:24
by kazze
Hi,
sorry for hijacking this thread, one more clarification:
"The logic behind the forXXX() command in Domoticz is that it restores the current state of the device after the given time." << - regarding this...is <dimTo> considered a state change as well? For example, dimTo (70).forMin(1) when the starting level is for example 30 - does this means that the switch will be restored to dim level 30 after one minute?
Thanks for your support, great work!

Re: .afterMin not retriggerable?

Posted: Monday 08 January 2018 22:07
by dannybloe
Yes.

Re: .afterMin not retriggerable?

Posted: Tuesday 09 January 2018 15:25
by triton
Works like a charm, just what I needed. Thank you for the explanation how the internals work on this.
dannybloe wrote: Tuesday 02 January 2018 17:42 What you can do is check the current state. If the device is On then you do a switchOff().afterMin(2) and otherwise you do a switchOn().forMin(2).

That should work. (famous last words).