Extract value from array, based on timestamp

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

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)

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

and paste the array into a reply

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

I will show you how.

[{"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"}]

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?

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 .-)

Is this good enough?

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;
}

Absoloutley :smiley:

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.

[{"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"}]

1 Like

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