Efficiently subscribe to multiple MQTT topics using one MQTT node

Hey. I need to subscribe to multiple MQTT topics using a single MQTT node. Is that a good way to simplify the flow as I do not want to create a MQTT subscribtion node for every remote device that I have.

I might have 50 remote devices that I need to listen to, and I certainly dont want to create 50 individual MQTT nodes.

[{"id":"ff173687.543bf8","type":"mqtt in","z":"df177cb8.27882","name":"","topic":"device1/number_to_pick","qos":"2","datatype":"auto","broker":"2727c5a5.a4fb6a","x":350,"y":400,"wires":[["c5854ac5.fde828"]]},{"id":"9e91a191.4a7a9","type":"debug","z":"df177cb8.27882","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":400,"wires":[]},{"id":"c5854ac5.fde828","type":"function","z":"df177cb8.27882","name":"Generate SQL","func":"//add the values to the msg so they can be debugged and used later\nquantity = msg.payload\n\n//msg.topic=\"UPDATE pack_to_light SET Quantity = 0 WHERE Device = 'device1'\"\nmsg.topic=`UPDATE pack_to_light SET Quantity = '${quantity}' WHERE Device = 'device1'`\n//msg.topic=`REPLACE INTO pack_to_light (Device,Item,Serial) VALUES ('${msg.device}','${msg.item}',${msg.serial})`;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":400,"wires":[["9e91a191.4a7a9","97fe0f98.1e2a3"]]},{"id":"97fe0f98.1e2a3","type":"mysql","z":"df177cb8.27882","mydb":"97be0e96.67231","name":"Add item to database","x":800,"y":480,"wires":[[]]},{"id":"2727c5a5.a4fb6a","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"workshop/status","birthQos":"1","birthRetain":"true","birthPayload":"online","closeTopic":"workshop/status","closeQos":"1","closeRetain":"true","closePayload":"offline","willTopic":"workshop/status","willQos":"1","willRetain":"true","willPayload":"disconnected"},{"id":"97be0e96.67231","type":"MySQLdatabase","z":"","name":"","host":"localhost","port":"3306","db":"test","tz":""}]

So this is a flow for 1 MQTT subscribe node. I am just listening and ocasionally a device sends a message. After receiving a message, I need to update MYSQL table.

quantity = msg.payload

msg.topic=`UPDATE pack_to_light SET Quantity = '${quantity}' WHERE Device = 'device1'`

return msg;

How can I change the current nodes to listen for lets say 20 devices? (devices1,devices2,devices3,devices4 .......) As you can see now, I am only listening to one device:

Also, what would happen if multiple devices send a mqtt message at the same time?

You can use wildcards + and #

If you had structured your topics device/1/xxxxx, device/2/xxxxx, device/n/xxxxx etc, you could subscribe to device/+/xxxxx

As it stands, you could subscribe to +/xxxxx but then you will also get bedroom/xxxxx, garage/xxxxx etc.

If that is not an issue, then there is a solution.


This is something that often hits people later down the line, who didnt read up on MQTT topic best practices. (not saying that is the case for you - just illustrating my point below)

1 Like

I assume I would need to change the structure of my topics for this solution since I have arranged them as following:




I only need to subscribe to "number_to_pick" topic for all devices

I have downloaded digitaloak-mqtt library which allows an input to be connected to mqtt subscribe node ( for the original mqtt subscribe node you cannot do that).

I would assume this would not work?

Not if you subscribe to +/number_to_pick and you will get all 4 - however, my point was if you have kitchen/number_to_pick you will ALSO get that. Thats why topic convention should be good.

That is an option but it is still multiple subscriptions under the hood. It is better to have good topic structure.

Look into homie convention and alternative mqtt topic conventions - they have put far more consideration into topic structure than I can express here.

1 Like

You can use one mqtt node with a topic of +/# you will get everything, you still need to handle all devices yourself, but as @Steve-Mcl indicated - better set it up with a certain convention.

I have root topics based on their source/location and in there grouped by device, ie.


Pretty sure you can make a whole study out of it, each device as a root topic does not seem really flexible.

I got it. Thank you for you help guys!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.