17.08.2023, 13:43
(16.08.2023, 06:19)admin Wrote: Previous version did not handle zero timer values. Try this one instead:
Code:if not client then
timers = {
['1/1/1'] = {
output = '1/1/2', -- binary output
delayon = 10, -- on delay time (seconds)
delayoff = 0, -- off delay time (seconds)
}
}
grp.sender = 'tm'
function set(timer)
grp.checkwrite(timer.output, timer.state == 'on')
timer.state = nil
timer.ticks = nil
end
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
local ticks = timer['delay' .. state]
if ticks > 0 then
timer.ticks = ticks
else
set(timer)
end
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
set(timer)
end
end
end
I will try but I tried previous also with 1 second and it didn’t work.