# 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:** 20\
**Page:** 1

<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:** [19 August 2020 22:43 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/1 "2020-08-19T22:43:12Z")

</div>

I am sorry, I know I have asked before, but now can't remember and when I tried to search for it here, couldn't find it.

Yes, _You_ will find it .... _as quick as_ when you try.  
I don't know why that is.

I am receiving a message from MQTT.

_Typically_ the message is built on `msg.payload.xxxx` Which I get.  
I know the new `MQTT` nodes have the ability to output the right/required format.  
But that doesn't get rid of the extra `payload` in the message structure.

How do I get it back to the original message again?

I've tried `JSON` nodes, but to no luck.

Help appreciated.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 August 2020 05:21 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/2 "2020-08-20T05:21:35Z")

</div>

Can you rephrase your question ?

---

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

</div>

Ok the _Original_ message is:

`{payload:"REQUEST",device:"TABBYCAT",topic:"POWER}`

When it comes out of the MQTT node after being sent I get:

`{"device":"TABBYCAT","payload":"REQUEST"}` (Topic implied)

So these are really sub-sets of `payload` because if I expand the message and copy the path for the device I get: `payload.device` not just `device`.

No big deal. As this is (for now) specific for a small flow, I just:

```auto
msg.device = msg.payload.device;
msg.payload = msg.payload.payload

return msg;

```

But if there were more (other) things, it would be so nice for a simple way to put things back where they were before going through MQTT.

---

<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:** [20 August 2020 06:13 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/4 "2020-08-20T06:13:48Z")

</div>

Show us output from a debug node showing what is going into mqtt out node and another showing what comes back from mqtt in node.

---

<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 06:15 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/5 "2020-08-20T06:15:11Z")

</div>

That is on a tablet and I am trying to get the screen shot coppied, but _Samsung_ have been unhelpful with how things are working.

It is painful to get it sent.

Working on alternative now.

 ![2020-08-20-15-48-34](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/77cd11291d04093e7b1051e7f061ddb5b26bc2e4.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 August 2020 06:48 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/6 "2020-08-20T06:48:34Z")

</div>

```auto
{payload:"REQUEST",device:"TABBYCAT",topic:"POWER}

```

If this is your original message, you have send originally to mqtt as:

```auto
{"payload":{"device":"TABBYCAT","payload":"REQUEST"},"topic":"POWER"}

```

as you cannot have a payload string "REQUEST" combined with a json keypair

---

<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 06:53 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/7 "2020-08-20T06:53:24Z")

</div>

(This is not my week)

> [@bakman2](#):
>
> as you cannot have a payload string "REQUEST" combined with a json keypair

English please.

Is **request** a reserved word?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 August 2020 06:57 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/8 "2020-08-20T06:57:49Z")

</div>

If you see this on the broker (as your screenshot implies):

```auto
{payload:"REQUEST",device:"TABBYCAT",topic:"POWER}

```

You must have send it as:

```auto
{"payload":{"device":"TABBYCAT","payload":"REQUEST"},"topic":"POWER"}

```

---

<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:05 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/9 "2020-08-20T07:05:58Z")

</div>

I am not trying to be difficult.

The screen shot shows you what I enter on the device.

This is what is received **directly** out of the `MQTT` node on Node-red.

```auto
{"device":"TABBYCAT","payload":"REQUEST"}

```

 ![Screenshot from 2020-08-20 17-04-30](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35235432f92346aee15e4f7e3a592c01f6381c5a.png)

Make of that as you will.

But to me after expanding the message it is:

`payload.device``payload.payload`

Again:  
Is there an easy way to take messages like that and put them back to how they were before they went through MQTT?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 August 2020 07:08 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/10 "2020-08-20T07:08:06Z")

</div>

> [@Trying\_to\_learn](#):
>
> put them back to how they were before they went through

That's my point, you didn't send them differently.  
Check the flow part where you send them to 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 07:12 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/11 "2020-08-20T07:12:38Z")

</div>

I can **NOT**

> [@bakman2](#):
>
> Check the flow part where you send them to mqtt.

Do that.

It is a SAMSUNG DEVICE that has a whole different program system in it and it has an MQTT send _node_ (poor choice in the circumstances) of its own.

I have shown you CLEARLY what it is set to do.

There is no .... Flow.

So - and I accept, as it is 99% of the time correct - I am seriously missing something.  
But I can't see what.

On a slight Tangent: Do you know how to program **Tasker**? (Android app)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 August 2020 07:15 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/12 "2020-08-20T07:15:31Z")

</div>

Got it.

The mqtt-in node produces a `msg.payload` with contents. You are sending a message: `{payload:"REQUEST",device:"TABBYCAT"}`

Result: `msg.payload.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 07:23 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/13 "2020-08-20T07:23:21Z")

</div>

This flow shows you exactly what NOT to do.

```auto
[{"id":"27f0730a.da3ecc","type":"inject","z":"d103f472.19f3c8","name":"","topic":"","payload":"message payload","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":3340,"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":650,"y":3340,"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":320,"y":3340,"wires":[["db3b98d3.d2c5a8"]]},{"id":"db3b98d3.d2c5a8","type":"mqtt out","z":"d103f472.19f3c8","name":"","topic":"temp","qos":"","retain":"","broker":"378c0403.8cda04","x":410,"y":3400,"wires":[]},{"id":"3877c78e.e86728","type":"mqtt in","z":"d103f472.19f3c8","name":"","topic":"temp","qos":"2","datatype":"auto","broker":"378c0403.8cda04","x":520,"y":3290,"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":""}]

```

What ever the Samsung device does: Good. It puts all the stuff in `msg.payload......`

For the third time:  
Is there an EASY way to propagate (reconstruct?) the message back to its original form/structure?

I have _cheated_ this time as there are only two things to move.  
But this is a trap which I keep falling for.

I've tried a `JSON` node and it isn't working.  
Well: It didn't last time I tried. But now Nick is replying, watch: It will work.  
But I'll wait to check what he says.

---

<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:25 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/14 "2020-08-20T07:25:04Z")

</div>

> [@Trying\_to\_learn](#):
>
> But if there were more (other) things, it would be so nice for a simple way to put things back where they were before going through MQTT.

We have discussed this many times @trying_to_learn.

The MQTT Out node publishes the contents of `msg.payload`.

The MQTT In node puts the received message in `msg.payload` and can optionally parse it for you if it was encoded as a JSON string.

If you want properties like `msg.device` to be published, you are responsible for moving it under `msg.payload` first. And the reverse is true - you have to move it back to `msg.devices` after receiving the message.

There's nothing else to discuss again.

---

<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:27 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/15 "2020-08-20T07:27:33Z")

</div>

I know this and the message format - as received - is honouring that.

As shown by the `debug` node.

But I am (again) asking for an easy way to restore the message.  
I am sure this was done in one of the times I asked.

But as I said in my first post: I tried looking for it and can't find the answer.

---

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

</div>

> [@Trying\_to\_learn](#):
>
> But I am (again) asking for an easy way to restore the message.

That is a meaningless question. There is no automatic way of knowing what you started with in the Samsung device, which apparently does strange things when it publishes to MQTT. All that node-red knows is what comes out of the MQTT node. If you want to restructure that data then you will have to do it manually in a Change node, for example.

---

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

</div>

> [@Trying\_to\_learn](#):
>
> I am sure this was done in one of the times I asked.

Two options:

- Add a Change node after the MQTT In node to move the properties wherever you want them

- Use a function node:

```auto
return msg.payload;

```

---

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

</div>

I think you are confusing two things here. The payload property that is part of a nodered msg object which is where many nodes nodes put the information to be passed on and a property incidentally also called payload in a json that you receive from some other device over mqtt.  
So I think you have to wrap your head around the fact that although the object you get in the payload from the mqtt node also has a property payload there is no such thing as restoring the message.  
you can easily do what you want though with a simple function:

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

```

but be aware this destroys any other properties that were in the msg object before.

Edit do as @knolleary says

---

<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:37 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/19 "2020-08-20T07:37:43Z")

</div>

Well, maybe what it does is ..... _strange_, but it is also good. Because it moves anything else you put in the message into a recognised/compatible with 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 07:41 UTC](https://discourse.nodered.org/t/mqtt-messages-and-getting-back-the-original-message/31750/20 "2020-08-20T07:41:07Z")

</div>

As I have said: I have worked around it for this with a `function` node and this code:

```auto
msg.device = msg.payload.device;
msg.payload = msg.payload.payload

return msg;

```

I get that this works.

I was just wanting to work out an _automagic_ way where any `msg.payload.x` things get moved (automagically) to just being `msg.x`

Or is it that every time I do this, I have to remember to add a `function` node (or `change`) and move all the things coming through to their original .... _names_ (path/what ever)?

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