Help parsing out MQTT message

Hey everyone, I'm getting into node red and have an ESP32 broadcasting some MQTT messages for a Ruuvitag and they are received by Mosquitto broker. I then read these values into node-red via the MQTT subscribe node. I managed to get some data which looks like the following payload:

{"temperature":21.13,"humidity":51.495,"pressure":946.67,"battery":2.959,"accel_x":0.04,"accel_y":0,"accel_z":1.016,"epoch":1637785153,"txdbm":4,"move_count":242,"sequence":61988}

I'm wondering if anyone can suggest a way to parse out this data such that I can use the values individually to control a flow, for example just reading the temperature value and sending that through as a payload.

Any help is much appreciated!

First thing to know is whether you've managed to work out that the MQTT-in node will deliver that MQTT message as a long string instead of an object (which is what you want). You can fix that by changing the settings so that it automatically converts the string to JSON.

Then you have a JavaScript object on the msg.payload that you can work with. If, for example, you want the temperature, msg.payload.temperature is the property name.

By putting in a debug node onto the output of the mqtt-in node, you will be able to see exactly the property names you need. Indeed, the debug panel has small icons against each property line that will copy the full property name (though without the msg. bit since change and other nodes have that as a drop-down selection already.

2 Likes

Thank you very much kind sir! Changing the MQTT subscribe node to a parsed JSON object did the trick, I was then able to use the debug node to reference the individual objects by copying the path in the debug window.

1 Like

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