Change a mqtt message property

Hi All,

Hopefully a really easy one for someone. Im still a newbe to node red. If I have a mqtt message coming into a flow of a value -200. In the debug window it looks like this.

image

How do I change the message properties is so that it looks like this below.

image

This what I have composed so far.

[{"id":"ecfdacce.ba719","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"d11f7e94.99f4","type":"inject","z":"ecfdacce.ba719","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-200","payloadType":"num","x":530,"y":280,"wires":[["5710bad6.8ddbd4","3672fb61.c48564"]]},{"id":"5710bad6.8ddbd4","type":"debug","z":"ecfdacce.ba719","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":280,"wires":[]},{"id":"3672fb61.c48564","type":"change","z":"ecfdacce.ba719","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"value","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":440,"wires":[["5710bad6.8ddbd4"]]}]

Any help would be much appreciated.

Cheers

Gareth

Take a change node and move payload to payload.value.

@mickym2 thanks for your reply. Pretty much there.

image

how do I now get the quotation marks either side of the value?

cheers

That depends on what you actually want.

If you need the output to be a JSON Object then you don't need to do anything, it's just how the sidebar displays objects. (you can see what you are showing is an object because it says object next to msg.payload

If what you need is a JSON Object encoded as a String (where the quotes are required for it to be valid) then you need to pass the output of the change node through a JSON node (this will convert Object to string or vice versa based on the input).

Also, what is the output set to on your MQTT node?

@hardillb and @grant. Long story short I was trying to publish a calculated value to a mqtt broker.

Just tried sending { value: -200} and the piece of equipment is now responding. Before I sent just -200 and it did nothing.

Also, what is the output set to on your MQTT node?
I know on a 'mqtt in' node you can set the message output to a parsed json object/buffer/string. This cant be done via a 'mqtt out' node. Maybe a something that should be added in the future?

thanks for your help :slight_smile:

MQTT essentially transmits strings only. So behind the scenes (to assist you) the MQTT out node does it's best to assist you. It's only the MQTT in node where you need be concerned.

A better solution is to use the features of MQTT v5 & provide a content type in the transmission. Then perhaps "auto" mode (or a new v5 auto mode) could do a more informed job of figuring out what the MQTT payload was intended to be. Something I have in the back of my minds "to do" list.

Just send the payload to a JSON Node - and the JS Object will be changed into a string, a JSON string

Bytes only, if they just happen to be UTF-8 characters then so be it.

Yeah, was an attempt to simplify for readers

My real point was using v5 & content type, we could be more intelligent in the auto conversion.

Something I have mulled over since adding v5 support to the MQTT nodes - a missed opportunity I feel?

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