This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

MQTT subscribe
#21
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.
Reply
#22
(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?
Reply
#23
Use grp.create() to create objects based on topic name in mclient.ON_MESSAGE callback.
Reply
#24
(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?
Reply
#25
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
Reply
#26
(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?
Reply
#27
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
]]
Reply
#28
(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
Reply
#29
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
Reply
#30
How do I enter authentication parameters in the MQTT script?
Reply
#31
Add this before client:connect(...)
Code:
client:login_set(username, password)
Reply
#32
thanks
Reply
#33
does Wiser og SpaceLync have MQTT compability? or just LM?
Reply
#34
They have same libraries for MQTT client
------------------------------
Ctrl+F5
Reply
#35
(23.01.2024, 08:09)Daniel Wrote: They have same libraries for MQTT client

but not Broker app?
Reply
#36
No broker app on SE store
------------------------------
Ctrl+F5
Reply
#37
(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.
Reply
#38
Broker is not a script it is a app, ask SE they have the app.
------------------------------
Ctrl+F5
Reply
#39
Question:

From another function, how can I publish a message on this open connection?
Reply
#40
See beginning of this thread, there is example.
------------------------------
Ctrl+F5
Reply


Forum Jump: