.afterMin not retriggerable?

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
IanDury
Posts: 47
Joined: Wednesday 08 April 2015 15:22
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

.afterMin not retriggerable?

Post 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.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: .afterMin not retriggerable?

Post 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).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
IanDury
Posts: 47
Joined: Wednesday 08 April 2015 15:22
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: .afterMin not retriggerable?

Post 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.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: .afterMin not retriggerable?

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: .afterMin not retriggerable?

Post 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?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: .afterMin not retriggerable?

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
kazze
Posts: 3
Joined: Tuesday 02 January 2018 9:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: .afterMin not retriggerable?

Post 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!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: .afterMin not retriggerable?

Post by dannybloe »

Yes.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
triton
Posts: 15
Joined: Monday 03 April 2017 15:01
Target OS: Linux
Domoticz version: 4.9701
Location: Netherlands
Contact:

Re: .afterMin not retriggerable?

Post 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).
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest