05.08.2024, 04:47
I have written this script for surveillance of alarms. It will only generate one alarm if an Object is not updated within a range of time. You can use it as Resident or Scheduled Script. It takes every object with the tag "Alarm". Change the "controlltime" to your desire.
And this script that will actively send a read request to check if alarms are still available. It will take every objekt with the tag "Alarm". This is designed to be used as resedent or scheduled script.
Code:
--This script is for Object surveillance passivley with the updatetime property
--Alarms will be updated on change!
alarms = grp.tag('Alarm') --add the Tag or Tags of the Object that need surveillance
controlltime = 30 --Allowed Time between recieved Telegramms in Seconds
--initializing Tables for Alarming
currentstate = {}
previousstate = storage.get("previousstate") or {}
now = os.time()
--Iteration throgh the alarms Table to get the update time of every Object
for index, alarm in ipairs(alarms) do
lastrecieved = alarm.updatetime
local online
--calculating the Timedelta and comparing it with our Variable
if (now - lastrecieved) > controlltime then
online = false
else
online = true
end
-- Store the current status in the currentstatus table
currentstate[alarm.id] = {id = alarm.id, comment = alarm.comment, online = online}
-- Compare the current status with the previous status
if previousstate[alarm.id] == nil or currentstate[alarm.id].online ~= previousstate[alarm.id].online then
if not currentstate[alarm.id].online then
alert("Alarm " .. currentstate[alarm.id].comment .. " nicht verfügbar")
else
alert("Alarm " ..currentstate[alarm.id].comment .. " verfügbar")
end
end
end
-- Log the current status
-- log(currentstate)
-- Save the current status to persistent storage as previous status
storage.set("previousstate", currentstate)
And this script that will actively send a read request to check if alarms are still available. It will take every objekt with the tag "Alarm". This is designed to be used as resedent or scheduled script.
Code:
-- Variables as Tag
alarms = grp.tag('Alarm')
currentstatus = {}
previousstate = storage.get("previousstate") or {}
-- Iterate over all alarms
for index, alarm in ipairs(alarms) do
value, responding = grp.readvalue(alarm.address, readtimeout)
local online
if responding == "timeout" then
online = false
else
online = true
end
-- Store the current status in the currentstatus table
currentstatus[alarm.id] = {id = alarm.id, name = alarm.name, online = online}
-- Compare the current status with the previous status
if previousstate[alarm.id] == nil or currentstatus[alarm.id].online ~= previousstate[alarm.id].online then
if not currentstatus[alarm.id].online then
alert("Alarm " .. currentstatus[alarm.id].name .. " nicht verfügbar")
end
end
end
-- Log the current status
--log(currentstatus)
-- Save the current status to persistent storage as previous status
storage.set("previousstate", currentstatus)