# MQTT messages and getting back the original message

**URL:** https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750
**Category:** General
**Created:** [19 August 2020 22:43 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750 "2020-08-19T22:43:12Z")
**Posts on this page:** 11
**Page:** 2

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [20 August 2020 07:42 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/21 "2020-08-20T07:42:35Z")

</div>

> [@Trying\_to\_learn](#):
>
> I was just wanting to work out an _automagic_ way where any `msg.payload.x` things get moved (automagically) to just being `msg.x`

But this is what both @knolleary

> [@knolleary](#):
>
> `return msg.payload;`

and me just showed you how to do:

> [@JGKK](#):
>
> ```auto
> var newMsg = msg.payload; 
> return newMsg;
> 
> ```

which does the same thing.  
Just use either in a function after the mqtt node and you will have a msg object where any `msg.payload.xxx`is now `msg.xxx`.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 August 2020 07:52 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/22 "2020-08-20T07:52:53Z")

</div>

(Some one shoot me!)

I posted a flow of what to NOT do in post 12.

So then I added a `function` node to give me this:

```auto
[{"id":"27f0730a.da3ecc","type":"inject","z":"d103f472.19f3c8","name":"","topic":"","payload":"message payload","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":3270,"wires":[["13c865eb.e3f5ba"]]},{"id":"e8c8383b.82e6f","type":"debug","z":"d103f472.19f3c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":560,"y":3310,"wires":[]},{"id":"13c865eb.e3f5ba","type":"change","z":"d103f472.19f3c8","name":"","rules":[{"t":"set","p":"part1","pt":"msg","to":"part 1","tot":"str"},{"t":"set","p":"part2","pt":"msg","to":"this is part 2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":150,"y":3320,"wires":[["db3b98d3.d2c5a8"]]},{"id":"db3b98d3.d2c5a8","type":"mqtt out","z":"d103f472.19f3c8","name":"","topic":"temp","qos":"","retain":"","broker":"378c0403.8cda04","x":320,"y":3320,"wires":[]},{"id":"3877c78e.e86728","type":"mqtt in","z":"d103f472.19f3c8","name":"","topic":"temp","qos":"2","datatype":"utf8","broker":"378c0403.8cda04","x":430,"y":3270,"wires":[["4360889e.602bf"]]},{"id":"4360889e.602bf","type":"function","z":"d103f472.19f3c8","name":"","func":"var newMsg = msg.payload; \nreturn newMsg;","outputs":1,"noerr":0,"x":560,"y":3270,"wires":[["e8c8383b.82e6f"]]},{"id":"378c0403.8cda04","type":"mqtt-broker","z":"","name":"MQTT HOST","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"true","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

Now, it doesn't work at all.

I've tried a couple of settings in the `MQTT IN` node but I get `Function tried to send a message of type string` in the `function` node.

So, sorry, just to establish a working version.....

Could you check what I have done wrong?

---

<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: [20 August 2020 07:59 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/23 "2020-08-20T07:59:50Z")

</div>

I can't look at your flow as I'm away from my computer this week.

But my guess is you have not configured the MQTT In node to parse the message as JSON.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 August 2020 08:05 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/24 "2020-08-20T08:05:07Z")

</div>

Thanks Nick.

Yes, it is set to `parse JSON`

I did stuff up what I posted as the code though.

Small-ish detail. Now corrected, but still not working.

This is the best code to now.

```auto
[{"id":"27f0730a.da3ecc","type":"inject","z":"d103f472.19f3c8","name":"","topic":"","payload":"this is the message payload","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":3270,"wires":[["13c865eb.e3f5ba"]]},{"id":"e8c8383b.82e6f","type":"debug","z":"d103f472.19f3c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":560,"y":3310,"wires":[]},{"id":"13c865eb.e3f5ba","type":"change","z":"d103f472.19f3c8","name":"","rules":[{"t":"set","p":"payload.payload","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload.part1","pt":"msg","to":"part 1","tot":"str"},{"t":"set","p":"payload.part2","pt":"msg","to":"this is part 2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":150,"y":3320,"wires":[["db3b98d3.d2c5a8","9b6b1e2b.5fa24"]]},{"id":"db3b98d3.d2c5a8","type":"mqtt out","z":"d103f472.19f3c8","name":"","topic":"temp","qos":"","retain":"","broker":"378c0403.8cda04","x":320,"y":3320,"wires":[]},{"id":"3877c78e.e86728","type":"mqtt in","z":"d103f472.19f3c8","name":"","topic":"temp","qos":"2","datatype":"json","broker":"378c0403.8cda04","x":430,"y":3270,"wires":[["4360889e.602bf"]]},{"id":"4360889e.602bf","type":"function","z":"d103f472.19f3c8","name":"","func":"var newMsg = msg.payload; \nreturn newMsg;","outputs":1,"noerr":0,"x":560,"y":3270,"wires":[["e8c8383b.82e6f"]]},{"id":"9b6b1e2b.5fa24","type":"debug","z":"d103f472.19f3c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":320,"y":3380,"wires":[]},{"id":"378c0403.8cda04","type":"mqtt-broker","z":"","name":"MQTT HOST","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"true","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [20 August 2020 08:07 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/25 "2020-08-20T08:07:47Z")

</div>

You are aware that the mqtt node will only send what is in the `msg.payload`?  
As Nick already stated here:

> [@knolleary](#):
>
> The MQTT Out node publishes the contents of `msg.payload` .

So you're `msg.part1` and `msg.part2`will never be send and as you `msg.payload`is a string the function after the mqtt in will give you this error as it will only work on objects.  
You have to move any msg object properties that you want to be send over mqtt to the `msg.payload`.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 August 2020 08:08 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/26 "2020-08-20T08:08:26Z")

</div>

Yeah, sorry. Please look at the latest code I posted.  
Still isn't working. ☹

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [20 August 2020 08:11 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/27 "2020-08-20T08:11:55Z")

</div>

Yes because you cant use a change node like this. You are trying to set the property `msg.payload.payload` but you cant do that because `msg.payload` is a string and you cant set properties of a string so again only your initial string that is in `msg.payload` gets send and causes the error in the function.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 August 2020 08:16 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/28 "2020-08-20T08:16:25Z")

</div>

Ok.

I'll try setting the parts all in the node.

I set all parts like this:

```auto
[{"id":"13c865eb.e3f5ba","type":"change","z":"d103f472.19f3c8","name":"","rules":[{"t":"set","p":"payload.payload","pt":"msg","to":"This is the payload","tot":"str"},{"t":"set","p":"payload.part1","pt":"msg","to":"part 1","tot":"str"},{"t":"set","p":"payload.part2","pt":"msg","to":"this is part 2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":150,"y":3320,"wires":[["db3b98d3.d2c5a8","9b6b1e2b.5fa24"]]}]

```

I'm now trying with a `function` node.

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [20 August 2020 08:17 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/29 "2020-08-20T08:17:42Z")

</div>

```auto
[{"id":"467f1710.509fc","type":"inject","z":"447386dd.6b884","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"payload\":\"this is a payload\",\"part1\":\"this is part 1\",\"part2\":\"this is part 2\"}","payloadType":"json","x":370,"y":580,"wires":[["dc3f0cc5.c7a74","d9e9a6a1.a61c6"]]},{"id":"94b1c130.dc32e8","type":"debug","z":"447386dd.6b884","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":810,"y":580,"wires":[]},{"id":"dc3f0cc5.c7a74","type":"mqtt out","z":"447386dd.6b884","name":"","topic":"temp","qos":"","retain":"","broker":"4b4790c.bc5cf7","x":570,"y":590,"wires":[]},{"id":"9d9242b2.d81f3","type":"mqtt in","z":"447386dd.6b884","name":"","topic":"temp","qos":"2","datatype":"json","broker":"4b4790c.bc5cf7","x":680,"y":540,"wires":[["a0f89673.98d73"]]},{"id":"a0f89673.98d73","type":"function","z":"447386dd.6b884","name":"","func":"var newMsg = msg.payload; \nreturn newMsg;","outputs":1,"noerr":0,"x":810,"y":540,"wires":[["94b1c130.dc32e8"]]},{"id":"d9e9a6a1.a61c6","type":"debug","z":"447386dd.6b884","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":570,"y":650,"wires":[]},{"id":"4b4790c.bc5cf7","type":"mqtt-broker","z":"","name":"","broker":"pipspi1","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]

```

Look at this

**Edit**

> [@Trying\_to\_learn](#):
>
> I'm now trying with a `function` node.

Yes a function node would have to be used if you wanted to manipulate a message with existing properties that you want to move to `msg.payload`. Pretty much the reverse of what the function after the mqtt in node does. Something like this before an mqtt out node:

```auto
var newMsg = {payload:msg};
return newMsg;

```

would send the whole msg object over mqtt.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [20 August 2020 08:20 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/30 "2020-08-20T08:20:30Z")

</div>

Ok. I was doing it with a `function` node.

Sorry I missed the _magic_ solution:

```auto
var newMsg = msg.payload; 
return newMsg;

```

Shall document it for future use.

---

<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: [3 September 2020 08:20 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/31 "2020-09-03T08:20:32Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750.md?page=1)
