# Extract value from array, based on timestamp

**URL:** <https://discourse.nodered.org/t/extract-value-from-array-based-on-timestamp/56954>\
**Category:** General\
**Created:** [20 January 2022 08:52 UTC](https://discourse.nodered.org/t/extract-value-from-array-based-on-timestamp/56954 "2022-01-20T08:52:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![haavardbj](https://avatars.discourse-cdn.com/v4/letter/h/e9c0ed/32.png) [@haavardbj](https://discourse.nodered.org/u/haavardbj)\
**Post date:** [20 January 2022 08:52 UTC](https://discourse.nodered.org/t/extract-value-from-array-based-on-timestamp/56954/1 "2022-01-20T08:52:54Z")

</div>

Hi

I have a set of data in an array that i fetch from Nordpool.  
This array has the same set of data every-time.  
looks like this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7c44df44b8b0a69ba88a11f6d93ec4cbc2d80e65.png)

What i would like to achieve, is to extract new data from the array every hour. EX: At 09:00, 10:00, 11:00 and so it goes.  
The spesific value i would like to extract is the "Price".

As far as i know, the arrangement of the array does not change, meaning it always startes at midnight (00:00:00)

Could i make some sort of filter, that every hour fetches the data, and then extract the value based on an actual timestamp? (this could repeat every hour)

---

<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:** [20 January 2022 08:55 UTC](https://discourse.nodered.org/t/extract-value-from-array-based-on-timestamp/56954/2 "2022-01-20T08:55:09Z")

</div>

Hi, screenshots are great but not so good for helping with data or code issues (I cant extract the demo data from a screenshot) so, if you copy the value using the hidden "copy value" button  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/87fc64b4e446581f97ee4247b074653231feaa4e.png)

and paste the array into a reply

`````  
`paste like this between three backticks`  
`````

I will show you how.

---

<div class="post-metadata">

**Author:** ![haavardbj](https://avatars.discourse-cdn.com/v4/letter/h/e9c0ed/32.png) [@haavardbj](https://discourse.nodered.org/u/haavardbj)\
**Post date:** [20 January 2022 09:03 UTC](https://discourse.nodered.org/t/extract-value-from-array-based-on-timestamp/56954/3 "2022-01-20T09:03:19Z")

</div>

```auto
[{"Area":"Oslo","Timestamp":"2022-01-20 00:00","StartTime":"2022-01-20 00:00","EndTime":"2022-01-20 01:00","Price":1263.4,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 01:00","StartTime":"2022-01-20 01:00","EndTime":"2022-01-20 02:00","Price":1222.81,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 02:00","StartTime":"2022-01-20 02:00","EndTime":"2022-01-20 03:00","Price":1182.21,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 03:00","StartTime":"2022-01-20 03:00","EndTime":"2022-01-20 04:00","Price":1152.75,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 04:00","StartTime":"2022-01-20 04:00","EndTime":"2022-01-20 05:00","Price":1153.95,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 05:00","StartTime":"2022-01-20 05:00","EndTime":"2022-01-20 06:00","Price":1228.08,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 06:00","StartTime":"2022-01-20 06:00","EndTime":"2022-01-20 07:00","Price":1297.93,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 07:00","StartTime":"2022-01-20 07:00","EndTime":"2022-01-20 08:00","Price":1311.56,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 08:00","StartTime":"2022-01-20 08:00","EndTime":"2022-01-20 09:00","Price":1352.76,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 09:00","StartTime":"2022-01-20 09:00","EndTime":"2022-01-20 10:00","Price":1337.73,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 10:00","StartTime":"2022-01-20 10:00","EndTime":"2022-01-20 11:00","Price":1319.23,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 11:00","StartTime":"2022-01-20 11:00","EndTime":"2022-01-20 12:00","Price":1315.94,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 12:00","StartTime":"2022-01-20 12:00","EndTime":"2022-01-20 13:00","Price":1286.89,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 13:00","StartTime":"2022-01-20 13:00","EndTime":"2022-01-20 14:00","Price":1277.83,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 14:00","StartTime":"2022-01-20 14:00","EndTime":"2022-01-20 15:00","Price":1307.98,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 15:00","StartTime":"2022-01-20 15:00","EndTime":"2022-01-20 16:00","Price":1322.21,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 16:00","StartTime":"2022-01-20 16:00","EndTime":"2022-01-20 17:00","Price":1355.35,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 17:00","StartTime":"2022-01-20 17:00","EndTime":"2022-01-20 18:00","Price":1375.15,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 18:00","StartTime":"2022-01-20 18:00","EndTime":"2022-01-20 19:00","Price":1362.01,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 19:00","StartTime":"2022-01-20 19:00","EndTime":"2022-01-20 20:00","Price":1361.22,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 20:00","StartTime":"2022-01-20 20:00","EndTime":"2022-01-20 21:00","Price":1360.12,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 21:00","StartTime":"2022-01-20 21:00","EndTime":"2022-01-20 22:00","Price":1356.14,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 22:00","StartTime":"2022-01-20 22:00","EndTime":"2022-01-20 23:00","Price":1318.93,"Valuta":"NOK/MWh"},{"Area":"Oslo","Timestamp":"2022-01-20 23:00","StartTime":"2022-01-20 23:00","EndTime":"2022-01-21 00:00","Price":1280.72,"Valuta":"NOK/MWh"}]

```

---

<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:** [20 January 2022 09:06 UTC](https://discourse.nodered.org/t/extract-value-from-array-based-on-timestamp/56954/4 "2022-01-20T09:06:19Z")

</div>

Hi, am I missing something?

That data is already hourly based? Are you wanting to grab the value for a specific hour? Or re-format the array into a list of prices?

What exactly would you like to transform the data to? Can you provide an example output format?

---

<div class="post-metadata">

**Author:** ![haavardbj](https://avatars.discourse-cdn.com/v4/letter/h/e9c0ed/32.png) [@haavardbj](https://discourse.nodered.org/u/haavardbj)\
**Post date:** [20 January 2022 09:10 UTC](https://discourse.nodered.org/t/extract-value-from-array-based-on-timestamp/56954/5 "2022-01-20T09:10:25Z")

</div>

I would like to grab the value "price" every hour, for the actual hour.  
Ex: at 09:00 i would like to grab the value the has the timestamp 09:00. at 10:00 i would like to grab the value the has the timestamp 10:00.

The output format should be a number value, this will be transfered to node, that exports to a BUS-system. (the export part should be ok)

Should run all by itself 24/7 .-)

---

<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:** [20 January 2022 09:19 UTC](https://discourse.nodered.org/t/extract-value-from-array-based-on-timestamp/56954/6 "2022-01-20T09:19:27Z")

</div>

Is this good enough?

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

```javascript
const data = msg.payload;
const now = new Date();
const nearestHour = roundDate(now);
const findTimestamp = nearestHour.valueOf();

msg.payload = data.find(e => {
    return (new Date(e.Timestamp)).valueOf() == findTimestamp;
});

return msg;

function roundDate(date) {
    date.setHours(date.getHours() + Math.round(date.getMinutes() / 60));
    date.setMinutes(0, 0, 0);
    return date;
}

```

---

<div class="post-metadata">

**Author:** ![haavardbj](https://avatars.discourse-cdn.com/v4/letter/h/e9c0ed/32.png) [@haavardbj](https://discourse.nodered.org/u/haavardbj)\
**Post date:** [20 January 2022 09:40 UTC](https://discourse.nodered.org/t/extract-value-from-array-based-on-timestamp/56954/7 "2022-01-20T09:40:07Z")

</div>

Absoloutley 😃

In total it looks like this:  
It injects every hour, 7-days a week. I extract only the price value and scales it down. Then transfer it to the KNX-bus.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/2/92bbb112edab4d5108c1be113efd23b8ff6402d2.png)

`[{"id":"6750177e.9c5248","type":"nordpool-api","z":"54e0f9c.d085f08","area":"Oslo","currency":"NOK","timeSpan":"hourly","x":320,"y":360,"wires":[["b689464a.6b2158"]]},{"id":"b689464a.6b2158","type":"function","z":"54e0f9c.d085f08","name":"","func":"const data = msg.payload;\nconst now = new Date();\nconst nearestHour = roundDate(now);\nconst findTimestamp = nearestHour.valueOf();\n\nmsg.payload = data.find(e => {\n return (new Date(e.Timestamp)).valueOf() == findTimestamp;\n});\n\nreturn msg;\n\nfunction roundDate(date) {\n date.setHours(date.getHours() + Math.round(date.getMinutes() / 60));\n date.setMinutes(0, 0, 0);\n return date;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":360,"wires":[["bdbe6609.b51ab8"]]},{"id":"dde0a818.954328","type":"inject","z":"54e0f9c.d085f08","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"0 0-23 * * *","once":true,"onceDelay":0.1,"topic":"1","payload":"1","payloadType":"msg","x":150,"y":360,"wires":[["6750177e.9c5248"]]},{"id":"64e7fe1f.b7639","type":"knxUltimate","z":"54e0f9c.d085f08","server":"cc7ede91.2dee4","topic":"10/7/2","outputtopic":"","dpt":"9.024","initialread":0,"notifyreadrequest":false,"notifyresponse":false,"notifywrite":false,"notifyreadrequestalsorespondtobus":false,"notifyreadrequestalsorespondtobusdefaultvalueifnotinitialized":"0","listenallga":false,"name":"Spot Timesverdi","outputtype":"write","outputRBE":true,"inputRBE":false,"formatmultiplyvalue":1,"formatnegativevalue":"leave","formatdecimalsvalue":999,"passthrough":"yes","x":1160,"y":360,"wires":[[]]},{"id":"bdbe6609.b51ab8","type":"change","z":"54e0f9c.d085f08","name":"","rules":[{"t":"move","p":"payload.Price","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":360,"wires":[["e1665470.9ddb68"]]},{"id":"e1665470.9ddb68","type":"range","z":"54e0f9c.d085f08","minin":"0","maxin":"10000","minout":"0","maxout":"1000","action":"scale","round":false,"property":"payload","name":"","x":920,"y":360,"wires":[["64e7fe1f.b7639"]]},{"id":"cc7ede91.2dee4","type":"knxUltimate-config","host":"192.168.1.43","port":"3671","physAddr":"1.1.230","suppressACKRequest":false,"csv":"","KNXEthInterface":"Auto","KNXEthInterfaceManuallyInput":"","statusDisplayLastUpdate":true,"statusDisplayDeviceNameWhenALL":true,"statusDisplayDataPoint":false,"stopETSImportIfNoDatapoint":"stop","loglevel":"error","name":"Siemenes IP-interface","localEchoInTunneling":true,"delaybetweentelegrams":"40","delaybetweentelegramsfurtherdelayREAD":"1"}]`

---

<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:** [3 February 2022 09:40 UTC](https://discourse.nodered.org/t/extract-value-from-array-based-on-timestamp/56954/8 "2022-02-03T09:40:34Z")

</div>

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