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:

Re: Did I leave the door open?

Post by felix63 »

What does your log show?
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 »

My log show:
2019-03-04 17:10:00.579 Status: dzVents: Info: POR: ------ Start internal script: Deuren open test2:, trigger: every 5 minutes
2019-03-04 17:10:00.602 Status: dzVents: Info: POR: Device Garage staat 0 minuten open.
2019-03-04 17:10:00.604 Status: dzVents: Info: POR: ------ Finished Deuren open test2
2019-03-04 17:15:00.576 Status: dzVents: Info: POR: ------ Start internal script: Deuren open test2:, trigger: every 5 minutes
2019-03-04 17:15:00.599 Status: dzVents: Info: POR: Device Garage staat 5 minuten open.
2019-03-04 17:15:00.599 Status: dzVents: Error (2.4.6): POR: An error occured when calling event handler Deuren open test2
2019-03-04 17:15:00.599 Status: dzVents: Error (2.4.6): POR: .../scripts/dzVents/generated_scripts/Deuren open test2.lua:35: attempt to index global 'Domoticz' (a nil value)
2019-03-04 17:15:00.599 Status: dzVents: Info: POR: ------ Finished Deuren open test2
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 »

remko2000 wrote: Monday 04 March 2019 17:17 My log show:
2019-03-04 17:10:00.579 Status: dzVents: Info: POR: ------ Start internal script: Deuren open test2:, trigger: every 5 minutes
2019-03-04 17:10:00.602 Status: dzVents: Info: POR: Device Garage staat 0 minuten open.
2019-03-04 17:10:00.604 Status: dzVents: Info: POR: ------ Finished Deuren open test2
2019-03-04 17:15:00.576 Status: dzVents: Info: POR: ------ Start internal script: Deuren open test2:, trigger: every 5 minutes
2019-03-04 17:15:00.599 Status: dzVents: Info: POR: Device Garage staat 5 minuten open.
2019-03-04 17:15:00.599 Status: dzVents: Error (2.4.6): POR: An error occured when calling event handler Deuren open test2
2019-03-04 17:15:00.599 Status: dzVents: Error (2.4.6): POR: .../scripts/dzVents/generated_scripts/Deuren open test2.lua:35: attempt to index global 'Domoticz' (a nil value)
2019-03-04 17:15:00.599 Status: dzVents: Info: POR: ------ Finished Deuren open test2
try to change Domoticz to domoticz (case matters)
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 »

Case matters indeed. I change the capital and it looks likt it works fine now! Thanx for the help. This is my code 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
}
TheJuice

Re: Did I leave the door open?

Post by TheJuice »

felix63 wrote: Friday 01 March 2019 0:01
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?
Yes sorry, I didn't receive notification of your message.
I did the following :

Code: Select all

local devicesToCheck = {
--  table with doors to check and the minutes before the first warning is given
  { ['name'] = 'Porte Entrée', 
    ['threshold'] = 1, 
    ['actionOpen'] = function(domoticz)
                      domoticz.log('Porte entrée ouverte')
                   end ,
    ['actionClose'] = function(domoticz)
                      domoticz.log('Porte entrée fermée')
                   end },
  { ['name'] = 'Porte Terrasse', 
    ['threshold'] = 1, 
    ['actionOpen'] = function(domoticz)
                      domoticz.devices('Thermostat Away Mode').switchOn()
                   end ,
    ['actionClose'] = function(domoticz)
                      domoticz.devices('Thermostat Away Mode').switchOff()
                   end },
}
-- number of times you are warned about an open door
local maxAlertCount = 5

return {
  active = true,
  
  on = {
    timer = {'every 1 minutes'},
  },
  logging = {
--  level = domoticz.LOG_INFO,
    marker = "PORTES"
  },    
--  count per door of the number of alerts per door
  data = {
    ['Porte Entrée'] = {initial=0},
    ['Porte Terrasse'] = {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 == 'Unlocked') then 
        domoticz.log('La ' .. name .. ' est ouverte depuis  ' .. minutes .. ' minutes.')
        if (minutes > threshold) and (domoticz.data[name] < maxAlertCount) then
          domoticz.data[name] = domoticz.data[name] + 1
          domoticz.notify('Porte ouverte', 'La ' .. name .. ' est ouverte depuis ' .. minutes .. ' minutes.', domoticz.PRIORITY_HIGH, domoticz.SOUND_NONE, nil, domoticz.NSS_TELEGRAM)
          deviceToCheck['actionOpen'](domoticz)
        end
      elseif (domoticz.data[name] > 0) then
        domoticz.data[name] = 0
        domoticz.notify('Porte ouverte', 'La ' .. name .. ' est refermée.', domoticz.PRIORITY_HIGH, domoticz.SOUND_NONE, nil, domoticz.NSS_TELEGRAM)
        deviceToCheck['actionClose'](domoticz)
      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 »

Great! Thanks.. great addition to the script.
TheJuice

Re: Did I leave the door open?

Post by TheJuice »

felix63 wrote: Tuesday 21 May 2019 20:17 Great! Thanks.. great addition to the script.
One thing I'd like to change in the future would be to act upon a change on the sensor instead of testing every x minutes.
But I have so many things to do in Domoticz that I'm not that willing to touch something that is already functionning :-p
Harald777
Posts: 48
Joined: Thursday 01 June 2017 8:44
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Location: Netherlands
Contact:

Re: Did I leave the door open?

Post by Harald777 »

Hi there,

Hope there is still support on this item.

I am using the script from post 1 combined with the telegram notification fix.
The script is almost working fine except for the fact that the warning notifications don''s stop after 3 times.

Code: Select all

local devicesToCheck = {
--  table with doors to check and the minutes before the first warning is given
	{ ['name'] = 'Voordeur', ['threshold'] = 5 },
	{ ['name'] = 'Zolderdeur', ['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},
        ['Zolderdeur'] = {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
}
I hope someone can tell me where I have made an error?

Greetings,

Harald
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 »

Harald777 wrote: Tuesday 03 December 2019 18:06 Hope there is still support on this item.
Can you try this ?

Code: Select all

return 
{
    active = true,

    on = 
    {
        timer = {'every 5 minutes'},
    },

    logging = 
    {
        level = domoticz.LOG_INFO,
        marker = "POR"
    },    

    data = 
    {
        VoordeurAlerts  = { initial = 0 },
        ZolderdeurAlerts = { initial = 0 },
    },

    execute = function(dz)
        --  table with names of doors to check as key, minutes before the first warning is given and max number of Alerts as values 
        local devicesToCheck = 
        {
            Voordeur = { threshold = 5 , maxAlerts = 3},
            Zolderdeur = { threshold = 10,  maxAlerts = 2 },
        }

        for name, settings in pairs(devicesToCheck) do
            local device = dz.devices(name)
            local alertCount = dz.data[name .. 'Alerts']

            if device.state == 'Open' then
                dz.log('Device ' .. name .. ' staat  ' ..  device.lastUpdate.secondsAgo .. ' seconden open.')
                local minutesOpen = device.lastUpdate.minutesAgo  
        
                if minutesOpen > settings.threshold and alertCount < settings.maxAlerts then
                    alertCount = alertCount + 1
                    dz.notify('Herinnering-open',name .. ' staat al langer dan ' .. minutesOpen .. ' minuten open.',dz.PRIORITY_HIGH)
                    dz.log('dit is waarschuwing ' .. alertCount)
                end
            elseif alertCount > 0 then
                dz.notify('Herinnering-dicht',name .. ' is weer gesloten.',dz.PRIORITY_HIGH)
                dz.log('Device ' .. name .. ' is  ' .. device.lastUpdate.secondsAgo .. ' seconden dicht.')
                alertCount = 0
            end

            dz.data[name .. 'Alerts'] = alertCount
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Harald777
Posts: 48
Joined: Thursday 01 June 2017 8:44
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Location: Netherlands
Contact:

Re: Did I leave the door open?  [SOLVED]

Post by Harald777 »

waaren wrote: Tuesday 03 December 2019 21:00 Can you try this ?

Code: Select all

return 
{
    active = true,

    on = 
    {
        timer = {'every 5 minutes'},
    },

    logging = 
    {
        level = domoticz.LOG_INFO,
        marker = "POR"
    },    

    data = 
    {
        VoordeurAlerts  = { initial = 0 },
        ZolderdeurAlerts = { initial = 0 },
    },

    execute = function(dz)
        --  table with names of doors to check as key, minutes before the first warning is given and max number of Alerts as values 
        local devicesToCheck = 
        {
            Voordeur = { threshold = 5 , maxAlerts = 3},
            Zolderdeur = { threshold = 10,  maxAlerts = 2 },
        }

        for name, settings in pairs(devicesToCheck) do
            local device = dz.devices(name)
            local alertCount = dz.data[name .. 'Alerts']

            if device.state == 'Open' then
                dz.log('Device ' .. name .. ' staat  ' ..  device.lastUpdate.secondsAgo .. ' seconden open.')
                local minutesOpen = device.lastUpdate.minutesAgo  
        
                if minutesOpen > settings.threshold and alertCount < settings.maxAlerts then
                    alertCount = alertCount + 1
                    dz.notify('Herinnering-open',name .. ' staat al langer dan ' .. minutesOpen .. ' minuten open.',dz.PRIORITY_HIGH)
                    dz.log('dit is waarschuwing ' .. alertCount)
                end
            elseif alertCount > 0 then
                dz.notify('Herinnering-dicht',name .. ' is weer gesloten.',dz.PRIORITY_HIGH)
                dz.log('Device ' .. name .. ' is  ' .. device.lastUpdate.secondsAgo .. ' seconden dicht.')
                alertCount = 0
            end

            dz.data[name .. 'Alerts'] = alertCount
        end
    end
}
Waaren,

Thanks for the advice, working flawlessly now.
Great.

Greetings, Harald
marktn
Posts: 232
Joined: Tuesday 21 April 2015 21:39
Target OS: Windows
Domoticz version:
Contact:

Re: Did I leave the door open?

Post by marktn »

Nice script and it works fine!

i tried to change something, but i need a little help. DZevents is new for me...

I have addes this:

if alertCount = '3' then
domoticz.devices('ToonApiLib - Scene').switchSelector(30)
dz.notify('Verwarming gaat lager in tempratuur',dz.PRIORITY_HIGH)

This is the error:
2020-01-18 19:34:42.268 Error: dzVents: Error: (2.4.29) error loading module 'deuropen_verwaming_uit' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/deuropen_verwaming_uit.lua':
2020-01-18 19:34:42.268 ...pts/dzVents/generated_scripts/deuropen_verwaming_uit.lua:41: 'then' expected near '='

Code: Select all

return 
{
    active = true,

    on = 
    {
        timer = {'every 2 minutes'},
    },

    logging = 
    {
        level = domoticz.LOG_INFO,
        marker = "POR"
    },    

    data = 
    {
        Deur_woonkamerAlerts  = { initial = 0 },
    },

    execute = function(dz)
        --  table with names of doors to check as key, minutes before the first warning is given and max number of Alerts as values 
        local devicesToCheck = 
        {
            Deur_woonkamer = { threshold = 5 , maxAlerts = 3},
        }

        for name, settings in pairs(devicesToCheck) do
            local device = dz.devices(name)
            local alertCount = dz.data[name .. 'Alerts']

            if device.state == 'Open' then
                dz.log('Device ' .. name .. ' staat  ' ..  device.lastUpdate.secondsAgo .. ' seconden open.')
                local minutesOpen = device.lastUpdate.minutesAgo  
        
                if minutesOpen > settings.threshold and alertCount < settings.maxAlerts then
                    alertCount = alertCount + 1
                    dz.notify('Herinnering-open',name .. ' staat al langer dan ' .. minutesOpen .. ' minuten open.',dz.PRIORITY_HIGH)
                    dz.log('dit is waarschuwing ' .. alertCount)
                end
            elseif alertCount > 0 then
                dz.notify('Herinnering-dicht',name .. ' is weer gesloten.',dz.PRIORITY_HIGH)
                dz.log('Device ' .. name .. ' is  ' .. device.lastUpdate.secondsAgo .. ' seconden dicht.')
                alertCount = 0
            end

            dz.data[name .. 'Alerts'] = alertCount
        end
    end
}
Domoticz latest stable, Intel NUC, Proxmox, Zigate, RFLink, Kaku switches, Ikea lights, Xiaomi sensors, Honeywell smoke detectors, Yeelight, Shelly, Slave Domoticz PI 3B+, Node-Red, Espeasy.
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 »

marktn wrote: Saturday 18 January 2020 19:37

Code: Select all

   if alertCount = '3' then    
change to

Code: Select all

if alertCount == '3' then   

Assignment is the basic means of changing the value of a variable or a table field:

a = "hello" .. "world"

Lua provides the following relational operators:
< > <= >= == ~=
All these operators always result in true or false.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
marktn
Posts: 232
Joined: Tuesday 21 April 2015 21:39
Target OS: Windows
Domoticz version:
Contact:

Re: Did I leave the door open?

Post by marktn »

waaren wrote: Saturday 18 January 2020 22:50
marktn wrote: Saturday 18 January 2020 19:37

Code: Select all

   if alertCount = '3' then    
change to

Code: Select all

if alertCount == '3' then   

Assignment is the basic means of changing the value of a variable or a table field:

a = "hello" .. "world"

Lua provides the following relational operators:
< > <= >= == ~=
All these operators always result in true or false.
Thank for helping! I have a error pts/dzVents/generated_scripts/deuropen_verwaming_uit.lua:54: unexpected symbol near '}'

What i want is, that after 3 warning the heating is going lower. And a notifacation what happend.

Code: Select all

return 
{
    active = true,

    on = 
    {
        timer = {'every 2 minutes'},
    },

    logging = 
    {
        level = domoticz.LOG_INFO,
        marker = "POR"
    },    

    --  table with names of doors Alerts
    data = 
    {
        Deur_woonkamerAlerts  = { initial = 0 },
    },

    execute = function(dz)
        --  table with names of doors to check as key, minutes before the first warning is given and max number of Alerts as values 
        local devicesToCheck = 
        {
            Deur_woonkamer = { threshold = 5 , maxAlerts = 3},
        }

        for name, settings in pairs(devicesToCheck) do
            local device = dz.devices(name)
            local alertCount = dz.data[name .. 'Alerts']

            if device.state == 'Open' then
                dz.log('Device ' .. name .. ' staat  ' ..  device.lastUpdate.secondsAgo .. ' seconden open.')
                local minutesOpen = device.lastUpdate.minutesAgo  
        
                if minutesOpen > settings.threshold and alertCount < settings.maxAlerts then
                    alertCount = alertCount + 1
                    dz.notify('Herinnering-open',name .. ' staat al langer dan ' .. minutesOpen .. ' minuten open.',dz.PRIORITY_HIGH)
                    dz.log('dit is waarschuwing ' .. alertCount)
                if alertCount == '3' then    
                    domoticz.devices('ToonApiLib - Scene').switchSelector(30)
                    dz.notify('Verwarming gaat lager in tempratuur',dz.PRIORITY_HIGH)
                end
            elseif alertCount > 0 then
                dz.notify('Herinnering-dicht',name .. ' is weer gesloten.',dz.PRIORITY_HIGH)
                dz.log('Device ' .. name .. ' is  ' .. device.lastUpdate.secondsAgo .. ' seconden dicht.')
                alertCount = 0
            end

            dz.data[name .. 'Alerts'] = alertCount
        end
    end
}
Domoticz latest stable, Intel NUC, Proxmox, Zigate, RFLink, Kaku switches, Ikea lights, Xiaomi sensors, Honeywell smoke detectors, Yeelight, Shelly, Slave Domoticz PI 3B+, Node-Red, Espeasy.
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 »

marktn wrote: Tuesday 21 January 2020 20:02 Thank for helping! I have a error pts/dzVents/generated_scripts/deuropen_verwaming_uit.lua:54: unexpected symbol near '}'
Made some corrections in the code and commented the lines with problems. Not tested so could be that you stil encounter something

Code: Select all

return 
{
    active = true,

    on = 
    {
        timer = {'every 2 minutes'},
    },

    logging = 
    {
        level = domoticz.LOG_INFO,
        marker = "POR"
    },    

    --  Persistent data table with names of doors Alerts 
    data = 
    {
        Deur_woonkamerAlerts  = { initial = 0 },
    },

    execute = function(dz)
        --  table with names of doors to check as key, minutes before the first warning is given and max number of Alerts as values 
        local devicesToCheck = 
        {
            Deur_woonkamer = { threshold = 5 , maxAlerts = 3},
        }

        for name, settings in pairs(devicesToCheck) do
            local device = dz.devices(name)
            local alertCount = dz.data[name .. 'Alerts']

            if device.state == 'Open' then
                dz.log('Device ' .. name .. ' staat  ' ..  device.lastUpdate.secondsAgo .. ' seconden open.',dz.LOG_INFO)
                local minutesOpen = device.lastUpdate.minutesAgo  
        
                if minutesOpen > settings.threshold and alertCount < settings.maxAlerts then
                    alertCount = alertCount + 1
                    dz.notify('Herinnering-open',name .. ' staat al langer dan ' .. minutesOpen .. ' minuten open.',dz.PRIORITY_HIGH)
                    dz.log('dit is waarschuwing ' .. alertCount,dz.LOG_INFO)
                end -- << every if block must be closed with an end statement >>
                -- if alertCount == '3' then -- << alertCount is a number not a string >>    
                if alertCount == 3 then 
                    -- domoticz.devices('ToonApiLib - Scene').switchSelector(30) -- << object domoticz does not exist. You should use dz here >>
                    dz.devices('ToonApiLib - Scene').switchSelector(30) 
                    -- dz.notify('Verwarming gaat lager in tempratuur',dz.PRIORITY_HIGH) -- << wrong syntax: must start with subject, message  >> 
                    dz.notify('Verwarming','Verwarming gaat lager in temperatuur',dz.PRIORITY_HIGH)
                end
            elseif alertCount > 0 then
                dz.notify('Herinnering-dicht',name .. ' is weer gesloten.',dz.PRIORITY_HIGH)
                dz.log('Device ' .. name .. ' is  ' .. device.lastUpdate.secondsAgo .. ' seconden dicht.',dz.LOG_INFO)
                alertCount = 0
            end

            dz.data[name .. 'Alerts'] = alertCount
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
marktn
Posts: 232
Joined: Tuesday 21 April 2015 21:39
Target OS: Windows
Domoticz version:
Contact:

Re: Did I leave the door open?

Post by marktn »

Thanks, it works!!

Mark

waaren wrote: Tuesday 21 January 2020 23:58
marktn wrote: Tuesday 21 January 2020 20:02 Thank for helping! I have a error pts/dzVents/generated_scripts/deuropen_verwaming_uit.lua:54: unexpected symbol near '}'
Made some corrections in the code and commented the lines with problems. Not tested so could be that you stil encounter something

Code: Select all

return 
{
    active = true,

    on = 
    {
        timer = {'every 2 minutes'},
    },

    logging = 
    {
        level = domoticz.LOG_INFO,
        marker = "POR"
    },    

    --  Persistent data table with names of doors Alerts 
    data = 
    {
        Deur_woonkamerAlerts  = { initial = 0 },
    },

    execute = function(dz)
        --  table with names of doors to check as key, minutes before the first warning is given and max number of Alerts as values 
        local devicesToCheck = 
        {
            Deur_woonkamer = { threshold = 5 , maxAlerts = 3},
        }

        for name, settings in pairs(devicesToCheck) do
            local device = dz.devices(name)
            local alertCount = dz.data[name .. 'Alerts']

            if device.state == 'Open' then
                dz.log('Device ' .. name .. ' staat  ' ..  device.lastUpdate.secondsAgo .. ' seconden open.',dz.LOG_INFO)
                local minutesOpen = device.lastUpdate.minutesAgo  
        
                if minutesOpen > settings.threshold and alertCount < settings.maxAlerts then
                    alertCount = alertCount + 1
                    dz.notify('Herinnering-open',name .. ' staat al langer dan ' .. minutesOpen .. ' minuten open.',dz.PRIORITY_HIGH)
                    dz.log('dit is waarschuwing ' .. alertCount,dz.LOG_INFO)
                end -- << every if block must be closed with an end statement >>
                -- if alertCount == '3' then -- << alertCount is a number not a string >>    
                if alertCount == 3 then 
                    -- domoticz.devices('ToonApiLib - Scene').switchSelector(30) -- << object domoticz does not exist. You should use dz here >>
                    dz.devices('ToonApiLib - Scene').switchSelector(30) 
                    -- dz.notify('Verwarming gaat lager in tempratuur',dz.PRIORITY_HIGH) -- << wrong syntax: must start with subject, message  >> 
                    dz.notify('Verwarming','Verwarming gaat lager in temperatuur',dz.PRIORITY_HIGH)
                end
            elseif alertCount > 0 then
                dz.notify('Herinnering-dicht',name .. ' is weer gesloten.',dz.PRIORITY_HIGH)
                dz.log('Device ' .. name .. ' is  ' .. device.lastUpdate.secondsAgo .. ' seconden dicht.',dz.LOG_INFO)
                alertCount = 0
            end

            dz.data[name .. 'Alerts'] = alertCount
        end
    end
}
Domoticz latest stable, Intel NUC, Proxmox, Zigate, RFLink, Kaku switches, Ikea lights, Xiaomi sensors, Honeywell smoke detectors, Yeelight, Shelly, Slave Domoticz PI 3B+, Node-Red, Espeasy.
IDomoticz
Posts: 5
Joined: Sunday 29 September 2019 20:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Did I leave the door open?

Post by IDomoticz »

Thanks for this great script, works perfectly.

The only thing I don't know is how to set some sort of alias for the device so my notification looks cleaner?
I like to name my devices very specific in the device section, so I know where I use the device for.
The only downside is that the names are long and not very clean for notifications.

I can of course change the notification manually, but that is not the most convenient way to do it!

Can someone help me with 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,

You could try this version of the script. Too lazy to test but should work,

Code: Select all

local devicesToCheck = {
--  table with doors to check and the minutes before the first warning is given
	{ ['name'] = 'Long Name of Device',
        ['threshold'] = 1,
        ['alias'] = 'Short Name'},
}

-- 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 = {
        ['Long Name of Device'] = {initial=0},
        },

	execute = function(domoticz)
		for i, deviceToCheck in pairs(devicesToCheck) do
			local name = deviceToCheck['name']
			local threshold = deviceToCheck['threshold']
			local alias = deviceToCheck['alias']
			local state = domoticz.devices(name).state
			local minutes = domoticz.devices(name).lastUpdate.minutesAgo
			if ( state == 'Open') then 
    			domoticz.log(alias .. ' staat  ' .. minutes .. ' minuten open.')
			    if (minutes > threshold) and (domoticz.data[name] < alertCount) then
    			    domoticz.data[name] = domoticz.data[name] + 1
    			    local alert = alias .. ' staat al langer dan ' .. minutes .. ' minuten open.'
                    domoticz.log('dit is waarschuwing #' .. tostring(domoticz.data[name]))
                end
            elseif (domoticz.data[name] > 0) then
                domoticz.notify('Security', 'Device ' .. alias .. ' is weer gesloten.', domoticz.PRIORITY_NORMAL, domoticz.SOUND_DEFAULT,'', domoticz.NSS_HTTP)
    			domoticz.log('Device ' .. alias .. ' is  ' .. minutes .. ' dicht.')
			    domoticz.data[name] = 0
			end
		end
	end
}

runy
Posts: 11
Joined: Monday 28 January 2019 15:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Did I leave the door open?

Post by runy »

Hi

I'm using the original script but the notification send by Telegram is always "1", this is my code:

Code: Select all

local devicesToCheck = {
--  table with doors to check and the minutes before the first warning is given
	{ ['name'] = 'ven-playroom', ['threshold'] = 3 },
	{ ['name'] = 'ven-adrian', ['threshold'] = 10 },
	{ ['name'] = 'ven-dormitorio', ['threshold'] = 3 },

}
-- 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 = "VEN"
    },    
--  count per door of the number of alerts per door
    data = {
        ['ven-playroom'] = {initial=0},
        ['ven-adrian'] = {initial=0},
        ['ven-dormitorio'] = {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('La ventana ' .. name .. ' lleva  ' .. minutes .. ' minutos abierta.')
			    if (minutes > threshold) and (domoticz.data[name] < alertCount) 
			    then
                    domoticz.data[name] = domoticz.data[name] + 1
                    domoticz.notify('La ventana ' .. name .. ' ha estado ' .. minutes .. ' minutos abierta.', domoticz.PRIORITY_HIGH)
                    domoticz.log('AVISO #' .. tostring(domoticz.data[name]))
                end
            elseif (domoticz.data[name] > 0) then
                domoticz.notify('La ventana ' .. name .. ' está cerrada de nuevo.', domoticz.PRIORITY_HIGH)
                domoticz.log('La ventana ' .. name .. ' está  ' .. minutes .. ' cerrada.')
			    domoticz.data[name] = 0
			end
		end
	end
}
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 »

runy wrote: Tuesday 17 November 2020 11:49 I'm using the original script but the notification send by Telegram is always "1", this is my code:
The syntax used here for domoticz.notify is wrong
From the wiki
notify(subject, message [,priority][,sound][,extra][,subsystem][,delay] ): Function. Send a notification (like Prowl). Priority can be like domoticz.PRIORITY_LOW, PRIORITY_MODERATE, PRIORITY_NORMAL, PRIORITY_HIGH, PRIORITY_EMERGENCY. extra is notification subsystem specific. For NSS_FIREBASEyou can specify the target mobile (‘midx_1’, midx_2, etc..). For sound see the SOUND constants below. subsystem can be a table containing one or more notification subsystems. See domoticz.NSS_subsystem types. Delay is delay in seconds
change the notification in your script to

Code: Select all

domoticz.notify('Door notification','La ventana ' .. name .. ' ha estado ' .. minutes .. ' minutos abierta.', domoticz.PRIORITY_HIGH)
where 'Door notification' will be the subject, 'La ventana ' .. name .. ' ha estado ' .. minutes .. ' minutos abierta.' the message and domoticz.PRIORITY_HIGH the priority.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
runy
Posts: 11
Joined: Monday 28 January 2019 15:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Did I leave the door open?

Post by runy »

Thanks mate!!!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest