# Filling an array with measurements

**URL:** <https://discourse.nodered.org/t/filling-an-array-with-measurements/94957>\
**Category:** General\
**Created:** [28 January 2025 12:11 UTC](https://discourse.nodered.org/t/filling-an-array-with-measurements/94957 "2025-01-28T12:11:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [28 January 2025 12:11 UTC](https://discourse.nodered.org/t/filling-an-array-with-measurements/94957/1 "2025-01-28T12:11:39Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 January 2025 12:25 UTC](https://discourse.nodered.org/t/filling-an-array-with-measurements/94957/2 "2025-01-28T12:25:03Z")

</div>

> [@HaVi](#):
>
> is that possible

Yes. you can store them in context

See [Working with context : Node-RED](https://nodered.org/docs/user-guide/context)

> [@HaVi](#):
>
> And can I set all values ​​in that array to 0 at 00:00

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.

---

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [28 January 2025 12:35 UTC](https://discourse.nodered.org/t/filling-an-array-with-measurements/94957/3 "2025-01-28T12:35:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [28 January 2025 12:47 UTC](https://discourse.nodered.org/t/filling-an-array-with-measurements/94957/4 "2025-01-28T12:47:55Z")

</div>

You can easily append a value to an array:

```auto
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

```auto
{
"00:00": 0.1,
"00:30": 2.8,
...
"23:30": 0
}

```

```auto
let precipitationObject = global.get ("precipitationObject") || {}
precipitationObject[msg.payload.timestamp] = msg.payload.precipitation
global.set("precipitationObject", precipitationObject)

```

---

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [28 January 2025 13:05 UTC](https://discourse.nodered.org/t/filling-an-array-with-measurements/94957/5 "2025-01-28T13:05:17Z")

</div>

With your advice I should be able to do it 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 April 2025 13:05 UTC](https://discourse.nodered.org/t/filling-an-array-with-measurements/94957/6 "2025-04-28T13:05:19Z")

</div>

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