--[[ -- This is the trigger to start scene with gateway start { type = "se-start", property = "start", operator = "==", value = true, isTrigger = true } --]] -- Scene File : Door-Windows-Monitoring.lua -- Purpose : Check doors and windows at the given interval. -- Version : 0.2.6-en -- Release Date : 18 Jan. 2021 -- Trigger : autostart -- License : Copyleft {jwi} -- Either you enter all the door / window IDs in line 29, or you -- let the script run in automatic mode. Save the line 29, so -- you do not have to pick all the IDs again, local Debug = function ( color, message ) fibaro.debug(string.format('<%s style="color:%s;">%s', "span", color, message, "span") ) end local manually = false -- | true if manually then devices = {48, 122, 123, 127} --enter your device IDs here print('Checking is in manual mode.') else devices = fibaro.getDevicesID({ enabled = true, visible = true, baseType = "com.fibaro.doorWindowSensor" }) print('Checking is in automatic mode.' ) end local interval = 10 -- Interval after which the first push message is sent. local numPushMsg = 3 -- number of push messages local pushAgain = 3600 -- Activate pushMsg again after specified time. 1h local IDtempOut = 3 -- IMPORTANT! ID of your outside temperatur sensor (e.g. Yahoo) local TempOutType = 'Temperature' -- IMPORTANT! 'Temperature' or 'value' local excludedev = {259,360} --This device(s) are exclcuded temporary from check local ignoredev = {} -- This devices are never checked local begPeriod = "08:00" -- During this period, no check is performed on the local endPeriod = "20:30" -- sensors contained in the variable "excludedev". local tThreshold = 15 -- nn° Celsius local newInterval = 5 -- minutes to increase or decrease interval counter local phoneID = {22} -- mobile phone-IDs -- local siren = 398 -- Neo Coolcam optional device id for sound output after 'numPushMsg' local debug = true -- | true -- Debug messages yes/no local advdebug = false -- | true -- advanced debug local soundOn = false -- No changes needed below this line. local version = '0.2.6' local pushMsg = 1 local warning = 1 local counter = 0 local lastopen = 0 local prevwarning = 0 print("Check for open doors and windows... Version ".. version) if (fibaro.getValue(IDtempOut, TempOutType)) == nil then Debug('red', 'No outside temperature detected!') Debug('red', 'Please correct. Scene is canceled!') fibaro.abort() end local function validate(phoneID) local ios = api.get('/iosDevices') local nID = 0 for n = 1,#phoneID do for _,i in pairs(ios) do if phoneID[n] == i.id then nID = nID+1 end end --for end --for if #phoneID ~= nID then print('Warning - unknown PhoneID detected, please correct!') end end --validate() local function excludecheck(excludedev, devid) if (os.date("%H:%M", os.time())) > begPeriod and (os.date("%H:%M", os.time())) < endPeriod then -- workaround for checking time after midnight -- or (os.date("%H:%M", os.time())) > "00:00" and (os.date("%H:%M", os.time())) < "06:30" then for i=1,#excludedev do if excludedev[i] == devid then return true end end --for end return false end local function ignorecheck(ignoredev, devid) for i=1,#ignoredev do if ignoredev[i] == devid then return true end end --for return false end local function tempcheck() local outsideTemp = tonumber( fibaro.getValue(IDtempOut, TempOutType)); local stringTemp = string.format("%.2f", outsideTemp) if outsideTemp > tThreshold then currInterval = interval + newInterval elseif outsideTemp < tThreshold then currInterval = interval - newInterval else currInterval = interval end if (currInterval < 5) then currInterval = 5 end return stringTemp, currInterval end local p = validate(phoneID) local outsideTemp = tempcheck() print("Outside temperature is " .. tempcheck() .. '°C') print("Current Interval is: " .. currInterval) if not debug then print('Debug is disabled.') end; function checkOpenDW() local outsideTemp,curInterval = tempcheck() local currentDate = os.date("*t"); local datum = os.date("%d.%m.%Y") counter = counter + 1 devopen = 0 for id = 1, #devices do if (fibaro.getValue(devices[id], 'value')) == true then local name = fibaro.getName(devices[id]) local room = fibaro.getRoomNameByDeviceID(devices[id]) if excludecheck(excludedev, devices[id]) ~= true and ignorecheck(ignoredev,devices[id]) ~= true then devopen = devopen + 1 status = " Warning! " ..name.. " in room " ..room.. " is open! Temp. = " .. outsideTemp.."°" print(datum .. status) warning = warning + 1 end end if (warning == prevwarning and warning > 0) then -- Door/window was closed warning = 1; counter = 1; prevwarning = 0; pushMsg = 1; end if (warning == 0) then -- No door/window open, restart counter counter = 1; prevwarning = 0; pushMsg = 1; end if (counter > currInterval+1 and warning > 0) then -- Door/windows was opened and closed, resstart warnings counter = 1; warning = 1; prevwarning = 0; pushMsg =1; end end --for if devopen == 0 then counter = 1; warning = 1; prevwarning = 0; pushMsg =1; if debug then print('All doors and windows are closed.') end; end prevwarning = prevwarning +1 -- Door/windows open longer than interval. Send push. if counter > currInterval then if (devopen > 1) then status = status .. ' Total: '.. devopen end if pushMsg <= numPushMsg then print(datum .. ' Door/window for more than '.. curInterval ..' minutes open, send push Nr. '..pushMsg) status = status .. ',Push-Nr.: ' .. pushMsg if pushMsg == numPushMsg then status = status ..' Latest!' lastopen = devopen if soundOn then fibaro.call(398, 'turnOn') -- Siren / sounder end end pushMsg = pushMsg + 1 uxtime = os.time(dt); for k=1, #phoneID do if phoneID[k] ~= nil then fibaro.call(phoneID[k], 'sendPush', status ) end end --for end warning = 1; counter = 1; prevwarning = 0 end if advdebug then Debug('withe', 'Counter: '.. counter) end; if advdebug then Debug('withe', 'Warning: '.. warning) end; if advdebug then Debug('withe', 'PreWarning: '.. prevwarning) end; if advdebug then Debug('withe', 'PushMsg : '.. pushMsg) end; if advdebug then Debug('cyan', 'Device open: ' .. devopen) end if advdebug then Debug('cyan', 'Last open: ' .. lastopen) end if pushMsg > numPushMsg and lastopen ~= devopen then pushMsg = 1 ; lastopen = 0 end if pushMsg > numPushMsg and (os.time() - uxtime) > pushAgain and pushAgain > 0 then pushMsg = 1 end fibaro.setTimeout(60000, checkOpenDW) end --function checkOpenDW()