Order in which scripts are triggered.

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

Moderator: leecollings

Post Reply
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Order in which scripts are triggered.

Post 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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Order in which scripts are triggered.

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: Order in which scripts are triggered.

Post 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
Regards,

Rob
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Order in which scripts are triggered.

Post 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
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Order in which scripts are triggered.

Post 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.
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Order in which scripts are triggered.

Post 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.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Order in which scripts are triggered.

Post 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?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: Order in which scripts are triggered.

Post 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?
Regards,

Rob
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Order in which scripts are triggered.

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

Who is online

Users browsing this forum: No registered users and 1 guest