MQTT messages and getting back the original message

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.

Can you rephrase your question ?

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:

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.

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.

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.

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

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

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

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

(This is not my week)

English please.

Is request a reserved word?

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

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

You must have send it as:

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

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.

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

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?

That's my point, you didn't send them differently.
Check the flow part where you send them to mqtt.

I can NOT

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)

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

This flow shows you exactly what NOT to do.

[{"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.

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.

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.

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.

Two options:

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

  • Use a function node:

return msg.payload;
1 Like

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:

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

1 Like

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.

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

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)?