Help splitting mqtt message into parts

I am trying to move from using a pi as a sensor to a d1mini using tasmoto, i have the sensor working and publishing to a topic that works, and nodered will pick up the string. However, it comes accross as a single string rather than the parts i would need to show like temp and humidity.

"{"Time":"1970-01-01T00:00:12","AM2301":{"Temperature":23.6,"Humidity":51.0,"DewPoint":12.9},"TempUnit":"C"}"

so i need to use one of the nodes to split this message up into its parts, ive tried change and function but cant figure it out, and no matter what mix of searches i use in google i cant find the right one to get this split up. Any help... please

You are receiving a JSON String. Use the Json Node from the parser section of the palette to parse it to a javascript object. Afterwards you can use the change node to access the individual object properties.

Johannes

Capture1
Capture2
result

this results in same output but better format

The beauty is that you can now for example access the temperature if you use the change node with set msg.payload to msg.payload.AM2301.Temperature
This will give you just the temperature as a payload.
Converting it to an object gives you the option to access any of its value from a function, a change node or as a routing property in a switch node.
You can easily get the path to a certain value when you hover over it in the debug tab and click on the copy path icon.
Johannes

This is really useful to understand for working with the message object that you have now that you used the Json parser https://nodered.org/docs/user-guide/messages
it also talks about exactly this topic of JSON strings and javascript objects

1 Like

yup that got it, i had to use another change node to grab the temperature but who cares how many IT WORKS!!!!!!

1 Like

If you are using the MQTT In node then if you select the Output as Parsed JSON then it will automatically convert the string to an object so you don't need the JSON node.

2 Likes

forgot about that :see_no_evil:

Both methods work, thanks allot. Have literally been trying to sort this for 5 hours... (after like a week getting the sensor to work in the first place)
Thank you to all... much much appreciated.

If you just want to display this in a gauge or text node then the display node can be configured to select a particular part of the payload to display, so you might not need the change node. I think how to do it is described in the info panel for the nodes, I always have to look it up, I can never remember the syntax.

{{msg.payload.AM2301.Temperature}}
double curly braces

well thats me redoing all my mqtt topics to gauges... :grin:

Now if we could just make it telepathic you wouldn't need to say you needed the MQTT or Gauge nodes either. :slight_smile:

gauge nodes fair enough, but did say mqtt at the start, does this apply to any of the display nodes you can go straight from mqtt as json to the node to display that bit?

It is nothing to do with MQTT, the display node doesn't know it is coming from mqtt, it just gets an object. I don't know whether all of them have that option but certainly most do. Easy enough to find out with any particular one you are interested in. You can also set the display format if you want (number of decimal places for example).

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