03.01.2024, 01:48
im running the below in script to port all obj wit tag with prefix 'MQ' to mqtt it runs everytime local bus has a message,
is there a simple way to make it more efficent? is there a better way to do this? at the moment i have one resident script with a loop which manages local bus and mqtt in one place.
is there a simple way to make it more efficent? is there a better way to do this? at the moment i have one resident script with a loop which manages local bus and mqtt in one place.
Code:
function localbus_onmessage(event)
event_get_tags = grp.gettags(event.dst)
-- check if tag has MQ prefix
for _, tag in ipairs(event_get_tags) do
tag_is_MQ = tag:match('MQ (.*)')
if tag_is_MQ then
-- get data type for knx obj
event_get_obj = grp.find(event.dst)
--decode data
tx_payload = busdatatype.decode(event.datahex, event_get_obj.datatype, event.dstraw)
-- log(event.dst, tx_payload)
if type(tx_payload) == 'boolean' then
tx_payload = tx_payload and 1 or 0
end
-- extract control type from obj name
cntrl_typ = string.split(event_get_obj.name, ' ')
--log(tx_payload)
mqtt_send:publish('from_knx_tag/'.. tag_is_MQ, cntrl_typ[1] ..' '.. tx_payload)
break
end
end