Help return array index

Hey all

I got this fairly simple flow that takes in an array of todays energi prices and returns the minimum and maximum value, this is all well and good but i need to know when the prices are high and low, can anyone help me get the index number out with the value ?

bonus question!
It doesn't help me a lot to wake up in the morning and see the the cheepest time to starte the washer was 3 hours ago, is there any way to compare the index number to the hour of the day and only look through index numbers equal to og higher ?

Thanks

ps. this is my first question here, if i f-ed up the format pls let me know.

[{"id":"6c44665eec3e570d","type":"api-current-state","z":"ec94b9448b1541aa","name":"Pris","server":"59eab6e.e46dc48","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.energi_data_service","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":310,"y":320,"wires":[["6d55f8d530d339fc","b5c298755ab028c0"]]},{"id":"239d9afbf19b5ccb","type":"debug","z":"ec94b9448b1541aa","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":300,"wires":[]},{"id":"6d55f8d530d339fc","type":"function","z":"ec94b9448b1541aa","name":"Max","func":"{const array1 = msg.data.attributes.today\n\n    msg.payload = (Math.round(Math.max(...array1)) / 100) + \" Kr/kWh\"\n\nreturn msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":300,"wires":[["239d9afbf19b5ccb"]]},{"id":"e79df40960824a3f","type":"debug","z":"ec94b9448b1541aa","name":"debug 11","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":340,"wires":[]},{"id":"b5c298755ab028c0","type":"function","z":"ec94b9448b1541aa","name":"Min","func":"{const array1 = msg.data.attributes.today\n\nmsg.payload = (Math.round(Math.min(...array1))/100) + \" Kr/kWh\"\n\nreturn msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":340,"wires":[["e79df40960824a3f"]]},{"id":"2628bf3e25567747","type":"inject","z":"ec94b9448b1541aa","name":"","props":[{"p":"time","v":"15","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":320,"wires":[["6c44665eec3e570d"]]},{"id":"59eab6e.e46dc48","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

Your flow.json is corrupt it needs to be posted between triple backticks
e.g.
```
code
```
As to min max and index , you have not given an example of the array, so i can only give you an example of a simple array, and how to search for min /max and their index.

[{"id":"1afec0d9.9b7c4f","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[5,4,3,2,1,6,7,9,8]","payloadType":"json","x":160,"y":3700,"wires":[["fff21b01.f17078"]]},{"id":"fff21b01.f17078","type":"function","z":"bf9e1e33.030598","name":"","func":"let min = {value: msg.payload[0], index:0};\nlet max = {value: msg.payload[0], index:0};\nmsg.payload.forEach((int, index) =>{\n    if(int > max.value) max = {value: int, index: index};\n    if(int < min.value) min = {value: int, index: index}\n})\nmsg.payload = {min, max}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":3700,"wires":[["53281e53.747e4"]]},{"id":"53281e53.747e4","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":3700,"wires":[]}]
let min = {value: msg.payload[0], index:0};
let max = {value: msg.payload[0], index:0};
msg.payload.forEach((int, index) =>{
    if(int > max.value) max = {value: int, index: index};
    if(int < min.value) min = {value: int, index: index};
})
msg.payload = {min, max};
return msg;
1 Like

thanks a bunch :smiley:

with just the payload changed to data.attributes.today i got exactly what i wanted :ok_hand:

an example of the array could just be this, the index numbers are hours of the day and the value is the price of a kWh

Skærmbillede 2022-09-17 kl. 20.19.49

have you got any ideas on getting the index number compared to the current hour so i can get the upcomming cheepest price ?

Then get the hour of day and only look at indices above or equal to the hour.

const hour = new Date().getHours()
let min = {value: msg.payload[hour], index:hour};
let max = {value: msg.payload[hour], index:hour};
msg.payload.forEach((int, index) =>{
    if(index >= hour){
        if(int > max.value) max = {value: int, index: index};
        else if(int < min.value) min = {value: int, index: index}
    }
})
msg.payload = {min, max}
return msg;

Or to minimise the loop and start search at hour index ( not that this small loop really matters)

let search_array = msg.payload;
let index = new Date().getHours();
let min = {value: search_array[index], index: index};
let max = {value: search_array[index], index: index};
for(index; index < 24; index++){
    let int = search_array[index];
    if(int > max.value) max = {value: int, index: index};
    else if(int < min.value) min = {value: int, index: index}
}
msg.payload = {min, max}
return msg;
1 Like

everything is working now, thanks a lot :smiley:

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