I'd like to store the open times for my windows/doors. I understood that I need lua tables for this purpose.
The purpose is to remember 'forgotten' open windows/doors. If the timer exceed a TIMEOUT value then an alarm etc should be fired.
I tried this:
Code: Select all
local INTERVAL = 1
local TIMEOUT = 3
local MyDevices = {'Fenster SZ','Tür Balkon OG','Tür Küche','Tür Keller','Testtür'}
local myData = {}
for i,v in ipairs(MyDevices) do
myData[v] = { initial = 0 } -- put your initial value here if there is one
end
local PROG = 'testArray'
local logToFile = true -- (Boolean) Set to true if you also wish to log to a file. It might get big by time.
local tmpLogFile = '/var/log/domoticz/Doors.log' -- Logging to a file if specified
return {
on = {
devices = MyDevices,
timer = {
'every ' .. INTERVAL .. ' minutes'
},
},
logging = {
level = domoticz.LOG_INFO,
marker = PROG
},
data = myData,
execute = function(dz, item, info)
local logheader = os.date('%Y-%m-%d %H:%M:%S ',os.time()) .. '-' .. PROG .. '-'
if item.isTimer then
dz.log('is Timer')
for i,v in pairs(dz.data.myData) do
logtext = 'i=' .. tostring(i) .. tostring(dz.data.myData[v]) .. ' minutes'
dz.log(logtext)
if dz.data.myData[i].state == 'Open' then
if dz.data.myData[v] > TIMEOUT then
logtext = 'Alarm: door is open for ' .. tostring(dz.data.myData[v]) .. ' minutes'
dz.log(logtext)
if logToFile then os.execute('echo ' .. logheader .. logtext .. ' >>' .. tmpLogFile) end
else
dz.data.myData[v] = dz.data.myData[v] + INTERVAL
end
end
end
else
dz.log('is device ' .. item.name .. ' state=' .. item.state)
if item.state == 'Open' or item.state == 'On' then
dz.log('Door was opened')
else
dz.log('Door was closed')
dz.data.myData[item.name] = 0
end
logtext = ' device=' .. item.name .. ' State= ' .. item.state .. ' ' .. tostring(myData[item.name]) .. ' minutes'
dz.log(logtext)
if logToFile then os.execute('echo ' .. logheader .. logtext .. ' >>' .. tmpLogFile) end
end
dz.utils.dumpTable(dz.data.myTable)
end
}
Line 34 is this:Error: (3.1.7) testArray: ...domoticz/scripts/dzVents/generated_scripts/testArray.lua:34: bad argument #1 to 'pairs' (table expected, got nil)
Code: Select all
for i,v in pairs(dz.data.myData) do