Door open notification script for multiple doors

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

Moderator: leecollings

Post Reply
sciroccojc
Posts: 11
Joined: Thursday 26 March 2020 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Door open notification script for multiple doors

Post by sciroccojc »

Hello, I am trying to get a script going that will notify me when any one of my doors have been open for more than a few minutes. I found this script here viewtopic.php?t=29250. The script works well for only a single door. I have tried many different ways to get the script to work for multiple doors but I can't seem to get it working. Any help would be appreciated. I am not getting any errors in the log, I just never get the notification.

Code: Select all

-- *** user settings below this line **

local doorSensorName1 = 'ATS1'  -- change to the name of your doorSensor' 
local doorSensorName2 = 'ATS2'  -- change to the name of your doorSensor' 
local scriptVar = 'simpleDelayedNotification'
local secondsUntilNotification = 5

-- *** No changes required below this line **

return
{
    on = {
            devices = { doorSensorName1, doorSensorName2 }, 
            httpResponses = { scriptVar },
         },

    execute = function(domoticz, item)

        local doorSensor1 = domoticz.devices(doorSensorName1) 
        local doorSensor2 = domoticz.devices(doorSensorName2) 
        
        local function retriggerScript(delay)
            local url = domoticz.settings['Domoticz url'] .. 
                        '/json.htm?type=command&param=addlogmessage&message=' .. 
                        scriptVar .. '%20retriggered'
            domoticz.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 (item == doorSensor1 and doorSensor1.active) then
                local secondsOpened = doorSensor1.lastUpdate.secondsAgo 
                if secondsOpened >= secondsUntilNotification then
                    domoticz.notify(doorSensorName1,'Still open after ' .. secondsOpened .. ' seconds.' )
                end
            elseif (item == doorSensor2 and doorSensor2.active) then
                local secondsOpened = doorSensor2.lastUpdate.secondsAgo 
                if secondsOpened >= secondsUntilNotification then
                    domoticz.notify(doorSensorName2,'Still open after ' .. secondsOpened .. ' seconds.' )
                end
            end
        end
    end
}
I also tried this way...

Code: Select all

-- *** user settings below this line **

local doorSensorName1 = 'ATS1'  -- change to the name of your doorSensor' 
local doorSensorName2 = 'ATS2'  -- change to the name of your doorSensor' 
local scriptVar = 'simpleDelayedNotification'
local secondsUntilNotification = 5

-- *** No changes required below this line **

return
{
    on = {
            devices = { doorSensorName1, doorSensorName2 }, 
            httpResponses = { scriptVar },
         },

    execute = function(domoticz, item)

        local doorSensor1 = domoticz.devices(doorSensorName1) 
        local doorSensor2 = domoticz.devices(doorSensorName2) 
        
        local function retriggerScript(delay)
            local url = domoticz.settings['Domoticz url'] .. 
                        '/json.htm?type=command&param=addlogmessage&message=' .. 
                        scriptVar .. '%20retriggered'
            domoticz.openURL  ({ url = url, callback = scriptVar }).afterSec(delay)   -- to come back after delay seconds
        end
        
        if item == doorSensor1 then
            if item.isDevice and item.active then
                retriggerScript(secondsUntilNotification) -- come back after x seconds
            else -- triggered by HTTPResponse
                if doorSensor1.active then 
                    local secondsOpened = doorSensor1.lastUpdate.secondsAgo 
                    if secondsOpened >= secondsUntilNotification then
                        domoticz.notify(doorSensorName1,'Still open after ' .. secondsOpened .. ' seconds.' )
                    end
                end
            end
        elseif item == doorSensor2 then
            if item.isDevice and item.active then
                retriggerScript(secondsUntilNotification) -- come back after x seconds
            else -- triggered by HTTPResponse
                if doorSensor2.active then 
                    local secondsOpened = doorSensor2.lastUpdate.secondsAgo 
                    if secondsOpened >= secondsUntilNotification then
                        domoticz.notify(doorSensorName2,'Still open after ' .. secondsOpened .. ' seconds.' )
                    end
                end
            end
        end
    
    end
}
StephaneM60
Posts: 12
Joined: Saturday 10 November 2018 12:47
Target OS: Windows
Domoticz version:
Contact:

Re: Door open notification script for multiple doors

Post by StephaneM60 »

Hi,

For me this script cannot work because when you get the http callback in the script you check if the item is one of the door sensor, but the item is the httpcallback.

so I would try this approach instead (I have not tested the code, so let met know the error message if any)

Code: Select all

-- *** user settings below this line **

local doorSensorName1 = 'Nuki - Porte de devant Sensor'  -- change to the name of your doorSensor' 
local doorSensorName2 = 'Nuki - Porte Cuisine Sensor'  -- change to the name of your doorSensor' 
local secondsUntilNotification = 5

-- *** No changes required below this line **

return
{
    on = {
            devices = { doorSensorName1, doorSensorName2 }, 
            customEvents = { 'doorStillOpen' },
         },
     
     logging = {
		level = domoticz.LOG_ERROR,
		marker = 'DOOR NOTIFICATION',
	},

    execute = function(domoticz, item)

        if item.isDevice and item.active then
                -- door is open : check in a few seconds if it is still open
                domoticz.emitEvent('doorStillOpen', item.id).afterSec(secondsUntilNotification)
        else
            -- Get the device from the event data
            local deviceId = tonumber(item.data)
            -- Number of seconds after last event on the sensor
            local secondsOpened = domoticz.devices(deviceId).lastUpdate.secondsAgo
            -- If the door is still open and has been for some time : notify
            if domoticz.devices(deviceId).active and secondsOpened >= secondsUntilNotification then
                domoticz.notify(domoticz.devices(deviceId).name,'Still open after ' .. secondsOpened .. ' seconds.' )
            end
            
        end
    end
}
Last edited by StephaneM60 on Thursday 12 August 2021 10:59, edited 1 time in total.
DutchHans
Posts: 229
Joined: Friday 03 April 2015 20:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Germany (near dutch border)
Contact:

Re: Door open notification script for multiple doors

Post by DutchHans »

Hi, I have the script you need running for years, happy to share.

Code: Select all

dofile ( '/home/pi/domoticz/scripts/lua/speakmodule.lua')

commandArray = {}

local function runEvery10min()
   local sensor_1 = 'sensor door 1'
   local sensor_2 = 'sensor door 2'
   local sensor_3 = 'deur 3 sensor'
   Doortext = ''

commandArray = {}
local Sensorvar = 0

if ((otherdevices[sensor_1] == 'Open')) then Sensorvar = Sensorvar + 1 end
if ((otherdevices[sensor_2] == 'Open') ) then Sensorvar = Sensorvar + 2 end
if ((otherdevices[sensor_3] ~= 'Normal') ) then Sensorvar = Sensorvar + 4 end

if Sensorvar == 0 then Message = "All doors are closed"
elseif Sensorvar == 1 then Message  = "Door 1 is open"
elseif Sensorvar == 2 then Message = "Door 2 is open"
elseif Sensorvar == 3 then Message = "Door 1 and 2 are open"
elseif Sensorvar == 4 then Message = "Door 3 is open"
elseif Sensorvar == 5 then Message = "Door 1 and 3 are open"
elseif Sensorvar == 6 then Message = "Door 2 and 3 are open"
elseif Sensorvar == 7 then Message = "Door 1, 2 and 3 are open"
end

  if (Sensorvar > 0 ) then
  -- your code here
   speak(Message)
   
  end 
  
    return commandArray
end

local m = os.date('%M')
if (m % 10== 0) then
   runEvery10min()
end

return commandArray



niki_lauda
Posts: 115
Joined: Saturday 31 August 2013 14:48
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Eindhoven (NL)
Contact:

Re: Door open notification script for multiple doors

Post by niki_lauda »

DutchHans wrote: Wednesday 11 August 2021 15:38 Hi, I have the script you need running for years, happy to share.

Code: Select all

dofile ( '/home/pi/domoticz/scripts/lua/speakmodule.lua')

commandArray = {}

local function runEvery10min()
   local sensor_1 = 'sensor door 1'
   local sensor_2 = 'sensor door 2'
   local sensor_3 = 'deur 3 sensor'
   Doortext = ''

commandArray = {}
local Sensorvar = 0

if ((otherdevices[sensor_1] == 'Open')) then Sensorvar = Sensorvar + 1 end
if ((otherdevices[sensor_2] == 'Open') ) then Sensorvar = Sensorvar + 2 end
if ((otherdevices[sensor_3] ~= 'Normal') ) then Sensorvar = Sensorvar + 4 end

if Sensorvar == 0 then Message = "All doors are closed"
elseif Sensorvar == 1 then Message  = "Door 1 is open"
elseif Sensorvar == 2 then Message = "Door 2 is open"
elseif Sensorvar == 3 then Message = "Door 1 and 2 are open"
elseif Sensorvar == 4 then Message = "Door 3 is open"
elseif Sensorvar == 5 then Message = "Door 1 and 3 are open"
elseif Sensorvar == 6 then Message = "Door 2 and 3 are open"
elseif Sensorvar == 7 then Message = "Door 1, 2 and 3 are open"
end

  if (Sensorvar > 0 ) then
  -- your code here
   speak(Message)
   
  end 
  
    return commandArray
end

local m = os.date('%M')
if (m % 10== 0) then
   runEvery10min()
end

return commandArray



Nice. Where can I find information on the speakmodule.lua?
sciroccojc
Posts: 11
Joined: Thursday 26 March 2020 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Door open notification script for multiple doors

Post by sciroccojc »

Hmmnm, I can't seam to get this one working.
sciroccojc
Posts: 11
Joined: Thursday 26 March 2020 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Door open notification script for multiple doors

Post by sciroccojc »

StephaneM60 wrote: Wednesday 11 August 2021 14:54 Hi,

For me this script cannot work because when you get the http callback in the script you check if the item is one of the door sensor, but the item is the httpcallback.

so I would try this approach instead (I have not tested the code, so let met know the error message if any)

Code: Select all

-- *** user settings below this line **

local doorSensorName1 = 'ATS1'  -- change to the name of your doorSensor' 
local doorSensorName2 = 'ATS2'  -- change to the name of your doorSensor' 
local secondsUntilNotification = 5

-- *** No changes required below this line **

return
{
    on = {
            devices = { doorSensorName1, doorSensorName2 }, 
            customEvent = { 'doorStillOpen' },
         },

    execute = function(domoticz, item)

        if item.isDevice and item.active then
                -- door is open : check in a few seconds if it is still open
                domoticz.emitEvent('doorStillOpen', item.idx).afterSec(secondsUntilNotification)
        else
            -- Get the device from the event data
            local deviceId = tonumber(item.data)
            -- Number of seconds after last event on the sensor
            local secondsOpened = domoticz.devices(deviceId).lastUpdate.secondsAgo
            -- If the door is still open and has been for some time : notify
            if domoticz.devices(deviceId).active and secondsOpened >= secondsUntilNotification then
                domoticz.notify(domoticz.devices(deviceId).name,'Still open after ' .. secondsOpened .. ' seconds.' )
            end
            
        end
    end
}
I ran this code but I'm getting an error.

2021-08-11 20:07:21.764 Error: dzVents: Error: (3.1.7) ...cripts/dzVents/generated_scripts/Doors Open - Test 3.lua:25: attempt to index a nil value (field 'lastUpdate')
besix
Posts: 99
Joined: Friday 25 January 2019 11:33
Target OS: Linux
Domoticz version: beta
Location: Poland
Contact:

Re: Door open notification script for multiple doors

Post by besix »

I am using this script successfully.
You can set a different time for each door (threshold)
viewtopic.php?f=72&t=23768&p=230569#p230569
StephaneM60
Posts: 12
Joined: Saturday 10 November 2018 12:47
Target OS: Windows
Domoticz version:
Contact:

Re: Door open notification script for multiple doors

Post by StephaneM60 »

sciroccojc wrote: Thursday 12 August 2021 2:07
I ran this code but I'm getting an error.

2021-08-11 20:07:21.764 Error: dzVents: Error: (3.1.7) ...cripts/dzVents/generated_scripts/Doors Open - Test 3.lua:25: attempt to index a nil value (field 'lastUpdate')
Ok, don't know why but the .idx wasn't returning the device ID. I fixed the script (I also had a typo for the event at the top, it was missing the "s" for customEvents)

Now I tested the script and it works fine (edited my first message so you can copy the code from it)
sciroccojc
Posts: 11
Joined: Thursday 26 March 2020 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Door open notification script for multiple doors

Post by sciroccojc »

Thank you!!! It works.
sciroccojc
Posts: 11
Joined: Thursday 26 March 2020 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Door open notification script for multiple doors

Post by sciroccojc »

StephaneM60 wrote: Thursday 12 August 2021 11:03
sciroccojc wrote: Thursday 12 August 2021 2:07
I ran this code but I'm getting an error.

2021-08-11 20:07:21.764 Error: dzVents: Error: (3.1.7) ...cripts/dzVents/generated_scripts/Doors Open - Test 3.lua:25: attempt to index a nil value (field 'lastUpdate')
Ok, don't know why but the .idx wasn't returning the device ID. I fixed the script (I also had a typo for the event at the top, it was missing the "s" for customEvents)

Now I tested the script and it works fine (edited my first message so you can copy the code from it)
So, in checking further, the script is working and I am receiving the alert, but for some reason I am still getting this error in the log.

2021-08-12 13:59:21.845 Error: dzVents: Error: (3.1.7) DOOR NOTIFICATION: ...cripts/dzVents/generated_scripts/Doors Open - Test 4.lua:30: attempt to index a nil value (field 'lastUpdate')
StephaneM60
Posts: 12
Joined: Saturday 10 November 2018 12:47
Target OS: Windows
Domoticz version:
Contact:

Re: Door open notification script for multiple doors

Post by StephaneM60 »

sciroccojc wrote: Thursday 12 August 2021 20:00 So, in checking further, the script is working and I am receiving the alert, but for some reason I am still getting this error in the log.

2021-08-12 13:59:21.845 Error: dzVents: Error: (3.1.7) DOOR NOTIFICATION: ...cripts/dzVents/generated_scripts/Doors Open - Test 4.lua:30: attempt to index a nil value (field 'lastUpdate')
If you do have this error, then the notification is not sent (as the error stop the script)

The error will occur when the event "doorStillOpen" occurs with a device ID that doesn't exist, so when it's time to check the device last update it fails because the device wasn't found (and lastUpdate is a nil value)

Let me know if you still see the error tomorrow
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Door open notification script for multiple doors

Post by rrozema »

StephaneM60 wrote: Thursday 12 August 2021 21:11
sciroccojc wrote: Thursday 12 August 2021 20:00 So, in checking further, the script is working and I am receiving the alert, but for some reason I am still getting this error in the log.

2021-08-12 13:59:21.845 Error: dzVents: Error: (3.1.7) DOOR NOTIFICATION: ...cripts/dzVents/generated_scripts/Doors Open - Test 4.lua:30: attempt to index a nil value (field 'lastUpdate')
If you do have this error, then the notification is not sent (as the error stop the script)

The error will occur when the event "doorStillOpen" occurs with a device ID that doesn't exist, so when it's time to check the device last update it fails because the device wasn't found (and lastUpdate is a nil value)

Let me know if you still see the error tomorrow
Most likely the error occurs when the door closes and the customevent will have processed successfully. The logic is a little bit off because you're not properly checking the conditions. Try this to see if it fixes the error message?

Code: Select all

-- *** user settings below this line **

local doorSensorName1 = 'ATS1'  -- change to the name of your doorSensor' 
local doorSensorName2 = 'ATS2'  -- change to the name of your doorSensor' 
local secondsUntilNotification = 5

-- *** No changes required below this line **

return
{
    on = {
            devices = { doorSensorName1, doorSensorName2 }, 
            customEvent = { 'doorStillOpen' },
         },

    execute = function(domoticz, item)

        if item.isDevice then
            if item.active then
                -- door is open : check in a few seconds if it is still open
                domoticz.emitEvent('doorStillOpen', item.idx).afterSec(secondsUntilNotification)
            end
        elseif item.isCustomEvent then
            -- Get the device from the event data
            local deviceId = tonumber(item.data)
            local device = domoticz.devices(deviceId)
            if nil == device then
                domoticz.log( 'Device ' .. tostring(deviceId) .. ' not found.', domoticz.LOG_ERROR)
            else
                -- Number of seconds after last event on the sensor
                local secondsOpened = device.lastUpdate.secondsAgo
                -- If the door is still open and has been for some time : notify
                if device.active and secondsOpened >= secondsUntilNotification then
                    domoticz.notify(device.name,'Still open after ' .. secondsOpened .. ' seconds.' )
                end
            end
        end
    end
}
Fredom
Posts: 140
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Krimpen aan den IJssel
Contact:

Re: Door open notification script for multiple doors

Post by Fredom »

sciroccojc wrote: Tuesday 10 August 2021 0:39 Hello, I am trying to get a script going that will notify me when any one of my doors have been open for more than a few minutes. I found this script here viewtopic.php?t=29250. The script works well for only a single door. I have tried many different ways to get the script to work for multiple doors but I can't seem to get it working. Any help would be appreciated. I am not getting any errors in the log, I just never get the notification.

Code: Select all

-- *** user settings below this line **

local doorSensorName1 = 'ATS1'  -- change to the name of your doorSensor' 
local doorSensorName2 = 'ATS2'  -- change to the name of your doorSensor' 
local scriptVar = 'simpleDelayedNotification'
local secondsUntilNotification = 5

-- *** No changes required below this line **

return
{
    on = {
            devices = { doorSensorName1, doorSensorName2 }, 
            httpResponses = { scriptVar },
         },

    execute = function(domoticz, item)

        local doorSensor1 = domoticz.devices(doorSensorName1) 
        local doorSensor2 = domoticz.devices(doorSensorName2) 
        
        local function retriggerScript(delay)
            local url = domoticz.settings['Domoticz url'] .. 
                        '/json.htm?type=command&param=addlogmessage&message=' .. 
                        scriptVar .. '%20retriggered'
            domoticz.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 (item == doorSensor1 and doorSensor1.active) then
                local secondsOpened = doorSensor1.lastUpdate.secondsAgo 
                if secondsOpened >= secondsUntilNotification then
                    domoticz.notify(doorSensorName1,'Still open after ' .. secondsOpened .. ' seconds.' )
                end
            elseif (item == doorSensor2 and doorSensor2.active) then
                local secondsOpened = doorSensor2.lastUpdate.secondsAgo 
                if secondsOpened >= secondsUntilNotification then
                    domoticz.notify(doorSensorName2,'Still open after ' .. secondsOpened .. ' seconds.' )
                end
            end
        end
    end
}
I also tried this way...

Code: Select all

-- *** user settings below this line **

local doorSensorName1 = 'ATS1'  -- change to the name of your doorSensor' 
local doorSensorName2 = 'ATS2'  -- change to the name of your doorSensor' 
local scriptVar = 'simpleDelayedNotification'
local secondsUntilNotification = 5

-- *** No changes required below this line **

return
{
    on = {
            devices = { doorSensorName1, doorSensorName2 }, 
            httpResponses = { scriptVar },
         },

    execute = function(domoticz, item)

        local doorSensor1 = domoticz.devices(doorSensorName1) 
        local doorSensor2 = domoticz.devices(doorSensorName2) 
        
        local function retriggerScript(delay)
            local url = domoticz.settings['Domoticz url'] .. 
                        '/json.htm?type=command&param=addlogmessage&message=' .. 
                        scriptVar .. '%20retriggered'
            domoticz.openURL  ({ url = url, callback = scriptVar }).afterSec(delay)   -- to come back after delay seconds
        end
        
        if item == doorSensor1 then
            if item.isDevice and item.active then
                retriggerScript(secondsUntilNotification) -- come back after x seconds
            else -- triggered by HTTPResponse
                if doorSensor1.active then 
                    local secondsOpened = doorSensor1.lastUpdate.secondsAgo 
                    if secondsOpened >= secondsUntilNotification then
                        domoticz.notify(doorSensorName1,'Still open after ' .. secondsOpened .. ' seconds.' )
                    end
                end
            end
        elseif item == doorSensor2 then
            if item.isDevice and item.active then
                retriggerScript(secondsUntilNotification) -- come back after x seconds
            else -- triggered by HTTPResponse
                if doorSensor2.active then 
                    local secondsOpened = doorSensor2.lastUpdate.secondsAgo 
                    if secondsOpened >= secondsUntilNotification then
                        domoticz.notify(doorSensorName2,'Still open after ' .. secondsOpened .. ' seconds.' )
                    end
                end
            end
        end
    
    end
}
Hi
Maybe this script is working for you. I have been using this for a long time and it works perfectly

Code: Select all

--(dzVents)--
--Door open  and 'Thuis' is On then no telegram
--Door open  and 'Thuis' is Off then send telegram with name of the door and door is open
--Thuis is Dummy switch that is on when smartphone is at home
--Door is an Kerui D026
--Telegram is activated

local doors =
{
    'Voordeur',
    'Garagedeur',
    'Schuifpui',
    'Schuifdeur',
    --'Schuurdeur'
    --'Overkappingdeur'
    --'Testdeur',  --for testing (only a switch)
}

local allDevices = doors            -- copy the doors table to table allDevices
table.insert(allDevices, 'Thuis')   -- this will add this deviceName to the table allDevices

return
{
    on =
    {
        devices = allDevices, 
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'deur',
    },

    execute = function(dz)
        local allDoorsClosed = true
        local  Thuis = dz.devices('Thuis') --atHome
        local atHome = Thuis.active
        dz.log('state of Thuis is ' .. dz.devices('Thuis').state .. '; atHome is ' .. tostring(atHome), dz.LOG_DEBUG)

        if atHome then
            return 
        else
            for _, doorName in ipairs(doors) do
                local door = dz.devices(doorName)
                dz.log('state of ' .. doorName .. ' is ' .. door.state .. ' (' .. ( ( door.active and 'active' ) or 'not active' ) .. ')', dz.LOG_DEBUG)

                if door.state == 'Open' or door.state == 'Alarm' then -- a door is open
                    allDoorsClosed = false
                    dz.notify('Devices', 'Device ' .. doorName .. ' is geopend ')
                    break -- no need to look at the other doors if one is open.
                end
            end
        end
    end
}
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Buster - Domoticz 2022.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest