Did I leave the door open?  [SOLVED]

Moderator: leecollings

User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Did I leave the door open?

Post by felix63 »

A DzVents script to check to see if doors have been left open for too long. Both the number of doors and the threshold for reporting on a door are easily changed my adjusting the values in the table at the beginning of the script. The table 'devicesToCheck' needs to contain the exact names of your door switches. To prevent you going nuts the warnings will stop once the amounts of warning per door exceeds the variable alerCount.

Code: Select all


local devicesToCheck = {
--  table with doors to check and the minutes before the first warning is given
	{ ['name'] = 'Voordeur', ['threshold'] = 3 },
	{ ['name'] = 'Garagedeur', ['threshold'] = 10 },
	{ ['name'] = 'Achterdeur', ['threshold'] = 3 },
	{ ['name'] = 'Provisiekast deur', ['threshold'] = 10 },
	{ ['name'] = 'Bergingdeur', ['threshold'] = 10 },
}
-- number of times you are warned about an open door
local alertCount = 3

return {
	active = true,
	
    on = {
        timer = {'every 5 minutes'},
    },
    logging = {
--        level = domoticz.LOG_INFO,
        marker = "POR"
    },    
--  count per door of the number of alerts per door
    data = {
        ['Voordeur'] = {initial=0},
        ['Garagedeur'] = {initial=0},
        ['Achterdeur'] = {initial=0},
        ['Provisiekast deur'] = {initial=0},
        ['Bergingdeur'] = {initial=0},
        },

	execute = function(domoticz)
		for i, deviceToCheck in pairs(devicesToCheck) do
			local name = deviceToCheck['name']
			local threshold = deviceToCheck['threshold']
			local state = domoticz.devices(name).state
			local minutes = domoticz.devices(name).lastUpdate.minutesAgo
			if ( state == 'Open') then 
                domoticz.log('Device ' .. name .. ' staat  ' .. minutes .. ' minuten open.')
			    if (minutes > threshold) and (domoticz.data[name] < alertCount) then
                    domoticz.data[name] = domoticz.data[name] + 1
                    domoticz.notify('Device ' .. name .. ' staat al langer dan ' .. minutes .. ' minuten open.', domoticz.PRIORITY_HIGH)
                    domoticz.log('dit is waarschuwing #' .. tostring(domoticz.data[name]))
                end
            elseif (domoticz.data[name] > 0) then
                domoticz.notify('Device ' .. name .. ' is weer gesloten.', domoticz.PRIORITY_HIGH)
                domoticz.log('Device ' .. name .. ' is  ' .. minutes .. ' dicht.')
			    domoticz.data[name] = 0
			end
		end
	end
}
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: Did I leave the door open?

Post by felix63 »

Updated the script above to give a notification if the door is closed again....
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: Did I leave the door open?

Post by hoeby »

I used your nice script.
But on the first try it didn't work.
I am using telegram for notification. When the IF = TRUE, i only got a "1" in the telegram message.
Therefor i changed your script to this

Code: Select all

if (minutes > threshold) then
                    domoticz.data[name] = domoticz.data[name] + 1
                    domoticz.notify('Herinnering-open',
                                     name .. ' staat al langer dan ' .. minutes .. ' minuten open.',
                                     domoticz.PRIORITY_HIGH)
                    domoticz.log('dit is waarschuwing ' .. tostring(domoticz.data[name]))
                end
            elseif (domoticz.data[name] > 0) then
                domoticz.notify('Herinnering-dicht',
                                 name .. ' is weer gesloten.', 
                                 domoticz.PRIORITY_HIGH)
What changed?:
I added 'Herinnering-open', and 'Herinnering-dicht', to the notify message.
Personal thing, i removed the devices tekst in de notify script, this had nothing to do with the problem i noticed.
Don't know if this adding gives problems to other notification options. But on telegram this works, now i nicely get "name .. ' staat al langer dan ' .. minutes .. ' minuten open. or name .. ' is weer gesloten.
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
vwtune
Posts: 19
Joined: Wednesday 24 January 2018 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Nederland
Contact:

Re: Did I leave the door open?

Post by vwtune »

Thanks for your script.

only the alertcount don't work. if i change the number in 4, 5 or whatever, it still gives me more messages that the door is open, till i close the door, then the alerts stopts. What is wrong with the script?
TheJuice

Re: Did I leave the door open?

Post by TheJuice »

Hi,

Works like a charm, thanks.
I wonder if it would be easy to add an array of actions to take for each door when they are open for too long and are closed again.

For instance :
  • If the entrance door is open for too long, send an alert.
  • If the terrace door is open for too long, turn off the heating, and turn it on again once the door gets closed again...
Cheers
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: Did I leave the door open?

Post by hoeby »

Add the code rules, unter the "THEN" word. And fill in the "Another switch" to the switch that needs to be controlled
Hope you get it to work, otherwise post it, then i will try to help.

Code: Select all

domoticz.devices('Another switch').switchOn()
or

Code: Select all

domoticz.devices('Another switch').switchOff()
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
TheJuice

Re: Did I leave the door open?

Post by TheJuice »

Hi,
I know how I could do it dirty in the "then" part of the if(), but I would have to test for each type of device :
if (device == "Entrance door") {
domoticz.devices('Another switch').switchOn()
}
elseif (device == "Terrace door") {
send Notification
}

But I wanted to know if I could do something like that :
actions = {
['Entrance door'] = {domoticz.devices('Another switch').switchOn()},
['Terrace door'] = {send Notification},
},

And then :
domoticz.actions[name].apllyAction...

Do you think it's feasible ?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Did I leave the door open?

Post by waaren »

TheJuice wrote: Monday 26 November 2018 18:00 Hi,
I know how I could do it dirty in the "then" part of the if(), but I would have to test for each type of device :
if (device == "Entrance door") {
domoticz.devices('Another switch').switchOn()
}
elseif (device == "Terrace door") {
send Notification
}

But I wanted to know if I could do something like that :
actions = {
['Entrance door'] = {domoticz.devices('Another switch').switchOn()},
['Terrace door'] = {send Notification},
},

And then :
domoticz.actions[name].apllyAction...

Do you think it's feasible ?
Yes, this is possible. You can add a function to a Lua table and call that function just like any other function.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
TheJuice

Re: Did I leave the door open?

Post by TheJuice »

Hi,

Thanks, I'll dig a little deeper :-)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Did I leave the door open?

Post by waaren »

TheJuice wrote: Tuesday 27 November 2018 8:53 Hi,

Thanks, I'll dig a little deeper :-)
Send you a PM
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: Did I leave the door open?

Post by remko2000 »

I used this script also:

Code: Select all

local devicesToCheck = {
--  table with doors to check and the minutes before the first warning is given
	{ ['name'] = 'Voordeur', ['threshold'] = 10 },
	{ ['name'] = 'Garage', ['threshold'] = 10 },
	{ ['name'] = 'Achterdeur', ['threshold'] = 10 },
	{ ['name'] = 'Provisiekast deur', ['threshold'] = 10 },
	{ ['name'] = 'Bergingdeur', ['threshold'] = 10 },
}
-- number of times you are warned about an open door
local alertCount = 3

return {
	active = true,
	
    on = {
        timer = {'every 5 minutes'},
    },
    logging = {
--        level = domoticz.LOG_INFO,
        marker = "POR"
    },    
--  count per door of the number of alerts per door
    data = {
        ['Voordeur'] = {initial=0},
        ['Garage'] = {initial=0},
        ['Achterdeur'] = {initial=0},
        ['Provisiekast deur'] = {initial=0},
        ['Bergingdeur'] = {initial=0},
        },

	execute = function(domoticz)
		for i, deviceToCheck in pairs(devicesToCheck) do
			local name = deviceToCheck['name']
			local threshold = deviceToCheck['threshold']
			local state = domoticz.devices(name).state
			local minutes = domoticz.devices(name).lastUpdate.minutesAgo
			if ( state == 'Open') then 
                domoticz.log('Device ' .. name .. ' staat  ' .. minutes .. ' minuten open.')
			    if (minutes > threshold) and (domoticz.data[name] < alertCount) then
                    domoticz.data[name] = domoticz.data[name] + 1
                    domoticz.notify('Device ' .. name .. ' staat al langer dan ' .. minutes .. ' minuten open.', domoticz.PRIORITY_HIGH)
                    domoticz.log('dit is waarschuwing #' .. tostring(domoticz.data[name]))
                end
            elseif (domoticz.data[name] > 0) then
                domoticz.notify('Device ' .. name .. ' is weer gesloten.', domoticz.PRIORITY_HIGH)
                domoticz.log('Device ' .. name .. ' is  ' .. minutes .. ' dicht.')
			    domoticz.data[name] = 0
			end
		end
	end
}
Works fine, only problem is that ' alerCount' won't work. I keep receiving messages. How do I adjust this?
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: Did I leave the door open?

Post by felix63 »

Hi,

Did you try running the script with logging turned on? I have the very same script still running. It stops notifying me when alertcount has been reached. Could it be that there is something wrong with the data variables used for keeping track of the number of alerts? Do the variables in the data section have identical names as the names of the doors being tested?

Otherwise, send me the script as you are running it.

Cheers
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: Did I leave the door open?

Post by remko2000 »

This is my code:

Code: Select all

local devicesToCheck = {
--  table with doors to check and the minutes before the first warning is given
	{ ['name'] = 'Voordeur', ['threshold'] = 10 },
	{ ['name'] = 'Garage', ['threshold'] = 2 },
	{ ['name'] = 'Achterdeur', ['threshold'] = 10 },
}
-- number of times you are warned about an open door
local alertCount = 3

return {
	active = true,
	
    on = {
        timer = {'every 5 minutes'},
    },
    logging = {
--        level = domoticz.LOG_INFO,
        marker = "POR"
    },    
--  count per door of the number of alerts per door
    data = {
        ['Voordeur'] = {initial=0},
        ['Garage'] = {initial=0},
        ['Achterdeur'] = {initial=0},
        },

	execute = function(domoticz)
		for i, deviceToCheck in pairs(devicesToCheck) do
			local name = deviceToCheck['name']
			local threshold = deviceToCheck['threshold']
			local state = domoticz.devices(name).state
			local minutes = domoticz.devices(name).lastUpdate.minutesAgo
			if ( state == 'Open') then 
                domoticz.log('Device ' .. name .. ' staat  ' .. minutes .. ' minuten open.')
			    if (minutes > threshold) then
                    domoticz.data[name] = domoticz.data[name] + 1
                    domoticz.notify('Herinnering-open',
                                     name .. ' staat al langer dan ' .. minutes .. ' minuten open.',
                                     domoticz.PRIORITY_HIGH)
                    domoticz.log('dit is waarschuwing ' .. tostring(domoticz.data[name]))
                end
            elseif (domoticz.data[name] > 0) then
                domoticz.notify('Herinnering-dicht',
                                 name .. ' is weer gesloten.', 
                                 domoticz.PRIORITY_HIGH)
                domoticz.log('Device ' .. name .. ' is  ' .. minutes .. ' dicht.')
			    domoticz.data[name] = 0
			end
		end
	end
}
Works great. I get every 5 minutes a message from Prowl.
Problem is: it doesn't stop. I think 3 messages is enough.

What do you mean with
Do the variables in the data section have identical names as the names of the doors being tested?
?

The doornames are corresponding. Do I have to add somewhere else variables?
TheJuice

Re: Did I leave the door open?

Post by TheJuice »

In my case, notifications stop after the threshhold has been reached...
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: Did I leave the door open?

Post by remko2000 »

yes but in my case not. Why doesn't work the threshold in my case. What can be the problem?
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: Did I leave the door open?

Post by felix63 »

I suppose something goes wrong with your data variable. Otherwise I can not explain the inconsistent behavior where it works fine for some users but not for you. Do you use other scripts that use persistent data?

Did you run with logging enabled? Uncomment the line
level = domoticz.LOG_INFO,
and please post the lines from your log marked with ‘POR’.

What hardware are you running on?
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: Did I leave the door open?

Post by felix63 »

TheJuice wrote: Thursday 28 February 2019 11:48 In my case, notifications stop after the threshhold has been reached...
Btw did you adapt the script with variable action stored in tables as you mentioned couple of posts back?
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: Did I leave the door open?

Post by remko2000 »

Or right. I uncommand the log.

If I see the log in domoticz with "POR" with the door open (cold :-))I get this:
2019-03-01 15:10:00.534 Status: dzVents: Info: POR: ------ Start internal script: Deuren open test2:, trigger: every 5 minutes
2019-03-01 15:10:00.573 Status: dzVents: Info: POR: Device Garage staat 28 minuten open.
2019-03-01 15:10:00.574 Status: dzVents: Info: POR: dit is waarschuwing 9
2019-03-01 15:10:00.577 Status: dzVents: Info: POR: ------ Finished Deuren open test2
2019-03-01 15:15:00.138 Status: dzVents: Info: POR: ------ Start internal script: Deuren open test2:, trigger: every 5 minutes
2019-03-01 15:15:00.158 Status: dzVents: Info: POR: Device Garage staat 33 minuten open.
2019-03-01 15:15:00.158 Status: dzVents: Info: POR: dit is waarschuwing 10
2019-03-01 15:15:00.160 Status: dzVents: Info: POR: ------ Finished Deuren open test2
2019-03-01 15:20:00.259 Status: dzVents: Info: POR: ------ Start internal script: Deuren open test2:, trigger: every 5 minutes
2019-03-01 15:20:00.278 Status: dzVents: Info: POR: Device Garage staat 38 minuten open.
2019-03-01 15:20:00.278 Status: dzVents: Info: POR: dit is waarschuwing 11
2019-03-01 15:20:00.280 Status: dzVents: Info: POR: ------ Finished Deuren open test2
It looks like the timer don't work.But I don't understand why.

My hardware is a raspberry 3 master en a raspberry 3 slave with a zwavecontroller (with doorcontacts). The slave send the status of my doors to my master. On the master I run this script.
Btw did you adapt the script with variable action stored in tables as you mentioned couple of posts back?
What do you mean? The script a copied earlier is the script I use.
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: Did I leave the door open?

Post by felix63 »

One can easily overlook the obvious. Change the line that reads:

Code: Select all

if (minutes > threshold) then
to

Code: Select all

if (minutes > threshold) and (Domoticz.data[name] < alertCount) then

And your problem will be solved! :D
remko2000
Posts: 167
Joined: Thursday 28 December 2017 14:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Broek op Langedijk
Contact:

Re: Did I leave the door open?

Post by remko2000 »

I changed this rule. My code is now:

Code: Select all

local devicesToCheck = {
--  table with doors to check and the minutes before the first warning is given
	{ ['name'] = 'Voordeur', ['threshold'] = 10 },
	{ ['name'] = 'Garage', ['threshold'] = 2 },
	{ ['name'] = 'Achterdeur', ['threshold'] = 10 },
}
-- number of times you are warned about an open door
local alertCount = 3

return {
	active = true,
	
    on = {
        timer = {'every 5 minutes'},
    },
    logging = {
       level = domoticz.LOG_INFO,
        marker = "POR"
    },    
--  count per door of the number of alerts per door
    data = {
        ['Voordeur'] = {initial=0},
        ['Garage'] = {initial=0},
        ['Achterdeur'] = {initial=0},
        },

	execute = function(domoticz)
		for i, deviceToCheck in pairs(devicesToCheck) do
			local name = deviceToCheck['name']
			local threshold = deviceToCheck['threshold']
			local state = domoticz.devices(name).state
			local minutes = domoticz.devices(name).lastUpdate.minutesAgo
			if ( state == 'Open') then 
                domoticz.log('Device ' .. name .. ' staat  ' .. minutes .. ' minuten open.')
			    if (minutes > threshold) and (Domoticz.data[name] < alertCount) then
                    domoticz.data[name] = domoticz.data[name] + 1
                    domoticz.notify('Herinnering-open',
                                     name .. ' staat al langer dan ' .. minutes .. ' minuten open.',
                                     domoticz.PRIORITY_HIGH)
                    domoticz.log('dit is waarschuwing ' .. tostring(domoticz.data[name]))
                end
            elseif (domoticz.data[name] > 0) then
                domoticz.notify('Herinnering-dicht',
                                 name .. ' is weer gesloten.', 
                                 domoticz.PRIORITY_HIGH)
                domoticz.log('Device ' .. name .. ' is  ' .. minutes .. ' dicht.')
			    domoticz.data[name] = 0
			end
		end
	end
}
Now I get only one messages (garages is closed) an after this nothing more (the door was closed).
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests