08.12.2022, 13:02
Resident script with 0 sleep time. Single script can be used for multiple timers by adding more entries to the timers table at the top of the script.
Code:
if not client then
timers = {
['1/1/1'] = {
output = '1/1/2', -- binary output
delayon = 6 * 60, -- on delay time (seconds)
delayoff = 30, -- off delay time (seconds)
}
}
grp.sender = 'tm'
client = require('localbus').new(0.1)
client:sethandler('groupwrite', function(event)
local timer = timers[ event.dst ]
if timer and event.sender ~= grp.sender then
local value = tonumber(event.datahex, 16) or 0
local state = value > 0 and 'on' or 'off'
if timer.state ~= state then
timer.state = state
timer.ticks = timer['delay' .. state]
end
end
end)
end
client:loop(1)
for _, timer in pairs(timers) do
if timer.ticks then
timer.ticks = timer.ticks - 1
if timer.ticks == 0 then
grp.checkwrite(timer.output, timer.state == 'on')
timer.state = nil
timer.ticks = nil
end
end
end