Page 1 of 1

Order in which scripts are triggered.

Posted: Wednesday 17 February 2021 10:29
by EddyG
This is my script:

Code: Select all

return {

    active = true,
--  active = false,

    on = {
        devices = { 'PIR Slaapkamer Sensor', 'PIR Slaapkamer Lux' }
--        devices = { ['PIR Slaapkamer Sensor']  = { 'at 10:00-22:00'  } }
    },

    execute = function(dz, device)
        local debug = true
--      local debug = false

        local lux = dz.devices("PIR Slaapkamer Lux").lux

        if debug then
            print(device.name .. ' = ' .. device.state .. " - Lux = " .. lux)
        end
        if device.state == "On" then
            if lux < 5 then
                dz.devices("Bedlampen").cancelQueuedCommands()
                dz.devices("Bedlampen").switchOn().checkFirst()
            end
        else
            dz.devices("Bedlampen").cancelQueuedCommands()
            dz.devices("Bedlampen").switchOff().checkFirst().afterMin(5)
        end
    end
}
I normally use the second trigger, but the first trigger shows the problem.
Outside the trigger hours I switch on bedroom lights either by Google commands or a Zigbee wallswitch.
The device is a https://www.zigbee2mqtt.io/devices/RTCG ... -rtcgq11lm
Which only sends the lux value when the motion triggers the device.
The problem is that first the motion triggers the script and second the lux triggers the script.
My problem is solved when it always is first the lux value is updated, because the motion triggers uses at midday the 'old' lux value (below 5 lux) of hours before.
I know it is not a REAL problem, but still it bugs me.
The zigbee output is just 1 line with all the values. Below the ON and OFF

Code: Select all

info  2021-02-17 10:11:45: MQTT publish: topic 'zigbee2mqtt/SlaapkamerPIR', payload '{"battery":91,"illuminance":177,"illuminance_lux":177,"last_seen":"2021-02-17T10:11:45+01:00","linkquality":33,"occupancy":true,"temperature":30,"voltage":2985}'
info  2021-02-17 10:13:15: MQTT publish: topic 'zigbee2mqtt/SlaapkamerPIR', payload '{"battery":91,"illuminance":177,"illuminance_lux":177,"last_seen":"2021-02-17T10:11:45+01:00","linkquality":33,"occupancy":false,"temperature":30,"voltage":2985}'
Has anyone a solution?

Re: Order in which scripts are triggered.

Posted: Wednesday 17 February 2021 10:50
by waaren
EddyG wrote: Wednesday 17 February 2021 10:29 Has anyone a solution?
I am not sure I completely understand the issue but let's try...

what would happen if you code it like below?

if the script is triggered by the motion sensor and the lastUpdate.secondsAgo of the lux is too long ago then don't do anything
if the script is triggered by the lux sensor and the lastUpdate.secondsAgo of the motion is too long ago then don't do anything

Re: Order in which scripts are triggered.

Posted: Wednesday 17 February 2021 15:51
by erem
Since motion always triggers a lux update, why not

Code: Select all

    on = {
        devices = {'PIR Slaapkamer Lux' }
that would prevent the double trigger, and give you the current lux value.

just my .02

Re: Order in which scripts are triggered.

Posted: Wednesday 17 February 2021 19:11
by EddyG
Tnx, I think the solution of @erem might work, sorry @waaren ;)
I try that one first, but will do that to morrow because currently lux = 0

Re: Order in which scripts are triggered.

Posted: Wednesday 17 February 2021 20:01
by EddyG
Sorry @erem, your solution won't work, because when you enter a dark room the lux = 0 and will not change until you turn on the lights.
So I go further with the solution of @waaren.

Re: Order in which scripts are triggered.

Posted: Thursday 18 February 2021 10:33
by EddyG
The @waaren solution does not work for me either.
So I stick with the fact that I have to live with my script that works but not perfectly.
Cause: the Lux trigger comes (always?) after the motion trigger.

Re: Order in which scripts are triggered.

Posted: Thursday 18 February 2021 10:40
by waaren
EddyG wrote: Thursday 18 February 2021 10:33 The @waaren solution does not work for me either.
I am kind of interested to understand what happens. Can you share what you tried and the results in the log?

Re: Order in which scripts are triggered.

Posted: Thursday 18 February 2021 12:21
by erem
EddyG wrote: Wednesday 17 February 2021 20:01 Sorry @erem, your solution won't work, because when you enter a dark room the lux = 0 and will not change until you turn on the lights.
So I go further with the solution of @waaren.
i am not sure i understand.
you wrote
EddyG wrote: Wednesday 17 February 2021 10:29 The device is a https://www.zigbee2mqtt.io/devices/RTCG ... -rtcgq11lm
Which only sends the lux value when the motion triggers the device.
i understood that every time motion was detected, a lux value would be sent.
So, logically, the 'PIR Slaapkamer Lux' would be triggered, albeit with value 0.

what am i missing?

Re: Order in which scripts are triggered.

Posted: Thursday 18 February 2021 14:24
by EddyG
I changed the script.

Code: Select all

return {

    active = true,
--  active = false,

    on = {
        devices = { ['PIR Slaapkamer*']  = { 'at 10:00-22:00'  } }
    },

    execute = function(dz, device)
        local debug = true
--      local debug = false

        local lux = dz.devices("PIR Slaapkamer Lux").lux
        if debug then
            if device.name == 'PIR Slaapkamer Sensor' then
                print('Trigger: '.. device.name .. ' - State: ' .. device.state .. " - Lux = " .. lux)
            else
                print('Trigger: '.. device.name .. " - Lux = " .. lux)
            end
        end
        if dz.devices('PIR Slaapkamer Sensor').state == "On" then
            if lux < 5 then
                dz.devices("Bedlampen").cancelQueuedCommands()
                dz.devices("Bedlampen").switchOn().checkFirst()
            end
        else
            dz.devices("Bedlampen").cancelQueuedCommands()
            dz.devices("Bedlampen").switchOff().checkFirst().afterMin(5)
        end
    end
}
The main difference is the wildcard in the devicename:

Code: Select all

        devices = { ['PIR Slaapkamer*']  = { 'at 10:00-22:00'  } }
Now the scripts gets triggered twice everytime. The second trigger is on the Lux value and that does the trick, because the first trigger on motion already changed the lux value so that the second trigger has the right lux value to work with.
I think this also works tonight when it is really needed.

My issue was that lights should go on the first time I enter the room when the lux value is below 5 lux. In the initial script the first time was always the old/previous value of lux and that might be more then 5 lux, because it was from early on during mid day.
The timeframe of 10:00-22:00 is chosen because I also want the lights go on when on a hot summer day the rollershutters are down and the room is also dark. I just tried and it works. The only downside is that I have 2 triggers and the first is just a "dummy" trigger just to get the right lux value in the sensor for the second trigger.