Dynamic topic for mqtt input

I already read Change mqtt-input node topic name, but could not use it.

I need an MQTT node, which is able, to subscribe to a variable topic. I want to use it within a flow, so i need an input and an output for this node, as I do not want to keep listening to the topic.
I also tried the dynamictopic module, but it was not working. I could not deploy properly, due to an error, which states, that the nodes from the downloaded module are somehow missing.

I hope someone can help!

1 Like

For the dynamictopic node, you can raise an issue on github. I think this is caused by some internal node-red changes over time and how to deal with credentials.

As an intermediate solution, you can use 1 mqtt node, subscribe to # you will receive everything and then dynamically filter what you need like:

[{"id":"1887d627.63360a","type":"mqtt in","z":"3be588bc.0b684","name":"","topic":"#","qos":"2","datatype":"auto","broker":"228872c0.fd94d6","x":484,"y":330,"wires":[["f403c56f.ac6b88"]]},{"id":"5a0b212c.3fd128","type":"debug","z":"3be588bc.0b684","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":798,"y":330,"wires":[]},{"id":"f403c56f.ac6b88","type":"switch","z":"3be588bc.0b684","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"topic1","vt":"str"},{"t":"eq","v":"topic2","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":622,"y":330,"wires":[["5a0b212c.3fd128"],[]]},{"id":"228872c0.fd94d6","type":"mqtt-broker","z":"","name":"mqtt","broker":"10.0.0.202","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]

You can filter dynamically based on variables if needed.

1 Like

When I subscribe to # IĀ“ll get like a thousand messages on the spot, isnĀ“t that to much for my network on the long term?

Specificity is always better, it won't hurt your network that much, networks talk more than you might think.
You need to wonder why you would need dynamic topics however.

The idea is to have an absolute generic dashboard which displays all devices publishing in mqtt. but IĀ“ll never know how many or which devices will be there. ThatĀ“s why I need to include the subscribtion inside of a flow and using the data created before to subscribe to the right device and use itĀ“s data in the following nodes.

Personally I think there are better applications for that purpose. Do you know about mqtt explorer ?

Yeah thatĀ“s absolutely true, but sometimes you canĀ“t choose :sweat_smile:

If you don't subscribe using the # how will you know when a new device starts using mqtt so you can add it to your list and then use it in your generic dashboard?

I used a # as the initial entry point to do exactly that, but my new idea is to subscribe to literally everything, and to save all that in the flow object. Hopefully i can get a nice structrure with that.