Why nest the if statements ?
also convert the starttime and endtime to dates
let now = new Date()
node.warn(now);
msg.payload.forEach(el => {
if (now > new Date(el.StartTime) && now < new Date(el.EndTime)) {
msg.payload = el
}
})
return msg;
Demo Flow
[{"id":"ca6248f4eb7934c1","type":"inject","z":"54efb553244c241f","name":"data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 00:00\",\"StartTime\":\"2022-01-02 00:00\",\"EndTime\":\"2022-01-02 01:00\",\"Price\":357.22,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 01:00\",\"StartTime\":\"2022-01-02 01:00\",\"EndTime\":\"2022-01-02 02:00\",\"Price\":353.73,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 02:00\",\"StartTime\":\"2022-01-02 02:00\",\"EndTime\":\"2022-01-02 03:00\",\"Price\":353.42,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 03:00\",\"StartTime\":\"2022-01-02 03:00\",\"EndTime\":\"2022-01-02 04:00\",\"Price\":350.74,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 04:00\",\"StartTime\":\"2022-01-02 04:00\",\"EndTime\":\"2022-01-02 05:00\",\"Price\":304.05,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 05:00\",\"StartTime\":\"2022-01-02 05:00\",\"EndTime\":\"2022-01-02 06:00\",\"Price\":349.41,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 06:00\",\"StartTime\":\"2022-01-02 06:00\",\"EndTime\":\"2022-01-02 07:00\",\"Price\":355.06,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 07:00\",\"StartTime\":\"2022-01-02 07:00\",\"EndTime\":\"2022-01-02 08:00\",\"Price\":429.94,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 08:00\",\"StartTime\":\"2022-01-02 08:00\",\"EndTime\":\"2022-01-02 09:00\",\"Price\":503.49,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 09:00\",\"StartTime\":\"2022-01-02 09:00\",\"EndTime\":\"2022-01-02 10:00\",\"Price\":617.24,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 10:00\",\"StartTime\":\"2022-01-02 10:00\",\"EndTime\":\"2022-01-02 11:00\",\"Price\":720.92,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 11:00\",\"StartTime\":\"2022-01-02 11:00\",\"EndTime\":\"2022-01-02 12:00\",\"Price\":681.53,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 12:00\",\"StartTime\":\"2022-01-02 12:00\",\"EndTime\":\"2022-01-02 13:00\",\"Price\":663.84,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 13:00\",\"StartTime\":\"2022-01-02 13:00\",\"EndTime\":\"2022-01-02 14:00\",\"Price\":483.02,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 14:00\",\"StartTime\":\"2022-01-02 14:00\",\"EndTime\":\"2022-01-02 15:00\",\"Price\":504.62,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 15:00\",\"StartTime\":\"2022-01-02 15:00\",\"EndTime\":\"2022-01-02 16:00\",\"Price\":771.32,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 16:00\",\"StartTime\":\"2022-01-02 16:00\",\"EndTime\":\"2022-01-02 17:00\",\"Price\":906.68,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 17:00\",\"StartTime\":\"2022-01-02 17:00\",\"EndTime\":\"2022-01-02 18:00\",\"Price\":981.26,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 18:00\",\"StartTime\":\"2022-01-02 18:00\",\"EndTime\":\"2022-01-02 19:00\",\"Price\":891.87,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 19:00\",\"StartTime\":\"2022-01-02 19:00\",\"EndTime\":\"2022-01-02 20:00\",\"Price\":733.06,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 20:00\",\"StartTime\":\"2022-01-02 20:00\",\"EndTime\":\"2022-01-02 21:00\",\"Price\":556.66,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 21:00\",\"StartTime\":\"2022-01-02 21:00\",\"EndTime\":\"2022-01-02 22:00\",\"Price\":474.79,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 22:00\",\"StartTime\":\"2022-01-02 22:00\",\"EndTime\":\"2022-01-02 23:00\",\"Price\":476.13,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-02 23:00\",\"StartTime\":\"2022-01-02 23:00\",\"EndTime\":\"2022-01-03 00:00\",\"Price\":338.71,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 00:00\",\"StartTime\":\"2022-01-03 00:00\",\"EndTime\":\"2022-01-03 01:00\",\"Price\":214.87,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 01:00\",\"StartTime\":\"2022-01-03 01:00\",\"EndTime\":\"2022-01-03 02:00\",\"Price\":204.89,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 02:00\",\"StartTime\":\"2022-01-03 02:00\",\"EndTime\":\"2022-01-03 03:00\",\"Price\":204.38,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 03:00\",\"StartTime\":\"2022-01-03 03:00\",\"EndTime\":\"2022-01-03 04:00\",\"Price\":203.86,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 04:00\",\"StartTime\":\"2022-01-03 04:00\",\"EndTime\":\"2022-01-03 05:00\",\"Price\":208.39,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 05:00\",\"StartTime\":\"2022-01-03 05:00\",\"EndTime\":\"2022-01-03 06:00\",\"Price\":301.89,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 06:00\",\"StartTime\":\"2022-01-03 06:00\",\"EndTime\":\"2022-01-03 07:00\",\"Price\":386.23,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 07:00\",\"StartTime\":\"2022-01-03 07:00\",\"EndTime\":\"2022-01-03 08:00\",\"Price\":912.96,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 08:00\",\"StartTime\":\"2022-01-03 08:00\",\"EndTime\":\"2022-01-03 09:00\",\"Price\":1264.42,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 09:00\",\"StartTime\":\"2022-01-03 09:00\",\"EndTime\":\"2022-01-03 10:00\",\"Price\":1133.18,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 10:00\",\"StartTime\":\"2022-01-03 10:00\",\"EndTime\":\"2022-01-03 11:00\",\"Price\":960.48,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 11:00\",\"StartTime\":\"2022-01-03 11:00\",\"EndTime\":\"2022-01-03 12:00\",\"Price\":933.22,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 12:00\",\"StartTime\":\"2022-01-03 12:00\",\"EndTime\":\"2022-01-03 13:00\",\"Price\":847.23,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 13:00\",\"StartTime\":\"2022-01-03 13:00\",\"EndTime\":\"2022-01-03 14:00\",\"Price\":832.11,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 14:00\",\"StartTime\":\"2022-01-03 14:00\",\"EndTime\":\"2022-01-03 15:00\",\"Price\":833.04,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 15:00\",\"StartTime\":\"2022-01-03 15:00\",\"EndTime\":\"2022-01-03 16:00\",\"Price\":1008.51,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 16:00\",\"StartTime\":\"2022-01-03 16:00\",\"EndTime\":\"2022-01-03 17:00\",\"Price\":1239.94,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 17:00\",\"StartTime\":\"2022-01-03 17:00\",\"EndTime\":\"2022-01-03 18:00\",\"Price\":1435.58,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 18:00\",\"StartTime\":\"2022-01-03 18:00\",\"EndTime\":\"2022-01-03 19:00\",\"Price\":1385.38,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 19:00\",\"StartTime\":\"2022-01-03 19:00\",\"EndTime\":\"2022-01-03 20:00\",\"Price\":1181,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 20:00\",\"StartTime\":\"2022-01-03 20:00\",\"EndTime\":\"2022-01-03 21:00\",\"Price\":955.64,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 21:00\",\"StartTime\":\"2022-01-03 21:00\",\"EndTime\":\"2022-01-03 22:00\",\"Price\":744.58,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 22:00\",\"StartTime\":\"2022-01-03 22:00\",\"EndTime\":\"2022-01-03 23:00\",\"Price\":405.77,\"Valuta\":\"SEK/MWh\"},{\"Area\":\"SE3\",\"Timestamp\":\"2022-01-03 23:00\",\"StartTime\":\"2022-01-03 23:00\",\"EndTime\":\"2022-01-04 00:00\",\"Price\":353.83,\"Valuta\":\"SEK/MWh\"}]","payloadType":"json","x":230,"y":540,"wires":[["4a1f3bd721238115","fe7a175f79248513"]]},{"id":"4a1f3bd721238115","type":"debug","z":"54efb553244c241f","name":"1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":430,"y":460,"wires":[]},{"id":"897d625f92fcfe39","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":540,"wires":[]},{"id":"fe7a175f79248513","type":"function","z":"54efb553244c241f","name":"","func":"\nlet now = new Date()\nnode.warn(now);\n\nmsg.payload.forEach(el => {\n\n if (now > new Date(el.StartTime) && now < new Date(el.EndTime)) {\n msg.payload = el\n }\n\n})\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":540,"wires":[["897d625f92fcfe39"]]}]