20.09.2024, 08:55
Try changing object value and the topic will appear in MQTT Explorer.
If you want to keep last topic values for other clients then you need to enable retain flag for messages. To do this replace last line in publishvalue:
With this:
Note that LM MQTT broker does not have any persistence. If the broker is restarted then retained topics are lost until new value is published.
Additionally you can modify the script to publish all mapped values after a connection to broker is established. Replace mclient.ON_CONNECT with this:
If you want to keep last topic values for other clients then you need to enable retain flag for messages. To do this replace last line in publishvalue:
Code:
mclient:publish(topic, tostring(value))
With this:
Code:
mclient:publish(topic, tostring(value), 0, true)
Note that LM MQTT broker does not have any persistence. If the broker is restarted then retained topics are lost until new value is published.
Additionally you can modify the script to publish all mapped values after a connection to broker is established. Replace mclient.ON_CONNECT with this:
Code:
mclient.ON_CONNECT = function(res, ...)
log('mqtt connect status', res, ...)
if res then
for addr, topic in pairs(object_to_mqtt) do
local value = grp.getvalue(addr)
if value ~= nil then
if type(value) == 'boolean' then
value = value and 1 or 0
end
mclient:publish(topic, tostring(value), 0, true)
end
end
for topic, _ in pairs(mqtt_to_object) do
mclient:subscribe(topic)
end
else
mclient:disconnect()
end
end