# Concat in mqtt topic variable plus text

**URL:** <https://discourse.nodered.org/t/concat-in-mqtt-topic-variable-plus-text/84568>\
**Category:** General\
**Created:** [15 January 2024 16:25 UTC](https://discourse.nodered.org/t/concat-in-mqtt-topic-variable-plus-text/84568 "2024-01-15T16:25:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![domingopdf](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@domingopdf](https://discourse.nodered.org/u/domingopdf)\
**Post date:** [15 January 2024 16:25 UTC](https://discourse.nodered.org/t/concat-in-mqtt-topic-variable-plus-text/84568/1 "2024-01-15T16:25:49Z")

</div>

Hi, I'm trying to publish and subscribe on several topics that have the same first part. I include this first part in a subflow using a variable. I am not able to add the final part of the topic. For example, I want the following themes:  
MicrodesysRed/f/entradasDig  
MicrodesysRed/f/salidasDig

Consulting the forum and the internet and I saw that it should be written as:

```auto
&{mqtt_topic} & "entriesDig"

```

where mqtt\_topic = MicrodesysRed/f/  
But it does not work. The variable does recognize it well.

I leave here the screenshots of the creation of the variable and the topic

![topic](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/ad5f947addfdec5fa3b5becb4ee4d97d5a317fef.png)  
 ![topic3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f906ff9cd7c69742a24ad73b5433ed6f85e8f5e6.png)  
 ![topic2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/2283eeb68048750044c3d4e1d85fdab2da82f5ad.png)

Thank you very much and greetings

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [15 January 2024 16:54 UTC](https://discourse.nodered.org/t/concat-in-mqtt-topic-variable-plus-text/84568/2 "2024-01-15T16:54:49Z")

</div>

Hi.

You can leave the topic blank and create it in a prior node, and pass the topic in msg.topic  
e.g

```auto
[{"id":"aa026e7dddedecc7","type":"subflow","name":"Subflow 1","info":"","category":"","in":[{"x":180,"y":120,"wires":[{"id":"9ab78c8c6e46d6b1"}]}],"out":[],"env":[{"name":"mqtt_topic","type":"str","value":"MicrodesysRed/f/","ui":{"label":{"en-US":"mqtt topic"},"type":"input","opts":{"types":["str","bool"]}}},{"name":"mqtt_end","type":"str","value":"test","ui":{"label":{"en-US":"mqtt ebd"},"type":"input","opts":{"types":["str"]}}}],"meta":{},"color":"#DDAA99"},{"id":"2b7f30fc05ca40a2","type":"mqtt out","z":"aa026e7dddedecc7","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"e8ba3ef5.22f4a8","x":550,"y":120,"wires":[]},{"id":"9ab78c8c6e46d6b1","type":"change","z":"aa026e7dddedecc7","name":"","rules":[{"t":"set","p":"start","pt":"msg","to":"mqtt_topic","tot":"env"},{"t":"set","p":"end","pt":"msg","to":"mqtt_end","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":60,"wires":[["261b55b08d58dc2a"]]},{"id":"261b55b08d58dc2a","type":"template","z":"aa026e7dddedecc7","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{{start}}}{{{end}}}","output":"str","x":460,"y":60,"wires":[["2b7f30fc05ca40a2","0007781504e401b1"]]},{"id":"0007781504e401b1","type":"debug","z":"aa026e7dddedecc7","name":"debug 2466","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":410,"y":200,"wires":[]},{"id":"e8ba3ef5.22f4a8","type":"mqtt-broker","name":"testb","broker":"192.168.1.25","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"6bfb6de15b27e2c1","type":"subflow:aa026e7dddedecc7","z":"d1395164b4eec73e","name":"","x":460,"y":3480,"wires":[]},{"id":"508728d4a9148935","type":"mqtt in","z":"d1395164b4eec73e","name":"","topic":"MicrodesysRed/f/test","qos":"2","datatype":"auto-detect","broker":"e8ba3ef5.22f4a8","nl":false,"rap":true,"rh":0,"inputs":0,"x":260,"y":3560,"wires":[["d3e3a8c179685bc6"]]},{"id":"d3e3a8c179685bc6","type":"debug","z":"d1395164b4eec73e","name":"debug 2465","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":3560,"wires":[]},{"id":"51a2de387bd8320c","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":3480,"wires":[["6bfb6de15b27e2c1"]]}]

```

There are other ways but this is using low code option.

---

<div class="post-metadata">

**Author:** ![domingopdf](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@domingopdf](https://discourse.nodered.org/u/domingopdf)\
**Post date:** [15 January 2024 17:14 UTC](https://discourse.nodered.org/t/concat-in-mqtt-topic-variable-plus-text/84568/3 "2024-01-15T17:14:41Z")

</div>

What you gave me works great, thank you very much for the quick response, however, I don't want the second part to be a variable, I need it to be a text.

![topic4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/3/93aeee13eed9bcfb34703822e11313a346f0859c.png)

Thanks!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [15 January 2024 17:38 UTC](https://discourse.nodered.org/t/concat-in-mqtt-topic-variable-plus-text/84568/4 "2024-01-15T17:38:24Z")

</div>

Then delete {{{end}}} and add the text you require  
`{{{start}}}anytextyouwish`

You could drop the change node to, as the template uses env vars( i forgot)  
`{{{env.mqtt_topic}}}anytextyouwish`

---

<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:** [15 March 2024 17:39 UTC](https://discourse.nodered.org/t/concat-in-mqtt-topic-variable-plus-text/84568/5 "2024-03-15T17:39:14Z")

</div>

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