Extract price value from array, based on timestamp now

Hi, i need help with an function node

I'm trying to do the same as in this tread Extract value from array, based on timestamp from an other sorce, but i'm struggling

I have the node-red-contrib-power-saver in my flow. It reports powergrid prices in an array pr. hour and i want to extract the price for the hour now, so that i can have it as an value presented in an sensor Home Assistant

My data is like
image

{"payload":{"schedule":[{"time":"2022-08-30T00:00:00.000+02:00","value":false,"countHours":2},{"time":"2022-08-30T02:00:00.000+02:00","value":true,"countHours":4},{"time":"2022-08-30T06:00:00.000+02:00","value":false,"countHours":8},{"time":"2022-08-30T14:00:00.000+02:00","value":true,"countHours":2},{"time":"2022-08-30T16:00:00.000+02:00","value":false,"countHours":8},{"time":"2022-08-31T00:00:00.000+02:00","value":true,"countHours":1},{"time":"2022-08-31T01:00:00.000+02:00","value":false,"countHours":2},{"time":"2022-08-31T03:00:00.000+02:00","value":true,"countHours":3},{"time":"2022-08-31T06:00:00.000+02:00","value":false,"countHours":8},{"time":"2022-08-31T14:00:00.000+02:00","value":true,"countHours":2},{"time":"2022-08-31T16:00:00.000+02:00","value":false,"countHours":7},{"time":"2022-08-31T23:00:00.000+02:00","value":true,"countHours":1}],"hours":[{"price":8.7252,"onOff":false,"start":"2022-08-30T00:00:00.000+02:00","saving":0.945},{"price":8.1356,"onOff":false,"start":"2022-08-30T01:00:00.000+02:00","saving":0.3554},{"price":7.7802,"onOff":true,"start":"2022-08-30T02:00:00.000+02:00","saving":null},{"price":7.7795,"onOff":true,"start":"2022-08-30T03:00:00.000+02:00","saving":null},{"price":7.9021,"onOff":true,"start":"2022-08-30T04:00:00.000+02:00","saving":null},{"price":8.6829,"onOff":true,"start":"2022-08-30T05:00:00.000+02:00","saving":null},{"price":8.9049,"onOff":false,"start":"2022-08-30T06:00:00.000+02:00","saving":1.1898},{"price":9.8284,"onOff":false,"start":"2022-08-30T07:00:00.000+02:00","saving":2.1133},{"price":9.8861,"onOff":false,"start":"2022-08-30T08:00:00.000+02:00","saving":2.171},{"price":9.5237,"onOff":false,"start":"2022-08-30T09:00:00.000+02:00","saving":1.8086},{"price":8.9406,"onOff":false,"start":"2022-08-30T10:00:00.000+02:00","saving":1.2255},{"price":8.6481,"onOff":false,"start":"2022-08-30T11:00:00.000+02:00","saving":0.933},{"price":7.9088,"onOff":false,"start":"2022-08-30T12:00:00.000+02:00","saving":0.1937},{"price":7.9417,"onOff":false,"start":"2022-08-30T13:00:00.000+02:00","saving":0.2266},{"price":7.7151,"onOff":true,"start":"2022-08-30T14:00:00.000+02:00","saving":null},{"price":8.2213,"onOff":true,"start":"2022-08-30T15:00:00.000+02:00","saving":null},{"price":8.7971,"onOff":false,"start":"2022-08-30T16:00:00.000+02:00","saving":1.4897},{"price":9.2211,"onOff":false,"start":"2022-08-30T17:00:00.000+02:00","saving":1.9137},{"price":9.8651,"onOff":false,"start":"2022-08-30T18:00:00.000+02:00","saving":2.5577},{"price":10.5044,"onOff":false,"start":"2022-08-30T19:00:00.000+02:00","saving":3.197},{"price":9.7655,"onOff":false,"start":"2022-08-30T20:00:00.000+02:00","saving":2.4581},{"price":9.0699,"onOff":false,"start":"2022-08-30T21:00:00.000+02:00","saving":1.7625},{"price":8.5817,"onOff":false,"start":"2022-08-30T22:00:00.000+02:00","saving":1.2743},{"price":7.8675,"onOff":false,"start":"2022-08-30T23:00:00.000+02:00","saving":0.5601},{"price":7.3074,"onOff":true,"start":"2022-08-31T00:00:00.000+02:00","saving":null},{"price":7.2048,"onOff":false,"start":"2022-08-31T01:00:00.000+02:00","saving":0.3159},{"price":7.4045,"onOff":false,"start":"2022-08-31T02:00:00.000+02:00","saving":0.5156},{"price":6.8889,"onOff":true,"start":"2022-08-31T03:00:00.000+02:00","saving":null},{"price":7.4153,"onOff":true,"start":"2022-08-31T04:00:00.000+02:00","saving":null},{"price":7.625,"onOff":true,"start":"2022-08-31T05:00:00.000+02:00","saving":null},{"price":8.4984,"onOff":false,"start":"2022-08-31T06:00:00.000+02:00","saving":1.514},{"price":8.9344,"onOff":false,"start":"2022-08-31T07:00:00.000+02:00","saving":1.95},{"price":9.3124,"onOff":false,"start":"2022-08-31T08:00:00.000+02:00","saving":2.328},{"price":8.853,"onOff":false,"start":"2022-08-31T09:00:00.000+02:00","saving":1.8686},{"price":8.6565,"onOff":false,"start":"2022-08-31T10:00:00.000+02:00","saving":1.6721},{"price":8.6331,"onOff":false,"start":"2022-08-31T11:00:00.000+02:00","saving":1.6487},{"price":7.7032,"onOff":false,"start":"2022-08-31T12:00:00.000+02:00","saving":0.7188},{"price":7.3886,"onOff":false,"start":"2022-08-31T13:00:00.000+02:00","saving":0.4042},{"price":6.9844,"onOff":true,"start":"2022-08-31T14:00:00.000+02:00","saving":null},{"price":7.2696,"onOff":true,"start":"2022-08-31T15:00:00.000+02:00","saving":null},{"price":7.7563,"onOff":false,"start":"2022-08-31T16:00:00.000+02:00","saving":0.4988},{"price":8.6739,"onOff":false,"start":"2022-08-31T17:00:00.000+02:00","saving":1.4164},{"price":8.8834,"onOff":false,"start":"2022-08-31T18:00:00.000+02:00","saving":1.6259},{"price":9.2192,"onOff":false,"start":"2022-08-31T19:00:00.000+02:00","saving":1.9617},{"price":8.9507,"onOff":false,"start":"2022-08-31T20:00:00.000+02:00","saving":1.6932},{"price":8.6882,"onOff":false,"start":"2022-08-31T21:00:00.000+02:00","saving":1.4307},{"price":7.6748,"onOff":false,"start":"2022-08-31T22:00:00.000+02:00","saving":0.4173},{"price":7.2575,"onOff":true,"start":"2022-08-31T23:00:00.000+02:00","saving":null}],"source":"Tibber","config":{"maxHoursToSaveInSequence":"8","minHoursOnAfterMaxSequenceSaved":"1","minSaving":0.1,"sendCurrentValueWhenRescheduling":true,"outputIfNoSchedule":true,"contextStorage":"memory"},"sentOnCommand":false,"time":"2022-08-30T19:01:07.246+02:00","version":"3.6.2","current":false},"_msgid":"80ac1dfd0b3c85eb"}

And running every hour i want payload.hours[i].start ( time = now ) to send payload.hours[i].price as payload.price.now

Help anyone ??

This may help, it uses JSONata and moment(), I find date/time easier then the javascript date(). Hope it helps.
You may need to adjust the tz() timezone.

[{"id":"a9307deb28e64897","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"schedule\":[{\"time\":\"2022-08-30T00:00:00.000+02:00\",\"value\":false,\"countHours\":2},{\"time\":\"2022-08-30T02:00:00.000+02:00\",\"value\":true,\"countHours\":4},{\"time\":\"2022-08-30T06:00:00.000+02:00\",\"value\":false,\"countHours\":8},{\"time\":\"2022-08-30T14:00:00.000+02:00\",\"value\":true,\"countHours\":2},{\"time\":\"2022-08-30T16:00:00.000+02:00\",\"value\":false,\"countHours\":8},{\"time\":\"2022-08-31T00:00:00.000+02:00\",\"value\":true,\"countHours\":1},{\"time\":\"2022-08-31T01:00:00.000+02:00\",\"value\":false,\"countHours\":2},{\"time\":\"2022-08-31T03:00:00.000+02:00\",\"value\":true,\"countHours\":3},{\"time\":\"2022-08-31T06:00:00.000+02:00\",\"value\":false,\"countHours\":8},{\"time\":\"2022-08-31T14:00:00.000+02:00\",\"value\":true,\"countHours\":2},{\"time\":\"2022-08-31T16:00:00.000+02:00\",\"value\":false,\"countHours\":7},{\"time\":\"2022-08-31T23:00:00.000+02:00\",\"value\":true,\"countHours\":1}],\"hours\":[{\"price\":8.7252,\"onOff\":false,\"start\":\"2022-08-30T00:00:00.000+02:00\",\"saving\":0.945},{\"price\":8.1356,\"onOff\":false,\"start\":\"2022-08-30T01:00:00.000+02:00\",\"saving\":0.3554},{\"price\":7.7802,\"onOff\":true,\"start\":\"2022-08-30T02:00:00.000+02:00\",\"saving\":null},{\"price\":7.7795,\"onOff\":true,\"start\":\"2022-08-30T03:00:00.000+02:00\",\"saving\":null},{\"price\":7.9021,\"onOff\":true,\"start\":\"2022-08-30T04:00:00.000+02:00\",\"saving\":null},{\"price\":8.6829,\"onOff\":true,\"start\":\"2022-08-30T05:00:00.000+02:00\",\"saving\":null},{\"price\":8.9049,\"onOff\":false,\"start\":\"2022-08-30T06:00:00.000+02:00\",\"saving\":1.1898},{\"price\":9.8284,\"onOff\":false,\"start\":\"2022-08-30T07:00:00.000+02:00\",\"saving\":2.1133},{\"price\":9.8861,\"onOff\":false,\"start\":\"2022-08-30T08:00:00.000+02:00\",\"saving\":2.171},{\"price\":9.5237,\"onOff\":false,\"start\":\"2022-08-30T09:00:00.000+02:00\",\"saving\":1.8086},{\"price\":8.9406,\"onOff\":false,\"start\":\"2022-08-30T10:00:00.000+02:00\",\"saving\":1.2255},{\"price\":8.6481,\"onOff\":false,\"start\":\"2022-08-30T11:00:00.000+02:00\",\"saving\":0.933},{\"price\":7.9088,\"onOff\":false,\"start\":\"2022-08-30T12:00:00.000+02:00\",\"saving\":0.1937},{\"price\":7.9417,\"onOff\":false,\"start\":\"2022-08-30T13:00:00.000+02:00\",\"saving\":0.2266},{\"price\":7.7151,\"onOff\":true,\"start\":\"2022-08-30T14:00:00.000+02:00\",\"saving\":null},{\"price\":8.2213,\"onOff\":true,\"start\":\"2022-08-30T15:00:00.000+02:00\",\"saving\":null},{\"price\":8.7971,\"onOff\":false,\"start\":\"2022-08-30T16:00:00.000+02:00\",\"saving\":1.4897},{\"price\":9.2211,\"onOff\":false,\"start\":\"2022-08-30T17:00:00.000+02:00\",\"saving\":1.9137},{\"price\":9.8651,\"onOff\":false,\"start\":\"2022-08-30T18:00:00.000+02:00\",\"saving\":2.5577},{\"price\":10.5044,\"onOff\":false,\"start\":\"2022-08-30T19:00:00.000+02:00\",\"saving\":3.197},{\"price\":9.7655,\"onOff\":false,\"start\":\"2022-08-30T20:00:00.000+02:00\",\"saving\":2.4581},{\"price\":9.0699,\"onOff\":false,\"start\":\"2022-08-30T21:00:00.000+02:00\",\"saving\":1.7625},{\"price\":8.5817,\"onOff\":false,\"start\":\"2022-08-30T22:00:00.000+02:00\",\"saving\":1.2743},{\"price\":7.8675,\"onOff\":false,\"start\":\"2022-08-30T23:00:00.000+02:00\",\"saving\":0.5601},{\"price\":7.3074,\"onOff\":true,\"start\":\"2022-08-31T00:00:00.000+02:00\",\"saving\":null},{\"price\":7.2048,\"onOff\":false,\"start\":\"2022-08-31T01:00:00.000+02:00\",\"saving\":0.3159},{\"price\":7.4045,\"onOff\":false,\"start\":\"2022-08-31T02:00:00.000+02:00\",\"saving\":0.5156},{\"price\":6.8889,\"onOff\":true,\"start\":\"2022-08-31T03:00:00.000+02:00\",\"saving\":null},{\"price\":7.4153,\"onOff\":true,\"start\":\"2022-08-31T04:00:00.000+02:00\",\"saving\":null},{\"price\":7.625,\"onOff\":true,\"start\":\"2022-08-31T05:00:00.000+02:00\",\"saving\":null},{\"price\":8.4984,\"onOff\":false,\"start\":\"2022-08-31T06:00:00.000+02:00\",\"saving\":1.514},{\"price\":8.9344,\"onOff\":false,\"start\":\"2022-08-31T07:00:00.000+02:00\",\"saving\":1.95},{\"price\":9.3124,\"onOff\":false,\"start\":\"2022-08-31T08:00:00.000+02:00\",\"saving\":2.328},{\"price\":8.853,\"onOff\":false,\"start\":\"2022-08-31T09:00:00.000+02:00\",\"saving\":1.8686},{\"price\":8.6565,\"onOff\":false,\"start\":\"2022-08-31T10:00:00.000+02:00\",\"saving\":1.6721},{\"price\":8.6331,\"onOff\":false,\"start\":\"2022-08-31T11:00:00.000+02:00\",\"saving\":1.6487},{\"price\":7.7032,\"onOff\":false,\"start\":\"2022-08-31T12:00:00.000+02:00\",\"saving\":0.7188},{\"price\":7.3886,\"onOff\":false,\"start\":\"2022-08-31T13:00:00.000+02:00\",\"saving\":0.4042},{\"price\":6.9844,\"onOff\":true,\"start\":\"2022-08-31T14:00:00.000+02:00\",\"saving\":null},{\"price\":7.2696,\"onOff\":true,\"start\":\"2022-08-31T15:00:00.000+02:00\",\"saving\":null},{\"price\":7.7563,\"onOff\":false,\"start\":\"2022-08-31T16:00:00.000+02:00\",\"saving\":0.4988},{\"price\":8.6739,\"onOff\":false,\"start\":\"2022-08-31T17:00:00.000+02:00\",\"saving\":1.4164},{\"price\":8.8834,\"onOff\":false,\"start\":\"2022-08-31T18:00:00.000+02:00\",\"saving\":1.6259},{\"price\":9.2192,\"onOff\":false,\"start\":\"2022-08-31T19:00:00.000+02:00\",\"saving\":1.9617},{\"price\":8.9507,\"onOff\":false,\"start\":\"2022-08-31T20:00:00.000+02:00\",\"saving\":1.6932},{\"price\":8.6882,\"onOff\":false,\"start\":\"2022-08-31T21:00:00.000+02:00\",\"saving\":1.4307},{\"price\":7.6748,\"onOff\":false,\"start\":\"2022-08-31T22:00:00.000+02:00\",\"saving\":0.4173},{\"price\":7.2575,\"onOff\":true,\"start\":\"2022-08-31T23:00:00.000+02:00\",\"saving\":null}],\"source\":\"Tibber\",\"config\":{\"maxHoursToSaveInSequence\":\"8\",\"minHoursOnAfterMaxSequenceSaved\":\"1\",\"minSaving\":0.1,\"sendCurrentValueWhenRescheduling\":true,\"outputIfNoSchedule\":true,\"contextStorage\":\"memory\"}}","payloadType":"json","x":110,"y":1480,"wires":[["c88c9d0c.09a368"]]},{"id":"c88c9d0c.09a368","type":"change","z":"30af2d3e.d94ea2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.hours[$split($.start,\"+\")[0] = $moment().tz(\"Europe/London\").format(\"YYYY-MM-DDTHH:\" & \"00:00.000\")]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":1480,"wires":[["5121ca73.646294"]]},{"id":"5121ca73.646294","type":"debug","z":"30af2d3e.d94ea2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":1480,"wires":[]}]

expression

$$.payload.hours[
   $split($.start,"+")[0] = $moment().tz("Europe/London").format("YYYY-MM-DDTHH:" &
   "00:00.000")
]

output at time now 19.06

{
  "price":10.5044,
  "onOff":false,
  "start":"2022-08-30T19:00:00.000+02:00",
  "saving":3.197
}

Wonderfull It works! Thanks for the help :slight_smile:

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