Page 1 of 1
Looking for a very simple script for a door open notification.
Posted: Monday 02 September 2019 19:56
by fotomann100
Hello,
I'm a beginner in dzVents.
I'm looking for a script template with does the following:
1. If a door is open for more than x minutes, It should send a notification.
2. The script should be triggered by the door sensor itself (Status open) and should not run periodically every x minutes.
I found some templates here in the forum, which I tested. There are working fine, but are to complex for me to analyse (I like to understand what the script is doing and how).
Can anybody provide me a simple version of such a script? I would be appreciated for any help.
Re: Looking for a very simple script for a door open notification.
Posted: Monday 02 September 2019 20:10
by hoeby
It is easier for everyone to stay with one thread.
And not open another thread when a question is added on your first question.
https://www.domoticz.com/forum/viewtopi ... 62&t=29240
Could you tell use why you don't want a timer trigger in the script, which looks every X minutes if the script is true or false?
Re: Looking for a very simple script for a door open notification.
Posted: Monday 02 September 2019 20:31
by fotomann100
Good evening hoeby,
Please excuse the new thread in the dzVents forum. I realized that the function cannot be programmed with blockly. So I considered the thread is complete. Sorry, If this makes some confusion.
To your question:
The script is only needed every few days. I thought, Its more economical (keyword: processor load) to start the script only if it is really needed.
Re: Looking for a very simple script for a door open notification.
Posted: Monday 02 September 2019 21:48
by waaren
fotomann100 wrote: ↑Monday 02 September 2019 19:56
I'm looking for a script template with does the following:
1. If a door is open for more than x minutes, It should send a notification.
2.
The script should be triggered by the door sensor itself (Status open) and should not run periodically every x minutes.
Can anybody provide me a simple version of such a script? I would be appreciated for any help.
Even the most simple method (in dzVents, Lua or Blockly) capable of doing this will need a second trigger to send the notification.
In dzVents that could be done by either
- a timer that executes every minute (according to 2. you don't want that)
- an additional virtual device specific used to generate an event after x minutes (can also be a "hidden" device that will not be visible in the switches tab)
- a delayed call (openURL) to domoticz o generate an event after x minutes (to the logfacility or anything else)
'simple' is subjective. According to some "once you understand it, it's simple"
Please feel free to ask if something is not clear.
method using openURL
Code: Select all
-- *** user settings below this line **
local doorSensorName = 'Doorsensor' -- change to the name of your doorSensor'
local scriptVar = 'simpleDelayedNotification'
local secondsUntilNotification = 360
-- *** No changes required below this line **
return
{
on = {
devices = { doorSensorName },
httpResponses = { scriptVar },
},
execute = function(dz, item)
local doorSensor = dz.devices(doorSensorName)
local function retriggerScript(delay)
local url = dz.settings['Domoticz url'] ..
'/json.htm?type=command¶m=addlogmessage&message=' ..
scriptVar .. '%20retriggered'
dz.openURL ({ url = url, callback = scriptVar }).afterSec(delay) -- to come back after delay seconds
end
if item.isDevice and item.active then
retriggerScript(secondsUntilNotification) -- come back after x seconds
else -- triggered by HTTPResponse
if doorSensor.active then
local secondsOpened = doorSensor.lastUpdate.secondsAgo
if secondsOpened >= secondsUntilNotification then
dz.notify(doorSensorName,'Still open after ' .. secondsOpened .. ' seconds.' )
end
end
end
end
}
method using hidden device
Code: Select all
-- *** user settings below this line **
local doorSensorHelperName = '$Doorsensor' -- if first char of name is '$' device is hidden in switches tab
local doorSensorName = 'Doorsensor' -- change to the name of your doorSensor'
local secondsUntilNotification = 6
-- *** No changes required below this line **
return
{
on = {
devices = { doorSensorName, doorSensorHelperName },
},
execute = function(dz, item)
local doorSensor = dz.devices(doorSensorName)
local helper = dz.devices(doorSensorHelperName)
if item == doorSensor then
if item.active then
helper.switchOn().afterSec(secondsUntilNotification) -- come back after x seconds
end
else -- triggered by helper
if doorSensor.active then
local secondsOpened = doorSensor.lastUpdate.secondsAgo
if secondsOpened >= secondsUntilNotification then
dz.notify(doorSensorName,'Still open after ' .. secondsOpened .. ' seconds.' )
helper.switchOff().silent()
end
end
end
end
}
Re: Looking for a very simple script for a door open notification.
Posted: Tuesday 03 September 2019 19:26
by fotomann100
Hello Waaren,
thank you for your great engagement! Both scripts are working fine. The openURL-version seems to be very elegant but for me it is very difficult to understand. The version with the hidden device I understood for the most part. Surely, I'm not able to write such a script at the moment but time will tell ...
Please let me ask some questions:
1. The original script sends a lot of notifications. I don't know exactly why, but I assume, that is because the sensor triggers the script several times (I can see this in the protocol). I solved this with a variable ("notificationSent") that is set to "1" after the first notification (see lines 6, 27 and 30). It works fine, but is this the right way to do this?
2. At the moment notification is sent always, even when the door was closed again before time. I tried to solve this with a modification of line 27: I added "and item.active". This produces no errors but nevertheless it's not working. Do you have an idea what's going wrong?
For your auxiliary information: The door sensor consists two switches, with registered each other as a slave. I believe to have read somewhere in the forum that in conjunction with a script this can be problematic (see:
https://www.domoticz.com/forum/viewtopic.php?t=18654).
The script with my modifications:
Code: Select all
-- ***user settings below this line **
local doorSensorHelperName = 'Garagentor_Timer' -- if first char of name is '$' device is hidden in switches tab
local doorSensorName = 'Garagentor Status' -- change to the name of your doorSensor'
local secondsUntilNotification = 60
local notificationSent = 0
-- *** No changes required below this line **
return
{
on = {
devices = { doorSensorName, doorSensorHelperName },
},
execute = function(dz, item)
local doorSensor = dz.devices(doorSensorName)
local helper = dz.devices(doorSensorHelperName)
if item == doorSensor then
if item.active then
helper.switchOn().afterSec(secondsUntilNotification) -- come back after x seconds
end
else -- triggered by helper
if doorSensor.active then
local secondsOpened = doorSensor.lastUpdate.secondsAgo
if secondsOpened >= secondsUntilNotification and notificationSent == 0 and item.active then
dz.notify(doorSensorName,'Still open after ' .. secondsOpened .. ' seconds.' )
helper.switchOff().silent()
local notificationSent = 1
end
end
end
end
}
Re: Looking for a very simple script for a door open notification.
Posted: Tuesday 03 September 2019 21:09
by hoeby
Unlike fotomann100 i took the other script to play with.
Copied some parts out of it and added a little timer. Which makes it possible to setup a first timer moment (when the first notification needs to be send) and when the reminders need to be send. Or set the reminder value less then 20, and no reminder is send
I Always add the e-mail option. This way i can test without having all those test messages on the telegram.
When i send them on my telegram, i get an angry wife
Only need to think about how to canceling the timer.
Why:
- If i open garage, the timer 600 is started.
- i close the garage
- When the timer is at 599 seconds i open the garage, 1 second later this script is true and i get a notification.
Code: Select all
-- *** user settings below this line **
local doorSensorName = 'Garage poort' -- change to the name of your doorSensor'
local scriptVar1 = 'simpleDelayed1Notification'
local scriptVar2 = 'simpleDelayed2Notification'
local secondsUntil1eNotification = 600 -- Timer when the first notification needs to be send, in seconds.
local secondsUntil2eNotification = 120 -- Timer when the reminder notification needs to be send, after 1e notification is send, in seconds. Value must exceed value 20
local notify = true
local email = false
local mailaddress = '[email protected]'
local notifyHead = 'dzGaragePoortCheck'
-- *** No changes required below this line **
return
{
on = {
devices = { doorSensorName },
httpResponses = { scriptVar1,
scriptVar2 },
},
execute = function(dz, item)
local doorSensor = dz.devices(doorSensorName)
function Timers(sec)
local ti
sec=tonumber(sec)
if sec == nil then ti='NIL'
elseif sec >= 86400 then ti=string.sub(tostring(sec/24/3600),1,4)..' dagen'
elseif sec >= 3600 then ti=string.sub(tostring(sec/3600),1,4)..' uren'
elseif sec >= 60 then ti=string.sub(tostring(sec/60),1,4)..' minuten'
else ti=sec..' seconden'
end
return ti
end
local function retrigger1eScript(delay1)
local url = dz.settings['Domoticz url'] ..
'/json.htm?type=command¶m=addlogmessage&message=' ..
scriptVar1 .. '%20retriggered'
dz.openURL ({ url = url, callback = scriptVar1 }).afterSec(delay1) -- to come back after delay seconds
end
local function retrigger2eScript(delay2)
print (delay2)
local zero = 20
if delay2 > zero then
local url = dz.settings['Domoticz url'] ..
'/json.htm?type=command¶m=addlogmessage&message=' ..
scriptVar2 .. '%20retriggered'
dz.openURL ({ url = url, callback = scriptVar2 }).afterSec(delay2) -- to come back after delay seconds
else
print ('geen herhaling nodig')
end
end
if item.isDevice and item.active then
retrigger1eScript(secondsUntil1eNotification) -- come back after x seconds
else -- triggered by HTTPResponse
if doorSensor.active then
local secondsOpened = doorSensor.lastUpdate.secondsAgo
if secondsOpened >= secondsUntil1eNotification then
if telegram then
dz.notify(notifyHead, doorSensorName ..' staat al langer dan '.. Timers(secondsOpened) ..' open.',dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
retrigger2eScript(secondsUntil2eNotification)
end
if email then
dz.email('Garage poort staat open', ''.. doorSensorName ..' staat al langer dan '.. Timers(secondsOpened) ..' open.', ''..mailaddress..'')
retrigger2eScript(secondsUntil2eNotification)
end
end
end
end
end
}
Re: Looking for a very simple script for a door open notification.
Posted: Tuesday 03 September 2019 23:58
by waaren
fotomann100 wrote: ↑Tuesday 03 September 2019 19:26
The version with the hidden device I understood for the most part. Surely, I'm not able to write such a script at the moment but time will tell ...
Please let me ask some questions:
1. The original script sends a lot of notifications. I don't know exactly why, but I assume, that is because the sensor triggers the script several times (I can see this in the protocol). I solved this with a variable ("notificationSent") that is set to "1" after the first notification (see lines 6, 27 and 30). It works fine, but is this the right way to do this?
This is probably because the setting to 6 or 60 seconds. The value of 6 is for testing and you should change that to the amount of seconds after you want to get a notification.
Please also take note that the variables in this script are not persistent between separate runs of this script. So setting them to a value is only useful when used in the same run.
If you still encounter too frequent notifications, please send the domoticz logfile so I can try to find out what happens.
Re: Looking for a very simple script for a door open notification.
Posted: Wednesday 04 September 2019 0:00
by waaren
hoeby wrote: ↑Tuesday 03 September 2019 21:09
- When the timer is at 599 seconds i open the garage, 1 second later this script is true and i get a notification.
That should be prevented by the lastupdate.secondsago part of the code. Is that not working for you ?
Re: Looking for a very simple script for a door open notification.
Posted: Wednesday 04 September 2019 7:02
by hoeby
waaren wrote: ↑Wednesday 04 September 2019 0:00That should be prevented by the lastupdate.secondsago part of the code. Is that not working for you ?
Yesterday evening i thoughed about that part of the code. But of was to late to test. Will do the testing today.
Re: Looking for a very simple script for a door open notification. - Solved!
Posted: Wednesday 04 September 2019 19:01
by fotomann100
Hello all together,
the problem is solved!
The problem with the multiple trigger and the wrong status info despite the already closed door had the same cause: The door Sensor was created with the Master / Slave function of Domoticz. Instead of this I created two contacts. Via a simple blockly script these contacts trigger the Door Status Dummyswitch.
Thank you all for your help!
Re: Looking for a very simple script for a door open notification.
Posted: Thursday 12 September 2019 21:07
by hoeby
i noticed a problem (or it has to work that way). This is on the http script option
When opening the door and close it before the first notification is send. No problem yet.
But opening again before the delay is pasted and leave it open, longer than delay is set. Than no notification is send.
I think this is because the script is still counting down in the backgrond. When it is triggered again, the delay is not set again.
Re: Looking for a very simple script for a door open notification.
Posted: Thursday 12 September 2019 21:35
by waaren
hoeby wrote: ↑Thursday 12 September 2019 21:07
i noticed a problem (or it has to work that way). This is on the http script option
When opening the door and close it before the first notification is send. No problem yet.
But opening again before the delay is pasted and leave it open, longer than delay is set. Than no notification is send.
I think this is because the script is still counting down in the backgrond. When it is triggered again, the delay is not set again.
The logic in the http script option should work like this
1. First door open --> delay 1 triggered
2. door closed --> no action
3. 2nd door open --> delay 2 triggered
4. first delay passed --> check if door still open and no door actions between 1 and 4 -->> no notification (there were door actions)
5. 2nd delay passed --> check if door still open and no door actions between 3 and 5 -->> notification
If the script behaves in another way the described here then please check the log (add log statements if needed) to see what really happens.