Page 1 of 1
dzVents - inspecting all Pir sensors in the house
Posted: Wednesday 13 November 2019 15:53
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
}
Re: dzVents - inspecting all Pir sensors in the house
Posted: Wednesday 13 November 2019 20:37
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
}
Re: dzVents - inspecting all Pir sensors in the house
Posted: Wednesday 13 November 2019 21:11
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
}
Re: dzVents - inspecting all Pir sensors in the house
Posted: Thursday 14 November 2019 12:25
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
Re: dzVents - inspecting all Pir sensors in the house
Posted: Thursday 14 November 2019 15:10
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
Re: dzVents - inspecting all Pir sensors in the house
Posted: Sunday 17 November 2019 16:21
by bertbigb
Hi Waaren,
Thanks again for your response. Now I understand.
Re: dzVents - inspecting all Pir sensors in the house
Posted: Tuesday 04 May 2021 23:47
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.
Re: dzVents - inspecting all Pir sensors in the house
Posted: Friday 05 January 2024 11:38
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 ...
Re: dzVents - inspecting all Pir sensors in the house
Posted: Friday 05 January 2024 11:49
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.
Re: dzVents - inspecting all Pir sensors in the house
Posted: Friday 05 January 2024 12:17
by Kedi
Should not this be the motion sensors?
Re: dzVents - inspecting all Pir sensors in the house
Posted: Friday 05 January 2024 14:08
by rezzalex
Kedi wrote: ↑Friday 05 January 2024 12:17
Should not this be the motion sensors?
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
Re: dzVents - inspecting all Pir sensors in the house
Posted: Friday 05 January 2024 14:11
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
Re: dzVents - inspecting all Pir sensors in the house
Posted: Friday 05 January 2024 14:17
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?
Re: dzVents - inspecting all Pir sensors in the house
Posted: Friday 05 January 2024 14:26
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 ...
Re: dzVents - inspecting all Pir sensors in the house
Posted: Friday 05 January 2024 14:40
by rezzalex
having a quick look at LUA string livrairy, not sure If I can easily use a "begin by" function for strings...
Re: dzVents - inspecting all Pir sensors in the house
Posted: Friday 05 January 2024 15:36
by rezzalex
OK, now it works, sorry for disturing...