Get time and date closest to current time

I am using Node-red to make a program for home automation. I take energy prices from the norpool api+ and format these values to just get the timestamp and also the prices per hour.
image
I get these values for every hour of the day. Is it possible to just get the price of the hour we are closest to. For example when it is 14/3/2023 10:0:0 to just get the price 0.1544. I want to show this value on the dashboard and also I want to make an automatisation that send values to some devices according to the current energy prices.
image
The code to get these values is:

flows.json (1.2 KB)

{"today":[{"Tijd":"14/3/2023 00:00:00","Prijs":0.01914},{"Tijd":"14/3/2023 01:00:00","Prijs":0.00902},{"Tijd":"14/3/2023 02:00:00","Prijs":0.00818},{"Tijd":"14/3/2023 03:00:00","Prijs":0.00426},{"Tijd":"14/3/2023 04:00:00","Prijs":0.00774},{"Tijd":"14/3/2023 05:00:00","Prijs":0.02138},{"Tijd":"14/3/2023 06:00:00","Prijs":0.09126000000000001},{"Tijd":"14/3/2023 07:00:00","Prijs":0.18872},{"Tijd":"14/3/2023 08:00:00","Prijs":0.20022},{"Tijd":"14/3/2023 09:00:00","Prijs":0.18762},{"Tijd":"14/3/2023 10:00:00","Prijs":0.1544},{"Tijd":"14/3/2023 11:00:00","Prijs":0.12192},{"Tijd":"14/3/2023 12:00:00","Prijs":0.09337999999999999},{"Tijd":"14/3/2023 13:00:00","Prijs":0.11236},{"Tijd":"14/3/2023 14:00:00","Prijs":0.13136},{"Tijd":"14/3/2023 15:00:00","Prijs":0.1218},{"Tijd":"14/3/2023 16:00:00","Prijs":0.16091999999999998},{"Tijd":"14/3/2023 17:00:00","Prijs":0.268},{"Tijd":"14/3/2023 18:00:00","Prijs":0.314},{"Tijd":"14/3/2023 19:00:00","Prijs":0.4096},{"Tijd":"14/3/2023 20:00:00","Prijs":0.3207},{"Tijd":"14/3/2023 21:00:00","Prijs":0.35926},{"Tijd":"14/3/2023 22:00:00","Prijs":0.31760000000000005},{"Tijd":"14/3/2023 23:00:00","Prijs":0.3092}]}

Thanks in advance

You can use a change node with JSONata and $moment() to return the correct hour.
e.g.

[{"id":"143738ee202674db","type":"inject","z":"e109375995a10df2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"today\":[{\"Tijd\":\"14/3/2023 00:00:00\",\"Prijs\":0.01914},{\"Tijd\":\"14/3/2023 01:00:00\",\"Prijs\":0.00902},{\"Tijd\":\"14/3/2023 02:00:00\",\"Prijs\":0.00818},{\"Tijd\":\"14/3/2023 03:00:00\",\"Prijs\":0.00426},{\"Tijd\":\"14/3/2023 04:00:00\",\"Prijs\":0.00774},{\"Tijd\":\"14/3/2023 05:00:00\",\"Prijs\":0.02138},{\"Tijd\":\"14/3/2023 06:00:00\",\"Prijs\":0.09126000000000001},{\"Tijd\":\"14/3/2023 07:00:00\",\"Prijs\":0.18872},{\"Tijd\":\"14/3/2023 08:00:00\",\"Prijs\":0.20022},{\"Tijd\":\"14/3/2023 09:00:00\",\"Prijs\":0.18762},{\"Tijd\":\"14/3/2023 10:00:00\",\"Prijs\":0.1544},{\"Tijd\":\"14/3/2023 11:00:00\",\"Prijs\":0.12192},{\"Tijd\":\"14/3/2023 12:00:00\",\"Prijs\":0.09337999999999999},{\"Tijd\":\"14/3/2023 13:00:00\",\"Prijs\":0.11236},{\"Tijd\":\"14/3/2023 14:00:00\",\"Prijs\":0.13136},{\"Tijd\":\"14/3/2023 15:00:00\",\"Prijs\":0.1218},{\"Tijd\":\"14/3/2023 16:00:00\",\"Prijs\":0.16091999999999998},{\"Tijd\":\"14/3/2023 17:00:00\",\"Prijs\":0.268},{\"Tijd\":\"14/3/2023 18:00:00\",\"Prijs\":0.314},{\"Tijd\":\"14/3/2023 19:00:00\",\"Prijs\":0.4096},{\"Tijd\":\"14/3/2023 20:00:00\",\"Prijs\":0.3207},{\"Tijd\":\"14/3/2023 21:00:00\",\"Prijs\":0.35926},{\"Tijd\":\"14/3/2023 22:00:00\",\"Prijs\":0.31760000000000005},{\"Tijd\":\"14/3/2023 23:00:00\",\"Prijs\":0.3092}]}","payloadType":"json","x":390,"y":140,"wires":[["72511b991e8b6e44"]]},{"id":"72511b991e8b6e44","type":"change","z":"e109375995a10df2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t  $hour := $moment().format(\"HH\");\t  $$.payload.today[\t   $moment($.Tijd,\"DD/MM/YYYY HH:mm:ss\").format(\"HH\") = $hour\t  ]\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":140,"wires":[["1add140e06f21e2f"]]},{"id":"1add140e06f21e2f","type":"debug","z":"e109375995a10df2","name":"debug 29","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":840,"y":180,"wires":[]}]

exprssion

(
  $hour := $moment().format("HH");
  $$.payload.today[
   $moment($.Tijd,"DD/MM/YYYY HH:mm:ss").format("HH") = $hour
  ]
)
1 Like

Thank you for the quick reply. This worked perfect.

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