16.10.2024, 07:03
Code:
if not client then
-- input to output mapping
mapping = {
['0/1/1'] = '0/1/2',
}
-- turn output on when input is on for X seconds
ondelay = 5 * 60
-- keep output on for X seconds
offdelayaddress = '0/1/4'
timers = {}
for input, output in pairs(mapping) do
timers[ input ] = {
output = output,
state = 'off',
}
end
function timerset(timer, input)
if input then
if timer.state ~= 'ondelay' then
timer.state = 'ondelay'
timer.ticks = ondelay
end
else
grp.checkwrite(timer.output, true)
timer.state = 'offdelay'
timer.ticks = grp.getvalue(offdelayaddress)
end
end
function timertimeout(timer)
if timer.state == 'ondelay' then
timerset(timer, false)
else
grp.checkwrite(timer.output, false)
timer.state = 'off'
timer.ticks = nil
end
end
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
timerset(timer, value ~= 0)
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
timertimeout(timer)
end
end
end(16.10.2024, 05:31)admin Wrote: Timer script:
Code:if not client then -- input to output mapping mapping = { ['0/0/1'] = '0/0/6', ['0/1/1'] = '0/1/6', ['0/2/1'] = '0/2/6', } -- turn output on when input is on for X seconds ondelay = 5 * 60 -- keep output on for X seconds offdelayaddress = '11/4/1' timers = {} for input, output in pairs(mapping) do timers[ input ] = { output = output, state = 'off', } end function timerset(timer, input) if input then if timer.state ~= 'ondelay' then timer.state = 'ondelay' timer.ticks = ondelay end else grp.checkwrite(timer.output, true) timer.state = 'offdelay' timer.ticks = grp.getvalue(offdelayaddress) end end function timertimeout(timer) if timer.state == 'ondelay' then timerset(timer, false) else grp.checkwrite(timer.output, false) timer.state = 'off' timer.ticks = nil end end 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 timerset(timer, value ~= 0) 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 timertimeout(timer) end end end
I try with other adresses, but nothing happend on the bus. What I do wrong?