Need help in Nested JSON object

I am trying to collect sensor data from ESP32 to RPi.
I am getting a packet of data containing strings in intervals of 10secs(this can be changed). I am getting sensor data, along with geolocation and device ID which comes in a single packet. I want to append the timestamp and convert the whole packets into JSON format and send it via MQTT to AWS. Can someone help me with how to do this?

Thank you

Yes we can help you but it’s unclear (at least to me) if the data getting into Node-RED is all arriving at the same time in one msg or does the data arrive at various times.

If it is in one msg and you just want to add a timestamp, you might be able do it in a function node or a template node or maybe even with a change node.

Show us the data you are getting and an example of what it should look like to send off.

This is my incoming data
"geodata=15.34-34.67;device_ID=3;Temperature=10 degC;Humidity=2 RH;WindSpeed=5 m/s"

and I would like to send it in this format
{"geodata": " ",
"device_ID: ,
"value": {
"timestamp": , // this is an array
"Temperature": , // this is an array
"Humidity": , // this is an array
"Windspeed": // this is an array
}
}
Here the data inside the timestamp, temperature, humidity, and windspeed will be an array because I will be sending the data to the cloud occasionally. For example, If I am getting data from ESP32 every 10secs, then I will append the data for 2mins, and send the complete set of data at once every 2mins. I hope this is clear.

You could try something like this in a function node (assuming it always arrives in this order):

const i = msg.payload.split(";")

const geodata = i[0].split("=")[1];
const device_ID = Number(i[1].split("=")[1]);
const Temperature = i[2].split("=")[1];
const Humidity = i[3].split("=")[1];
const WindSpeed = i[4].split("=")[1];

msg.payload = {
    geodata,
    device_ID,
    values:{
        Temperature: [Temperature],
        Humidity: [Humidity],
        WindSpeed: [WindSpeed]
    }
}

return msg;


1 Like

I had already tried this, but my requirement is to send the appended temperature, humidity, and windspeed data. If I get 10 data packets from esp32 and I want to send an array of data. I need to have geodata and device_ID as one entry but temp, humidity, and windspeed can have multiple entries like an array.

Are geodata and device_ID always the same?

Right now, I am testing one scenario only. So, geodata and device_ID will be the same for now. But there might be a scenario I might have 2 or 3. Not more than that. Geodata is a unique identifier according to device_ID.

This might work but only for constant geodata.
It constructs a compound object from successive messages, but it always returns the message as it is so far, which you may not want.

const i = msg.payload.split(";")

const geodata = i[0].split("=")[1];
const device_ID = Number(i[1].split("=")[1]);
const Temperature = i[2].split("=")[1];
const Humidity = i[3].split("=")[1];
const WindSpeed = i[4].split("=")[1];
const now = new Date()

let mydata = context.get("mydata") ?? {}
if (mydata.hasOwnProperty("geodata")) {

}
else {
    mydata.geodata = geodata
    mydata.deviceid = device_ID
    mydata.timestamp = []
    mydata.temperature=[]
    mydata.humidity=[]
    mydata.windspeed=[]
}
mydata.timestamp.push(now)
mydata.temperature.push(Temperature)
mydata.humidity.push(Humidity)
mydata.windspeed.push(WindSpeed)

context.set("mydata", mydata)
msg.payload = mydata
return msg;

Why don't you send the complete json from the esp ?

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