# \[solved\] Publishing dynamic MQTT topics

**URL:** <https://discourse.nodered.org/t/solved-publishing-dynamic-mqtt-topics/11888>\
**Category:** General\
**Created:** [5 June 2019 20:22 UTC](https://discourse.nodered.org/t/solved-publishing-dynamic-mqtt-topics/11888 "2019-06-05T20:22:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Eric](https://discourse.nodered.org/u/Eric)\
**Post date:** [5 June 2019 20:22 UTC](https://discourse.nodered.org/t/solved-publishing-dynamic-mqtt-topics/11888/1 "2019-06-05T20:22:10Z")

</div>

Hello,  
I have an input node that gives me a JSON with lots of keys and values. I'm able to extract the "deveui" value and the "payload" value. I'd like to publish this as a new MQTT message that is has a topic name of the value for "deveui" and a message that is the value for "payload".

I'm able to extract the two individually. But I'm unsure about how to create a new payload as input to the MQTT publish node so that the MQTT node takes dynamic topic and payloads.

I hope this image explains is well enough.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2438badc10972d400c9ef992bb6260c06058cee8.png)

I tried something like this for the input function to the MQTT node, but it's incorrect.

```auto
var mynewout;
mytopic = msg.deveui;
mypayload = msg.payload;

mynewout.topic = mytopic;
mynewout.payload = mypayload;

return mynewout;

```

Edit: Closing the loop in case someone else runs into this, I initialize the new variable to msg and the MQTT publishes work correctly now. So like this

```auto
var mynewout=msg;
mytopic = msg.deveui;
mypayload = msg.payload;

mynewout.topic = mytopic;
mynewout.payload = mypayload;
msg.topic = mytopic;
msg.payload = mypayload;
return msg;

```

---

<div class="post-metadata">

**Author:** ![nlecaude](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nlecaude/32/25_2.png) [@nlecaude](https://discourse.nodered.org/u/nlecaude)\
**Post date:** [5 June 2019 20:26 UTC](https://discourse.nodered.org/t/solved-publishing-dynamic-mqtt-topics/11888/2 "2019-06-05T20:26:07Z")

</div>

How about

```auto
msg.topic = msg.deveui;
return msg;

```

Also make sure you mqtt out node has no topic set as it'll override the incoming topic.

---

<div class="post-metadata">

**Author:** ![Eric](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Eric](https://discourse.nodered.org/u/Eric)\
**Post date:** [5 June 2019 20:29 UTC](https://discourse.nodered.org/t/solved-publishing-dynamic-mqtt-topics/11888/3 "2019-06-05T20:29:08Z")

</div>

> [@nlecaude](#):
>
> Also make sure you mqtt out node has no topic set as it'll override the incoming topic.

Yes, I left the MQTT node's topic blank, so it should take on the input node's topic. Trying what you wrote, I still get the same error msg, " cannot set property 'topic'"

---

<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:** [5 June 2019 20:44 UTC](https://discourse.nodered.org/t/solved-publishing-dynamic-mqtt-topics/11888/4 "2019-06-05T20:44:30Z")

</div>

Do you mean you have a Function node containing only those two lines and you see that error? Are you sure it is the function node that is generating the error?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [5 June 2019 21:47 UTC](https://discourse.nodered.org/t/solved-publishing-dynamic-mqtt-topics/11888/5 "2019-06-05T21:47:50Z")

</div>

Hi @Eric

The problem with the function code you have is that you declare `mynewout` as a variable, but you don't initialise it to anything - it is undefined. That means you cannot set the `topic` and `payload` properties and why you get the error message you do - it literally cannot set the `topic` property of an undefined value.

The suggestion from @nlecaude will have the same end result - returning a message with the same payload, but the topic now set to whatever `msg.deveui` was. This is generally a better approach than creating a new object - it will preserve any other msg properties.

---

<div class="post-metadata">

**Author:** ![Eric](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Eric](https://discourse.nodered.org/u/Eric)\
**Post date:** [7 June 2019 20:16 UTC](https://discourse.nodered.org/t/solved-publishing-dynamic-mqtt-topics/11888/6 "2019-06-07T20:16:08Z")

</div>

Thanks. Initializing it solves the issue, and now I'm getting the correct MQTT pubs.

---

<div class="post-metadata">

**Author:** ![samynathan17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/samynathan17/32/49923_2.png) [@samynathan17](https://discourse.nodered.org/u/samynathan17)\
**Post date:** [25 October 2021 10:00 UTC](https://discourse.nodered.org/t/solved-publishing-dynamic-mqtt-topics/11888/7 "2021-10-25T10:00:51Z")

</div>

Hi ,  
I trying the same thing but not working for me

retrieve results for Postgres database and send message to MQTT publisher

![image|690x352](upload://1625a98SoExT

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/7/07b0c6acc95a07c88baa37571461c0b300378baa.png)  
cRTA90p6JL6Hzbc.png)

---

<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:** [25 October 2021 10:25 UTC](https://discourse.nodered.org/t/solved-publishing-dynamic-mqtt-topics/11888/8 "2021-10-25T10:25:36Z")

</div>

Welcome to the forum @samynathan17

I don't think your question is related to the one in this thread, which is two years old anyway. Please start a new thread to ask your question. Note, however that you either have to specify the topic in `msg.topic` or in the mqtt node if it is fixed. You can't send it an array and expect it to sort it out. Send your array through a split node to split it into individual messages, (or do that earlier in the flow) and then make sure the topic is in msg.topic and the data is in msg.payload.

I will close this thread now.

---

<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:** [25 October 2021 10:25 UTC](https://discourse.nodered.org/t/solved-publishing-dynamic-mqtt-topics/11888/9 "2021-10-25T10:25:56Z")

</div>


