Using the change node

I am trying to extract one part of a mqtt message and use it as a payload in a node.

The incoming payload is {time:"00:02"} and i would like to use just the 00:02 for my new payload. I tried the change node by setting SET msg.payload to msg.payload.time and the debug comes back as undefined.

Any help would be much appreciated.

thanks

Hi @jgerardi

the first thing to check is whether the message is a JSON string, or a JavaScript object - you can do that with the Debug sidebar. If its a JSON string, then pass it through a JSON node to parse it to a JavaScript object and then you'll be able to access the individual parts.

This guide in the docs provides more information on working with messages - https://nodered.org/docs/user-guide/messages

Or if it is coming from the mqtt node then you can tick the box to convert it from JSON automatically

When i place the mqtt in node and connect to to a debug node, the message returned shows as a string.

cmnd/son4ch/timer1 : msg.payload : string[14]
"{time:"20:00"}"

I cant seem to figure out how to extract just the time "20:00" from this and display it in a text node.
Can anyone give me some sort of example or help with this please?

JG

You need to know a little about javascript to fully understand this.

The data you are seeing is a string ...

but the format of that string looks like JSON (JSON is a string representation of a JS object)

If it were an object you could access the value simply by accessing msg.payload.time and you would get 20:00

I recommend you read up a bit on javascript objects and JSON - but to get you moving, either pass the value through a JSON node or set the MQTT node to return a JS object.

Did you try @Colin's suggestion?
Screenshot-24

That was suggested to me earlier but when i choose "a parsed JSON object", i get

msg : string[27]

"Failed to parse JSON string"

If it is of length 27 then it has not got what you previously showed (which was length 14). Feed the string into a debug node and also into a JSON node. When the data is valid it will be converted to an object by the JSON node, but if the longer invalid string appears you will get the invalid json message from the JSON node and can look in the debug node to see what it is.

Im lost. I have tried all of these suggestions. I even went to node-red guide and read chapter 3. I followed their examples to the letter and i am still not getting it. I even went to the node red description and copied and imported to a new flow as follows:

[{"id":"6ec4dcef.913b24","type":"mqtt-broker","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":""},{"id":"6304660c.9cfb98","type":"mqtt in","name":"","topic":"noderedlecture/sensor","broker":"6ec4dcef.913b24","x":190.0994415283203,"y":150.09091186523438,"z":"a2a2c218.5d5d4","wires":[["d0760869.2f89f8"]]},{"id":"d0760869.2f89f8","type":"json","name":"","x":330.6988220214844,"y":92.18182373046875,"z":"a2a2c218.5d5d4","wires":[["f1f1bca2.0e0e4"]]},{"id":"f1f1bca2.0e0e4","type":"debug","name":"","active":true,"console":"false","complete":"false","x":448.0994415283203,"y":155.09091186523438,"z":"a2a2c218.5d5d4","wires":}]

I made all of the necessary changes for my broker, etc. What i am getting is this:

If i remove the JSON node i get this.

Can anyone get an idea as to what this unexpected character is?

I can see that the quotes round analyse and value are 'smart' quotes rather than normal double quotes. Look carefully at the debug output and you will see that they are different at each end and different to the surrounding double quotes. Where are those coming from?

that is the exact flow that is on used in the node red guide.

You can find the node-red description of this flow at:

https://raw.githubusercontent.com/SenseTecnic/nrguideflows/master/lesson3/3-1_mqqtmessages.json

I am publishing the message with MQTT.fx to a mosquitto broker. What i am publishing is {“analyze”:false, “value”:10}

ok, i gotcha. I had to read it over a hundred times or so before is sunk in. I will work backwards from here and see if get anywhere. I changed the quotes and published the message and it is working now.

Thsnks

1 Like

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