dzVents script not triggered by PIR status On, but triggered by status Off

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

Moderator: leecollings

Post Reply
Superdeboer
Posts: 10
Joined: Tuesday 21 April 2020 10:10
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Netherlands
Contact:

dzVents script not triggered by PIR status On, but triggered by status Off

Post by Superdeboer »

Hello, I have a problem that I've been running into for the past week or so.
I don't know if it is a coincidence that I updated my Domoticz to the most recent version around the same time.
My script was running (kind of) flawless until then and now it isn't.
That being said, I don't know whether this is a dzVents issue or rathers something to do with the behavior my PIR(s).

It seems that my script is only triggered when a PIR changes its status to "Off" and not on the status change to "On". Which is unfortunate, because I use it to do stuff immediately following the detection of motion.

This is the script that I use to flip a virtual switch named 'BewegingWoonkamerTimeout'. It used to flip the switch immediately when motion was detected, but since a week or so I noticed quite an annoying delay in this process. Tonight I got the time to look further into it and I discovered that it seems that this code does not flip the switch when the PIR status changes to "On", but it does flip the switch when the PIR status changes to "Off".

Code: Select all

--[[ livingroomMotionHelper.lua

    This script flips the Dummy switch named 'BewegingWoonkamerTimeout' when one of the PIR WK sensors 
    (the PIR's in the WoonKamer) detects motion. 
    When none of the PIR's has been active for 10 minutes, the Dummy switches is flipped to off status.
    The status of the Dummy switch is used in the conditionalMotion script that relies on this 
    for determining whether the lights can be switched off.
    
     original source: https://www.domoticz.com/forum/viewtopic.php?t=30136
     
]]--
    
return
{
    on =
    {
        devices = {'PIR WK*'}, -- triggered by PIRs in the livingroom
        timer = {'every minute'},
    },
       
   logging =
   {
        level = domoticz.LOG_ERROR, -- Select one of LOG_INFO, LOG_DEBUG, LOG_ERROR, LOG_FORCE to override system log level
        marker = "### livingroomMotionHelper ###"
   },

    execute = function(dz,trigger,triggerInfo)
        local active = 0
        local motionActive = dz.devices('BewegingWoonkamerTimeout')

        -- filter devices on name
        dz.devices().filter(function(dv)
            return (dv.name):match('PIR WK')
        -- filter result on lastUpdate
        end).filter(function(dv)
            return dv.lastUpdate.minutesAgo <= 10
        -- act on result
        end).forEach(function(dv)
            active = active + 1
            --dz.log(dv.name .. '; status ==>> ' .. dv.state .. '; last Updated: ' .. dv.lastUpdate.rawDate ..', ' ..dv.lastUpdate.rawTime ,dz.LOG_FORCE)
        end)

        if active == 0 then
            motionActive.switchOff().checkFirst()
        else
            motionActive.switchOn().checkFirst()
            dz.log('BewegingWoonkamerTimeout aangezet',dz.LOG_FORCE)            
        end
        --dz.log('Aantal actieve devices = ' .. active,dz.LOG_FORCE)
    end
}
Looking more closely into the log, I gather that the PIR changing status to "On" does not even trigger the script. Although the script is set to be triggered on devices = {'PIR WK*'}, it seems that only the status change to "Off", 30 seconds after being set to "On", does trigger the above script. And there's my delay.
These are the relevant lines of the logfile:

Code: Select all

2021-05-03 22:23:14.430 Aeon Z-Stick: Light/Switch (PIR WK FIB3)
2021-05-03 22:23:14.594 Status: dzVents: Info: Handling events for: "PIR WK FIB3", value: "On"
[... nothing happens here ...]
2021-05-03 22:23:48.876 Aeon Z-Stick: Light/Switch (PIR WK FIB3)
2021-05-03 22:23:49.126 Dummy: Lighting 1 (BewegingWoonkamerTimeout)
2021-05-03 22:23:49.037 Status: dzVents: Info: Handling events for: "PIR WK FIB3", value: "Off"
2021-05-03 22:23:49.115 Status: dzVents: !Info: ### livingroomMotionHelper ###: BewegingWoonkamerTimeout aan
2021-05-03 22:23:49.118 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
So here's my question, taking into account that I did not change anything in my scripts in the past few weeks: is this change in behavior caused by dzVents or a dzVents update, or should I replace my PIR and see if that is the problem?

The PIR is a Fibaro motion sensor (not the most recent version).
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents script not triggered by PIR status On, but triggered by status Off

Post by waaren »

Superdeboer wrote: Monday 03 May 2021 22:31 Looking more closely into the log, I gather that the PIR changing status to "On" does not even trigger the script. Although the script is set to be triggered on devices = {'PIR WK*'}, it seems that only the status change to "Off", 30 seconds after being set to "On", does trigger the above script. And there's my delay.
I guess quickest way to find out what happens is to add below line at line 36

Code: Select all

dz.log(dv.name .. '; status ==>> ' .. dv.state .. '; last Updated: ' .. dv.lastUpdate.minutesAgo ,dz.LOG_FORCE)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Superdeboer
Posts: 10
Joined: Tuesday 21 April 2020 10:10
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Netherlands
Contact:

Re: dzVents script not triggered by PIR status On, but triggered by status Off

Post by Superdeboer »

I added the line and this is the output:

Code: Select all

 2021-05-04 14:27:39.990 Status: dzVents: Info: Handling events for: "PIR WK FIB3", value: "On"
2021-05-04 14:27:40.071 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB2; status ==>> Off; last Updated: 7 min ago
2021-05-04 14:27:40.071 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK NEO1; status ==>> Off; last Updated: 7 min ago
2021-05-04 14:27:40.071 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB1; status ==>> Off; last Updated: 7 min ago
2021-05-04 14:27:40.071 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB3; status ==>> On; last Updated: 6 min ago
2021-05-04 14:27:40.071 Status: dzVents: !Info: ### livingroomMotionHelper ###: BewegingWoonkamerTimeout aan
2021-05-04 14:27:49.527 Aeon Z-Stick: Lux (LUX WK FIB1)
2021-05-04 14:27:49.707 Status: dzVents: Info: Handling events for: "LUX WK FIB1", value: "283"
2021-05-04 14:28:00.564 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB2; status ==>> Off; last Updated: 7 min ago
2021-05-04 14:28:00.564 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK NEO1; status ==>> Off; last Updated: 7 min ago
2021-05-04 14:28:00.564 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB1; status ==>> Off; last Updated: 7 min ago
2021-05-04 14:28:00.564 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB3; status ==>> On; last Updated: 0 min ago
2021-05-04 14:28:00.565 Status: dzVents: !Info: ### livingroomMotionHelper ###: BewegingWoonkamerTimeout aan 
 2021-05-04 14:28:15.166 Status: Aeon Z-Stick: OpenZWave: Alarm received (Home Security: Motion Detected at Unknown Location), NodeID: 5 (0x05)
2021-05-04 14:28:15.346 Status: dzVents: Info: Handling events for: "PIR WK FIB2", value: "On"
2021-05-04 14:28:15.425 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB2; status ==>> On; last Updated: 8 min ago
2021-05-04 14:28:15.426 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK NEO1; status ==>> Off; last Updated: 8 min ago
2021-05-04 14:28:15.426 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB1; status ==>> Off; last Updated: 8 min ago
2021-05-04 14:28:15.426 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB3; status ==>> On; last Updated: 0 min ago
2021-05-04 14:28:15.426 Status: dzVents: !Info: ### livingroomMotionHelper ###: BewegingWoonkamerTimeout aan
2021-05-04 14:28:15.578 Status: Aeon Z-Stick: OpenZWave: Alarm received (Home Security: Motion Detected at Unknown Location), NodeID: 6 (0x06)
2021-05-04 14:28:15.712 Status: dzVents: Info: Handling events for: "PIR WK FIB1", value: "On"
2021-05-04 14:28:15.789 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB2; status ==>> On; last Updated: 0 min ago
2021-05-04 14:28:15.789 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK NEO1; status ==>> Off; last Updated: 8 min ago
2021-05-04 14:28:15.789 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB1; status ==>> On; last Updated: 8 min ago
2021-05-04 14:28:15.789 Status: dzVents: !Info: ### livingroomMotionHelper ###: PIR WK FIB3; status ==>> On; last Updated: 0 min ago
2021-05-04 14:28:15.789 Status: dzVents: !Info: ### livingroomMotionHelper ###: BewegingWoonkamerTimeout aan 
So it seems like it does not depend on the PIR (I thought only PIR WK FIB3 showed this behavior).
When the script livingroomMotionHelper is triggered by a PIR after some time, the lastUpdate value of the PIR that triggers it has not been updated immediately. Only the second time the script is triggered within a short time after, usually not by another PIR but by the time trigger (each minute), the lastUpdate value has been changed accordingly.
So this means that the livingroomMotionHelper does not flip the BewegingWoonkamerTimeout immediately (note that in the above log lines this dummy switch was STILL on because of previous motion within 10 minutes before), but only the second time the script is triggered.

However, I do not know how to correct this behavior. And also I am quite sure that this did not occur a few weeks ago, because the script worked fine until approximately one week ago.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents script not triggered by PIR status On, but triggered by status Off

Post by waaren »

Superdeboer wrote: Tuesday 04 May 2021 14:38 However, I do not know how to correct this behavior. And also I am quite sure that this did not occur a few weeks ago, because the script worked fine until approximately one week ago.
There was an issue with the previous stable with lastUpdate and that was fixed in V2020.2 build 12002. (see github issue 4112 )
Maybe that fix is now causing an unexpected behavior of your script.

You could try this modified version

Code: Select all

--[[ livingroomMotionHelper.lua

    This script flips the Dummy switch named 'BewegingWoonkamerTimeout' when one of the PIR WK sensors
    (the PIR's in the WoonKamer) detects motion.
    When none of the PIR's has been active for 10 minutes, the Dummy switches is flipped to off status.
    The status of the Dummy switch is used in the conditionalMotion script that relies on this
    for determining whether the lights can be switched off.

     original source: https://www.domoticz.com/forum/viewtopic.php?t=30136

]]--

return
{
    on =
    {
        devices = {'PIR WK*'}, -- triggered by PIRs in the livingroom
        timer = {'every minute'},
    },

   logging =
   {
        level = domoticz.LOG_ERROR, -- Select one of LOG_INFO, LOG_DEBUG, LOG_ERROR, LOG_FORCE to override system log level
        marker = "### livingroomMotionHelper ###"
   },

    execute = function(dz,trigger,triggerInfo)
        local active = 0
        local motionActive = dz.devices('BewegingWoonkamerTimeout')

        -- filter devices on name
        dz.devices().filter(function(dv)
            return (dv.name):match('PIR WK')
        -- filter result on lastUpdate
        end).filter(function(dv)
            return dv.lastUpdate.minutesAgo <= 10
        -- act on result
        end).forEach(function(dv)
            active = active + 1
            --dz.log(dv.name .. '; status ==>> ' .. dv.state .. '; last Updated: ' .. dv.lastUpdate.rawDate ..', ' ..dv.lastUpdate.rawTime ,dz.LOG_FORCE)
        end)

        if trigger.isDevice and trigger.active then
            motionActive.switchOn().checkFirst()
            dz.log('BewegingWoonkamerTimeout (zo nodig) aangezet',dz.LOG_FORCE)
        elseif active == 0 then
            motionActive.switchOff().checkFirst()
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Superdeboer
Posts: 10
Joined: Tuesday 21 April 2020 10:10
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Netherlands
Contact:

Re: dzVents script not triggered by PIR status On, but triggered by status Off

Post by Superdeboer »

I read something about the issue that you mentioned on this forum last night, but I could not figure out what the consequences could be for my script.
Thank you very much for suggesting a modified version. I understand the difference between yours and mine. I just installed it and I will test if it works later today.
Superdeboer
Posts: 10
Joined: Tuesday 21 April 2020 10:10
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Netherlands
Contact:

Re: dzVents script not triggered by PIR status On, but triggered by status Off

Post by Superdeboer »

It works like a charm! Thank you so much for your quick replies! Problem solved.
DO977
Posts: 5
Joined: Thursday 04 August 2022 11:56
Target OS: Linux
Domoticz version: 2023.2
Contact:

Re: dzVents script not triggered by PIR status On, but triggered by status Off

Post by DO977 »

Hi

I'm trying to get the individual device name which has been triggered to be written to the log, however dv.name does not work.

The 6th line from the bottom is currently:

Code: Select all

dz.log('BewegingWoonkamerTimeout aangezet',dz.LOG_FORCE)
I have updated it to :

Code: Select all

dz.log(dv.name,dz.LOG_FORCE)
However nothing is written to the log file. The same occurs when using devices.name.

I have also tried dz.devices().value and dz.devices().name, which do write to the log file, however where I'd expect the device name, 'nil' is present.

I'm clearly missing something obvious, but can anyone see what the issue is please?

Thanks
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest