Separate MQTT JSON message

Hello guys, I'm developing an IoT project and I'm sending data from my ESP32 to an MQTT broker and I want to build a dasboard on Node-red. I'm sending it in JSON form, because I read that this is the best form when it comes to MQTT. How do I separate the variables that are inside JSON? For example, I will have readings from temperature, humidity and air quality sensors, all coming together in JSON. I want to separate this information to set up a dasboard .. Follow what I have done so far, my sensors haven't arrived yet, that's why the strange numbers.

Thanks!!

1 Like

Set the MQTT In node to return a Parsed JSON object (it is in the node configuration). That will parse the JSON string and convert it to a javascript object.

I see you are writing the data as an array, it might be better to write the values to separate properties rather than relying on the array order, so in the ESP you could send
{"temperature": 21, "humidity": 45, ...}
then after it comes out of the MQTT node you will be able to usemsg.payload.temperature.

I can't configure ESP in this format {"temperature": 21, "humidity": 45, ...} because I am sending an entire package from another LoRa device. So I get a buffer with all this data and send it to MQTT. Do you know any function in javascript that I can get each position of this array?

2

The usual array methods will do it. msg.payload.valores[0] for the first one, [1] for the second, and so on.

Ah yes, it works! But I wanted to separate right after MQTT to build a dashboard .. I'm still learning how to use Node-red, I apologize for the simple questions

You shouldn't need to separate them, you can tell the dashboard node which item to display.

But if you did want to extract one, use a Change node to Move msg.payload... To msg.payload

I did it !! Thank you! Now I need to organize things on my dasborad .. Thank you very much!

1 Like

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