Logic Machine Forum
MQTT subscribe - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: MQTT subscribe (/showthread.php?tid=1759)

Pages: 1 2 3


RE: MQTT subscribe - admin - 12.12.2022

You can subscribe to all topics by subscribing to #. But you will only immediately see topics that have retain flag set. Otherwise you will have to wait for something to be published to other topics.


RE: MQTT subscribe - a455115 - 13.12.2022

(12.12.2022, 16:00)admin Wrote: You can subscribe to all topics by subscribing to #. But you will only immediately see topics that have retain flag set. Otherwise you will have to wait for something to be published to other topics.

How can I bind them so that objects are created through the loop? I don't mind waiting for them to post something. Is it possible to run a script that, when a topic is spawned, creates an object and binds the values to the object against a template?


RE: MQTT subscribe - admin - 13.12.2022

Use grp.create() to create objects based on topic name in mclient.ON_MESSAGE callback.


RE: MQTT subscribe - a455115 - 14.12.2022

(13.12.2022, 08:48)admin Wrote: Use grp.create() to create objects based on topic name in mclient.ON_MESSAGE callback.

Thanks admin, helped me a lot!

I have one more question, I was able to read the topic and create an object with a name from MQTT, but every time my device is called, it creates a new object. How can I filter it so that it doesn't duplicate already created objects? That is, either to insert the already created topics into something, or to check the created objects?


RE: MQTT subscribe - admin - 14.12.2022

Use grp.find to check if an object with a given name already exists:
Code:
name = 'test object'
if not grp.find(name) then
  grp.create({ name = name, ... })
end



RE: MQTT subscribe - a455115 - 14.12.2022

(14.12.2022, 08:10)admin Wrote: Use grp.find to check if an object with a given name already exists:
Code:
name = 'test object'
if not grp.find(name) then
  grp.create({ name = name, ... })
end

Many many thanks!

One more - hopefully last - question:
How do I automatically add the entries in the mqtt_to_object_conv and object_to_mqtt tables? I also need to be able to read the name between two characters example: Floor_1_Room_302_Device_3 how do I loop through it to bind each element to part of the name?


RE: MQTT subscribe - admin - 14.12.2022

1. You can tag all objects with a common tag and populate mapping tables based on object names. Keep in mind that the script must be restarted manually when new objects are tagged so the mapping tables are updated.

2. Use string.split(str, '_'):
Code:
str = 'Floor_1_Room_302_Device_3'
chunks = str:split('_')
log(chunks)
--[[
* table:
[1]
  * string: Floor
[2]
  * string: 1
[3]
  * string: Room
[4]
  * string: 302
[5]
  * string: Device
[6]
  * string: 3
]]



RE: MQTT subscribe - a455115 - 14.12.2022

(14.12.2022, 12:49)admin Wrote: 1. You can tag all objects with a common tag and populate mapping tables based on object names. Keep in mind that the script must be restarted manually when new objects are tagged so the mapping tables are updated.

2. Use string.split(str, '_'):
Code:
str = 'Floor_1_Room_302_Device_3'
chunks = str:split('_')
log(chunks)
--[[
* table:
[1]
  * string: Floor
[2]
  * string: 1
[3]
  * string: Room
[4]
  * string: 302
[5]
  * string: Device
[6]
  * string: 3
]]

Thanks again!
Script with table works great!
I didn't figure out how to do it with a tag


RE: MQTT subscribe - admin - 15.12.2022

This example assumes that object name and topic name are the same. mqtt-status tag is for mqtt->object and mqtt-control is for object->mqtt.

Code:
mqtt_to_object = {}
mqtt_to_object_conv = {}
object_to_mqtt = {}

objs = grp.tag('mqtt-control')
for _, obj in ipairs(objs) do
  object_to_mqtt[ obj.address ] = obj.name
end

objs = grp.tag('mqtt-status')
for _, obj in ipairs(objs) do
  mqtt_to_object[ obj.name ] = obj.address
end



RE: MQTT subscribe - tomnord - 18.01.2024

How do I enter authentication parameters in the MQTT script?


RE: MQTT subscribe - admin - 18.01.2024

Add this before client:connect(...)
Code:
client:login_set(username, password)



RE: MQTT subscribe - tomnord - 18.01.2024

thanks


RE: MQTT subscribe - tomnord - 23.01.2024

does Wiser og SpaceLync have MQTT compability? or just LM?


RE: MQTT subscribe - Daniel - 23.01.2024

They have same libraries for MQTT client


RE: MQTT subscribe - tomnord - 23.01.2024

(23.01.2024, 08:09)Daniel Wrote: They have same libraries for MQTT client

but not Broker app?


RE: MQTT subscribe - Daniel - 23.01.2024

No broker app on SE store


RE: MQTT subscribe - tomnord - 23.01.2024

(23.01.2024, 11:16)Daniel Wrote: No broker app on SE store

Do you know if it is possible via scripting? To get broker working I mean.


RE: MQTT subscribe - Daniel - 23.01.2024

Broker is not a script it is a app, ask SE they have the app.


RE: MQTT subscribe - FatMax - 31.01.2024

Question:

From another function, how can I publish a message on this open connection?


RE: MQTT subscribe - Daniel - 31.01.2024

See beginning of this thread, there is example.