Filling an array with measurements

My weather station sends a payload with the precipitation measured over that period to NodeRed every 30 minutes. I would like to store those 48 measurements per day in the array precipitation(x), is that possible? And can I set all values ​​in that array to 0 at 00:00? Any help is welcome!

Yes. you can store them in context

See Working with context : Node-RED

Yes, use an inject set to trigger at 00:00 that sends [0,0,0,......] to a change node that sets global.measurements to the value in payload.

Thanks Steve, I'll try and let you know if I succeeded.

You can easily append a value to an array:

let precipitationArray = global.get ("precipitationArray") || []
precipitationArray.push(msg.payload.precipitation)
global.set("precipitationArray", precipitationArray)

But if you initialise the array to [0,0, ....0] then when a new precipitation reading arrives, you need to work out which bucket you need to update
eg precipitationArray[27] = msg.payload.precipitation

That is a little extra work, but probably worthwhile because if for some reason a message is missed (or gets duplicated) your context variable won't get out of sync.

Better yet, instead of an array of numbers, save an object, using the timestamp as the key

{
"00:00": 0.1,
"00:30": 2.8,
...
"23:30": 0
}
let precipitationObject = global.get ("precipitationObject") || {}
precipitationObject[msg.payload.timestamp] = msg.payload.precipitation
global.set("precipitationObject", precipitationObject)
2 Likes

With your advice I should be able to do it :wink: