Dynamic topic for mqtt input

#1

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!

#2

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.

#3

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?

#4

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.

#5

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.

#6

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

#7

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

#8

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?

#9

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.