dzVents - inspecting all Pir sensors in the house

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

Moderator: leecollings

Post Reply
bertbigb
Posts: 147
Joined: Thursday 13 August 2015 13:36
Target OS: NAS (Synology & others)
Domoticz version: beta
Location: Netherlands
Contact:

dzVents - inspecting all Pir sensors in the house

Post by bertbigb »

Dear All,

I started with dzVents just now and I want to translate my lua scripts to dzVents.
All my Pir / Motion sensors in the house are called Motion-<roomname>
Now I want to know if there is some movement in the house and if none of the sensors noticed motion in the house for the last hour I want to switch off the seperate / general Motion sensor.
From what I know about the subject I came to the following. But it doesn't work.
What did I do wrong and what to change in the scrip to make it work?
Your help is appreciated.

Code: Select all

local active = 0

return {
   logging = {
      level = domoticz.LOG_ERROR, -- Select one of LOG_INFO, LOG_DEBUG, LOG_ERROR, LOG_FORCE to override system log level
      marker = "-=# Movement #=-"
},
    on = {
		devices = {'Motion-*'},
		timer = {'every minute'},

	},
	data = {},
	logger = {},
	execute = function(domoticz, triggeredItem)
		-- check all Motion / Pir sensors 
		domoticz.devices().filter(function(device) 
		    return (string.match('Motion-', device.name)~=nil)
		end).forEach(function(motion)   
            if (domoticz.devices('Motion-', device.name).lastUpdate.minutesAgo <= 60) then
                active = active + 1
            end
		end)

		if active == 0 then 
		    domoticz.devices('Motion').switchOff()
		else
		    domoticz.devices('Motion').switchOn()
        end
    	print('Aantal actieve devices = ' .. active)
	end
}
Best regards Bert

Synology DS1517+ - DSM 6.2
Raspberry PI2-B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by waaren »

bertbigb wrote: Wednesday 13 November 2019 15:53 All my Pir / Motion sensors in the house are called Motion-<roomname>
What to change in the script to make it work?
This should do it.

Code: Select all

return
{
    on =
    {
        devices = {'Motion-*'},
        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 = "-=# Movement #=-"
   },

    execute = function(dz, item)
        local active = 0
        local motionActive = dz.devices('Motion')
       
        -- loop over all devices
        dz.devices().forEach(function(dv)
            -- check names / lastUpdate
            if (dv.name):match('Motion%-') and dv.lastUpdate.minutesAgo <= 60 then
                active = active + 1
                dz.log(dv.name .. '; status ==>> ' .. dv.state .. '; last Updated: ' .. dv.lastUpdate.rawDate ..', ' ..dv.lastUpdate.rawTime ,dz.LOG_FORCE)
            end
        end)

        if active == 0 then
            motionActive.switchOff().checkFirst()
        else
            motionActive.switchOn().checkFirst()
        end
        dz.log('Aantal actieve devices = ' .. active,dz.LOG_FORCE)
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by waaren »

bertbigb wrote: Wednesday 13 November 2019 15:53 All my Pir / Motion sensors in the house are called Motion-<roomname>
What to change in the script to make it work?
Or this..

Code: Select all

return
{
    on =
    {
        devices = {'Motion-*'},
        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 = "-=# Movement #=-"
   },

    execute = function(dz, item)
        local active = 0
        local motionActive = dz.devices('Motion')

        -- filter devices on name
        dz.devices().filter(function(dv)
            return (dv.name):match('Motion%-')
        -- filter result on lastUpdate
        end).filter(function(dv)
            return dv.lastUpdate.minutesAgo <= 60 
        -- 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()
        end
        dz.log('Aantal actieve devices = ' .. active,dz.LOG_FORCE)
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
bertbigb
Posts: 147
Joined: Thursday 13 August 2015 13:36
Target OS: NAS (Synology & others)
Domoticz version: beta
Location: Netherlands
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by bertbigb »

Hi Waaren,

First off all many thanks for these examples and your quick reply, both scripts are doing what I expected and I could see what you did different compared to my first attempt so also a learning experience.

One thing I don't get and probably you can explain a bit:
In the first script there is a line with:

Code: Select all

if (dv.name):match('Motion%-') and dv.lastUpdate.minutesAgo <= 60 then
How does that match? Motion%- since the sensors are called Motion-<roomname>, so the position of the wildcard is confusing me.
Also in the second script you provided:

Code: Select all

if (dv.name):match('Motion%-') and dv.lastUpdate.minutesAgo <= 60 then
I would have expected something like Motion-% but when I change it that way I get an error
malformed pattern (ends with '%')

Hope you can give a little bit of explanation so I can understand why it is like this.

Again many thanks
Bert
Best regards Bert

Synology DS1517+ - DSM 6.2
Raspberry PI2-B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by waaren »

bertbigb wrote: Thursday 14 November 2019 12:25 I would have expected something like Motion-% but when I change it that way I get an error
malformed pattern (ends with '%')
Hope you can give a little bit of explanation so I can understand why it is like this.
The % is not a wildcard character in Lua. In patterns % is used in combination with a letter to define a character class
  • %a --> all letters
  • %c --> control characters
  • %d --> digits
  • %l --> lower case letters
  • %p -- > punctuation characters
  • %s --> space characters
  • %u --> upper case letters
  • %w --> alphanumeric characters
  • %x --> hexadecimal digits
  • %z --> the character with representation 0
An upper case version of any of those classes represents the complement of the class. For instance, '%A' represents all non-letter characters.

Some characters, called magic characters, have special meanings when used in a pattern. The magic characters are
( ) . % + - * ? [ ^ $
The character `%´ works as an escape for those magic characters. So, '%-' matches a -;

For more detailed information look at the free Lua manual: Programming in Lua
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
bertbigb
Posts: 147
Joined: Thursday 13 August 2015 13:36
Target OS: NAS (Synology & others)
Domoticz version: beta
Location: Netherlands
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by bertbigb »

Hi Waaren,

Thanks again for your response. Now I understand.
Best regards Bert

Synology DS1517+ - DSM 6.2
Raspberry PI2-B, Raspberry Nano - Raspberry PI3 - model B
Xiaomi Gateway - Philips HUE Lights - Zwave - RFXCom(E) with KaKu and other 433MHz devices - Yeelight Lights - Toon
Superdeboer
Posts: 10
Joined: Tuesday 21 April 2020 10:10
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Netherlands
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by Superdeboer »

For future reference:
I used this script for more than a year without any problems. After updating Domoticz (and with it dzVents), it suddenly wasn't working like it should. In viewtopic.php?f=59&t=36202 you can find a solution to the problem that I experienced. The problem has to do with an issue in dzVents regarding lastUpdate, that was fixed, but broke this script.
rezzalex
Posts: 49
Joined: Thursday 24 September 2020 14:30
Target OS: NAS (Synology & others)
Domoticz version: Beta
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by rezzalex »

Hello,

I am trying to check all my PIR sensors, but the match function is not working at all
I have DZ almost latest Beta version.

Code: Select all

return {
	on = {
		devices = {
			'Test'
		}
	},
	logging = {
		level = domoticz.LOG_FORCE,
		marker = 'test',
	},
	execute = function(domoticz, device)
	    -- filter devices on name
        domoticz.devices().forEach(function(dv)
            -- check names / lastUpdate
            if (dv.name):match('DETECT%-') then
        -- filter result on lastUpdate
--        end).filter(function(dv)
--            return dv.lastUpdate.minutesAgo <= 1
            -- act on result
--            end).forEach(function(dv)
                domoticz.log(dv.name .. '; status ==>> ' .. dv.state .. '; last Updated: ' .. dv.lastUpdate.rawDate ..', ' ..dv.lastUpdate.rawTime , domoticz.LOG_FORCE)
            end
        end)
I tried to enter 1 full name of a device, replacing DETECT%- by "DETECT_MOUV_salle-a-manger", still not working ...
willemd
Posts: 648
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by willemd »

I have no experience defining functions within DzVents, but when I read and compare your script to the original in this thread, I see that the original is making a call to the defined function and you are not.
You could also try to add a "else" part to the if-statement to check whether that part of the code is executed at all.
Kedi
Posts: 575
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by Kedi »

Should not this be the motion sensors?

Code: Select all

		devices = {
			'Test'
		}
Logic will get you from A to B. Imagination will take you everywhere.
rezzalex
Posts: 49
Joined: Thursday 24 September 2020 14:30
Target OS: NAS (Synology & others)
Domoticz version: Beta
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by rezzalex »

Kedi wrote: Friday 05 January 2024 12:17 Should not this be the motion sensors?

Code: Select all

		devices = {
			'Test'
		}
No, it is a dummy device to test.
the real context of my script si to check all PIR when Alarm is set automatically, to check on any remaining presence at home like kids or guests
rezzalex
Posts: 49
Joined: Thursday 24 September 2020 14:30
Target OS: NAS (Synology & others)
Domoticz version: Beta
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by rezzalex »

willemd wrote: Friday 05 January 2024 11:49 I have no experience defining functions within DzVents, but when I read and compare your script to the original in this thread, I see that the original is making a call to the defined function and you are not.
You could also try to add a "else" part to the if-statement to check whether that part of the code is executed at all.
The "else" is working, meaning the match function doesn't
willemd
Posts: 648
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by willemd »

rezzalex wrote: Friday 05 January 2024 14:11
willemd wrote: Friday 05 January 2024 11:49 I have no experience defining functions within DzVents, but when I read and compare your script to the original in this thread, I see that the original is making a call to the defined function and you are not.
You could also try to add a "else" part to the if-statement to check whether that part of the code is executed at all.
The "else" is working, meaning the match function doesn't
And the dv.name that you are printing to the log is as expected?
rezzalex
Posts: 49
Joined: Thursday 24 September 2020 14:30
Target OS: NAS (Synology & others)
Domoticz version: Beta
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by rezzalex »

OK, I think this is working, except the name of the devices I want to check have a special characters in the middle.

I have tried with my "presence" device, and only works for the 2 more recents, the 2 olders are not recogised by the match, despite the begining of their names are exactly the same ...
rezzalex
Posts: 49
Joined: Thursday 24 September 2020 14:30
Target OS: NAS (Synology & others)
Domoticz version: Beta
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by rezzalex »

having a quick look at LUA string livrairy, not sure If I can easily use a "begin by" function for strings...
rezzalex
Posts: 49
Joined: Thursday 24 September 2020 14:30
Target OS: NAS (Synology & others)
Domoticz version: Beta
Contact:

Re: dzVents - inspecting all Pir sensors in the house

Post by rezzalex »

OK, now it works, sorry for disturing...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest