# Dynamic topic for mqtt input

**URL:** <https://discourse.nodered.org/t/dynamic-topic-for-mqtt-input/11157>\
**Category:** General\
**Created:** [15 May 2019 10:31 UTC](https://discourse.nodered.org/t/dynamic-topic-for-mqtt-input/11157 "2019-05-15T10:31:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jandos](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@Jandos](https://discourse.nodered.org/u/Jandos)\
**Post date:** [15 May 2019 10:31 UTC](https://discourse.nodered.org/t/dynamic-topic-for-mqtt-input/11157/1 "2019-05-15T10:31:45Z")

</div>

I already read [Change mqtt-input node topic name](https://discourse.nodered.org/t/change-mqtt-input-node-topic-name/1864/17), 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](https://flows.nodered.org/node/node-red-contrib-mqtt-dynamictopic), 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!

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 May 2019 11:00 UTC](https://discourse.nodered.org/t/dynamic-topic-for-mqtt-input/11157/2 "2019-05-15T11:00:55Z")

</div>

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:

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![Jandos](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@Jandos](https://discourse.nodered.org/u/Jandos)\
**Post date:** [15 May 2019 11:13 UTC](https://discourse.nodered.org/t/dynamic-topic-for-mqtt-input/11157/3 "2019-05-15T11:13:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 May 2019 11:20 UTC](https://discourse.nodered.org/t/dynamic-topic-for-mqtt-input/11157/4 "2019-05-15T11:20:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jandos](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@Jandos](https://discourse.nodered.org/u/Jandos)\
**Post date:** [15 May 2019 11:43 UTC](https://discourse.nodered.org/t/dynamic-topic-for-mqtt-input/11157/5 "2019-05-15T11:43:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 May 2019 11:45 UTC](https://discourse.nodered.org/t/dynamic-topic-for-mqtt-input/11157/6 "2019-05-15T11:45:50Z")

</div>

Personally I think there are better applications for that purpose. Do you know about [mqtt explorer](https://github.com/thomasnordquist/MQTT-Explorer) ?

---

<div class="post-metadata">

**Author:** ![Jandos](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@Jandos](https://discourse.nodered.org/u/Jandos)\
**Post date:** [15 May 2019 11:48 UTC](https://discourse.nodered.org/t/dynamic-topic-for-mqtt-input/11157/7 "2019-05-15T11:48:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 May 2019 12:21 UTC](https://discourse.nodered.org/t/dynamic-topic-for-mqtt-input/11157/8 "2019-05-15T12:21:47Z")

</div>

> [@Jandos](#):
>
> The idea is to have an absolute generic dashboard which displays all devices publishing in mqtt.

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?

---

<div class="post-metadata">

**Author:** ![Jandos](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@Jandos](https://discourse.nodered.org/u/Jandos)\
**Post date:** [15 May 2019 12:35 UTC](https://discourse.nodered.org/t/dynamic-topic-for-mqtt-input/11157/9 "2019-05-15T12:35:38Z")

</div>

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.
