How to select current object from an array?

Hi,

obvious newbie-post, but with too little experience from JS, one has to start somewhere...
I have an array of objects as per below showing the price of electricity every hour. The array contains this day and tomorrows prices, and I would like to select the one which is valid for now.

Obviously I need to store a proper TimeStamp, possibly type cast it to something similar to StartTime and EndTime as below and then loop through the objects in the array and select the object where StartTime < TimeStamp < EndTime

How on earth would something like that be written in JS? any pointers to ReadThisFirst?

object

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"

Hi Niklas,

Would it possible to post the whole array instead of one object element from it ?
(That would be more helpful to test this and recommend something)

From the Debug window hover over the array and use the Copy value

image

Sure, the whole array below. I take it that what I would like to achieve is clear?

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

emmm :sweat_smile: .. you want to return the object that has a Timestamp closest to now ?

Here is an example that uses a chnage node and JSonata, I had to adjust your timestamps to ISO 8601 timestamps, theen i returned all elements where The timestamp was greater than now. As the original array was in order i9 did not order it, I just selected the last element of the returned array.

[{"id":"7c7ae91b.16d2c8","type":"inject","z":"1378ba83.90c825","name":"","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":270,"y":840,"wires":[["57afca0c.57fd2c"]]},{"id":"57afca0c.57fd2c","type":"change","z":"1378ba83.90c825","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload[$now() > $replace($.StartTime,\" \", \"T\") & \":00.000\"][-1]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":840,"wires":[["f5f352b9.c4ef68"]]},{"id":"f5f352b9.c4ef68","type":"debug","z":"1378ba83.90c825","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":840,"wires":[]}]

JSONata expression

$$.payload[
   $now() > $replace($.StartTime," ", "T") & ":00.000"
][-1]
1 Like

Yes, that was the plan.

I am thinking of something like this, but not really sure of whether there is a now()-function... or similar. And if there is, I am quite sure it is of different types than StartTime and EndTime

for (var i = 0; i<msg.payload.length;i++){
    if msg.payload[i].StartTime < now(){
        if msg.payload[i].EndTime > now(){
            msg1 = {
                topic:msg.payload[i].currency, 
                payload:msg.payload[i].price, 
                timestamp:msg.payload[i].timestamp
            };
            node.send(msg1);
        }
    }
}

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"]]}]
1 Like

Thanks for helping out, will try the suggestions later tonight.

The reason for nesting is just readability while trying.

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