11.05.2023, 05:59
Try this. If something is not working add log() calls to check what the values are in the called functions. pcall is not needed as grp functions do not stop execution in case of an error, they simply return nil. grp.checkwrite does not write to non-existing objects so checking if object exists beforehand is not needed.
Code:
local inputgroup = {}
local groupobjects = {}
for i = 1, 60 do
groupobjects[i] = {}
for j = 11, 241, 10 do
local addr = i .. '/2/' .. j
local value = grp.getvalue(addr)
if value ~= nil then
inputgroup[addr] = i
groupobjects[i][addr] = value
end
end
end
local function updategroup(group)
local objects = groupobjects[group]
local groupon = false
for addr, value in pairs(objects) do
if value then
groupon = true
break
end
end
local controladdress = group .. '/2/0'
local controlenabled = grp.getvalue(controladdress)
if controlenabled then
local changestate = not groupon
for i = 11, 161, 10 do
local address3 = group .. '/3/' .. i
local address5 = group .. '/5/' .. (i + 7)
grp.checkwrite(address3, changestate)
grp.checkwrite(address5, changestate)
end
end
end
client = require('localbus').new(0.1)
client:sethandler('groupwrite', function(event)
local addr = event.dst
local group = inputgroup[addr]
if group then
local value = tonumber(event.datahex, 16) ~= 0
groupobjects[group][addr] = value
updategroup(group)
end
end)
while true do
client:loop(1)
end