# Inject and MQTT input topics - using variables

**URL:** <https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134>\
**Category:** General\
**Created:** [18 March 2019 15:52 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134 "2019-03-18T15:52:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [18 March 2019 15:52 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134/1 "2019-03-18T15:52:19Z")

</div>

This is most likely a stupid question, but I have a number of devices, identical in all but name which receive input from injects and from MQTT input. Is it possible to use, say, flow variables for topics in these two, rather than fixed strings?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 March 2019 16:15 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134/2 "2019-03-18T16:15:38Z")

</div>

I don't believe so, no.

What I do is make sure that all similar data uses a parent topic. So perhaps all the data from physical devices uses `DEVICES/<deviceid>` for example. Then I can simply listen for `DEVICES/+` and get all of the direct descendent data on that master topic. I can then sort out different paths for different devices in Node-RED. You could go another layer deep if, for example, you needed to split by device hub and then by id.

Does that make sense?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 March 2019 16:20 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134/3 "2019-03-18T16:20:09Z")

</div>

`Devices/#` I think.

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [18 March 2019 16:31 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134/4 "2019-03-18T16:31:46Z")

</div>

It would make sense but for the way I use my topics - devicex/fromesp - and on mqtt outputs I send topic devicex/toesp

Do I don't have a way to use descendent data/

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 March 2019 17:13 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134/5 "2019-03-18T17:13:15Z")

</div>

> [@Colin](#):
>
> `Devices/#` I think.

That will give you a full path so you need to take care with that.

`DEVICE/#` would give you

DEVICE/thing1  
and  
DEVICE/thing1/something

`DEVICE/+`

would only give you

DEVICE/thing1

Sometimes quite useful.

> **[MQTT Topics, Wildcards, & Best Practices – MQTT Essentials: Part 5](https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices/)**
>
> Learn about MQTT Topics, naming conventions, MQTT $SYS topics, MQTT Prefix, MQTT Topic Wildcards, and more in this MQTT Essentials article.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [18 March 2019 17:13 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134/6 "2019-03-18T17:13:40Z")

</div>

I _think_ you can set the mqtt topic string as an ENV variable... so the same flow could run on multiple servers setup with different environments.

But if you need to listen for different device topics within a single node-red instance, use the wildcard topic string `#` and then use a `switch` node to route to named devices.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 March 2019 17:14 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134/7 "2019-03-18T17:14:07Z")

</div>

> [@scargill](#):
>
> It would make sense but for the way I use my topics - devicex/fromesp - and on mqtt outputs I send topic devicex/toesp
> 
> Do I don't have a way to use descendent data/

Sorry, I can't think of another way.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 March 2019 17:30 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134/8 "2019-03-18T17:30:11Z")

</div>

> [@TotallyInformation](#):
>
> `DEVICE/#` would give you  
> DEVICE/thing1  
> and  
> DEVICE/thing1/something

Exactly, you said "listen for `DEVICES/+` and get all of the direct descendent data on that master topic" which is what the `#` gives. `DEVICE/thing1/something` is a direct descendent of `DEVICE` isn't it? Though perhaps by 'direct' you meant only one level, which I suppose it could mean, perhaps you are right. Anyway the important thing is that the operation has been clarified.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 March 2019 17:31 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134/9 "2019-03-18T17:31:34Z")

</div>

> [@Colin](#):
>
> `DEVICE/thing1/something` is a direct descendent of `DEVICE` isn't it?

Urm, no, that's an indirect descendent. 🙂 Sorry for not being clear.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 March 2019 17:35 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134/10 "2019-03-18T17:35:42Z")

</div>

> [@TotallyInformation](#):
>
> Urm, no, that's an indirect descendent

I don't think so. In family relationships I believe an indirect descendant would be via a cousin or marriage or similar

> <https://english.stackexchange.com/questions/61559/meaning-of-direct-descendant-as-opposed-to-indirect-descendant>

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 March 2019 20:36 UTC](https://discourse.nodered.org/t/inject-and-mqtt-input-topics-using-variables/9134/11 "2019-03-18T20:36:58Z")

</div>

Haha, I bow to your superior search skills 🙂

Well, hopefully everyone now understands what I meant anyway.
