# Set array to global once a day, then get specific array by the hour

**URL:** https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261
**Category:** General
**Created:** [22 October 2023 19:27 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261 "2023-10-22T19:27:26Z")
**Posts on this page:** 12
**Page:** 2

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [24 October 2023 06:36 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/21 "2023-10-24T06:36:21Z")

</div>

That' correct.

And if you query it now (prices not yet available) you'll get this situation:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5a18b0ef8161d03574fe4403c1224e3cee57c58b.png)

---

<div class="post-metadata">

### Author: ![P-solver](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@P-solver](https://discourse.nodered.org/u/P-solver)
#### Post date: [24 October 2023 06:52 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/22 "2023-10-24T06:52:40Z")

</div>

> [@hotNipi](#):
>
> ```auto
> let nordpool = global.get('nordpool') || []
> //node.warn(msg.payload.length)
> if(msg.payload && msg.payload.lenght != 0){
> msg.payload.forEach(function(element) {
> //get rid of duplicates if any
> let duplicate = nordpool.find(el => el.x == new Date(element.timestamp).getTime())
> if(duplicate == undefined){
> // x is timestamp
> // y is price
> // easy to use with charts.
> nordpool.push({
> x:new Date(element.timestamp).getTime(),
> y:element.price
> })
> }        
> });
> }
> //keep data in order time wise
> nordpool.sort((a, b) => {
> return a.x - b.x;
> });
> 
> //filter out too old data
> let old = new Date()
> old.setHours(0,0,0,0)
> old.setDate(old.getDate()-3)
> nordpool = nordpool.filter(el => el.x > old.getTime())
> 
> //store prices
> global.set('nordpool',nordpool)
> return msg;
> 
> ```

And this code goes into a function node running every hour (separate flow) or is this also just triggered once a day?

I can defenitly see the benefits of having a wider range of prices, including history, but I'm not sure this will solve the problem that I'm having, to fetch the "next hours price" each hour. from the array.

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [24 October 2023 07:38 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/24 "2023-10-24T07:38:57Z")

</div>

> [@P-solver](#):
>
> And this code goes into a function node running every hour (separate flow) or is this also just triggered once a day?

That's the function after the nordpool-api node. I make day-ahead query once a day at 15:00 (local time).  
In case that query had failed, I have check for data existence and then i do that query periodically until there is data.

To get price for next hour it is just a date manipulation. You can of course use one of many tools for date manipulation but it is simple enough with plain JavaScript

```auto
let prices = global.get('nordpool') || []

if (prices.length== 0) {
    return
}

let d = new Date()
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);
d.setHours(d.getHours() + 1)

let price = prices.find(el => el.x == d.getTime())

node.warn(price)
msg.payload = price
return msg;

```

---

<div class="post-metadata">

### Author: ![P-solver](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@P-solver](https://discourse.nodered.org/u/P-solver)
#### Post date: [24 October 2023 08:34 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/25 "2023-10-24T08:34:15Z")

</div>

I still just get an array[24] of todays prices running this.

I have the flow like this:

Timestamp -\> (Code from post 19) -\> Nordpool API -\> (Code from post 17) -\> Debug

Is that wrong?

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [24 October 2023 08:42 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/26 "2023-10-24T08:42:38Z")

</div>

This is what nordpool-api node's help says

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55dc5b585909306f4f6464556136ff6f4b3caef5.png)

As I'm using old version, who can tell why and how it behaves so don't rely on my experiences.

But no worries, after day-ahead prices published, I'm sure you'll get those too.

---

<div class="post-metadata">

### Author: ![P-solver](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@P-solver](https://discourse.nodered.org/u/P-solver)
#### Post date: [24 October 2023 14:30 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/27 "2023-10-24T14:30:47Z")

</div>

Everything seems to be working ok, except I suddently got an error "TypeError: nordpool.find is not a function" in the debug.

I get the array in msg. payload with 48 objects.

I have copy/pasted your code, but it gives this error.

Any idea what it can be because of?

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [24 October 2023 15:14 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/28 "2023-10-24T15:14:42Z")

</div>

Can't tell based on that less of info.  
Known that copy-paste is dangerous way of programming but the error indicates that the nordpool is not an array. So debug it to be sure.

---

<div class="post-metadata">

### Author: ![P-solver](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@P-solver](https://discourse.nodered.org/u/P-solver)
#### Post date: [24 October 2023 17:06 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/29 "2023-10-24T17:06:04Z")

</div>

> [@hotNipi](#):
>
> `let price = prices.find(el => el.x == d.getTime())`

Does the above code compare the "generated date+time (+1h)" and the "date+time" in the nordpool-array independant of format?

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [24 October 2023 18:18 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/30 "2023-10-24T18:18:29Z")

</div>

Format is expected to be as that big function stores it.  
x is timestamp  
y is price

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/75fced404d1f45161e7f54001151a55b36fc588b.png)

That code is searching in array for element with matching timestamp

---

<div class="post-metadata">

### Author: ![P-solver](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@P-solver](https://discourse.nodered.org/u/P-solver)
#### Post date: [25 October 2023 08:43 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/31 "2023-10-25T08:43:21Z")

</div>

Hi, I'm really struggling with the global variable "nordpool", as it seems to not be saved globally.  
I'm wondering if I'm missing out on anything in the flow itself?

Unfortunately I'm running the Node REd on my Raspberry pi, so I'm having trouble to take a screenshot of the setup of the flow.

Would you mind sharing how your flow is looking?

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [25 October 2023 09:10 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/32 "2023-10-25T09:10:45Z")

</div>

Do you use persistent context storage?  
Look at the settings.js for `contextStorage`

---

<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: [24 December 2023 09:10 UTC](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261/33 "2023-12-24T09:10:54Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/set-array-to-global-once-a-day-then-get-specific-array-by-the-hour/82261.md?page=1)
