# How to create MQTT In nodes with variable topics

**URL:** <https://discourse.nodered.org/t/how-to-create-mqtt-in-nodes-with-variable-topics/97537>\
**Category:** General\
**Tags:** mqtt\
**Created:** [10 June 2025 19:45 UTC](https://discourse.nodered.org/t/how-to-create-mqtt-in-nodes-with-variable-topics/97537 "2025-06-10T19:45:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![egibson940](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/egibson940/32/101144_2.png) [@egibson940](https://discourse.nodered.org/u/egibson940)\
**Post date:** [10 June 2025 19:45 UTC](https://discourse.nodered.org/t/how-to-create-mqtt-in-nodes-with-variable-topics/97537/1 "2025-06-10T19:45:43Z")

</div>

Currently I have a subflow with a MQTT in node that receives all sensor messages and I have to filter for the one I want based on the subflow property/environment variable sensorName.

Instead of subscribing the MQTT In node to all sensor messages and then filtering, how can I subscribe it to only the sensor 'sensorName'. Is there a way for me to set the MQTT in topic to something like  
qolsys/binary\_sensor/${sensorName}/state

Thank you.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6e1a02caf82a887c0144019c24623b6a5d300ba9.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/23e6cba2e80f1e7d4d677d103394c3b19a55aa55.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 June 2025 20:04 UTC](https://discourse.nodered.org/t/how-to-create-mqtt-in-nodes-with-variable-topics/97537/2 "2025-06-10T20:04:55Z")

</div>

> [@egibson940](#):
>
> qolsys/binary\_sensor/${sensorName}/state

This interpolated format is supported if the input field is an env var. Regular fields can only support `${envvar}` see [environment-variables docs](https://nodered.org/docs/user-guide/environment-variables) for details

Since you are putting the mqtt node inside a subflow, you could set it to the value of ${topic} and in the subflow template setup, you add a topic field and allow env var values.

  

Alternatively , you can dynamically subscribe to any topic you desire at runtime (inside or outside of a subflow). There is built in help describing this. Also search the forum for dynamic subscribe mqtt.

---

<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:** [10 June 2025 20:06 UTC](https://discourse.nodered.org/t/how-to-create-mqtt-in-nodes-with-variable-topics/97537/3 "2025-06-10T20:06:27Z")

</div>

Welcome to the forum. 🙂

I have a fairly extreme example of this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b7daa9ebea3398b521d4acf558bf1f92fa0550e8.png)

I would normally do that as a function node instead of 6 core nodes - but it demonstrates a point.

It reads a context variable and processes a set of MQTT topics from that and starts listening to them all. With outputs going to a web page.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 September 2025 20:06 UTC](https://discourse.nodered.org/t/how-to-create-mqtt-in-nodes-with-variable-topics/97537/4 "2025-09-08T20:06:28Z")

</div>

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