Get lowest values of persistence (JS)

I have a time series item which contains energy prices of the next day and I would like to grab the lowest x prices of that day (with timestamp) and add them to other items.

With the help of Blockly I came up with this already. This code grabs all items of tomorrow and returns this string:

2024-10-18T00:00+02:00[Europe/Brussels],8.303,2024-10-18T00:15+02:00[Europe/Brussels],8.303,2024-10-18T00:30+02:00[Europe/Brussels],8.303,2024-10-18T00:45+02:00[Europe/Brussels],8.303,2024-10-18T01:00+02:00[Europe/Brussels],8.303,2024-10-18T01:15+02:00[Europe/Brussels],8.303,2024-10-18T01:30+02:00[Europe/Brussels],8.303,2024-10-18T01:45+02:00[Europe/Brussels],
.....
var now, prices_00_24, year, day, tomorrow, tomorrow_00, tomorrow_24, month;

now = (time.ZonedDateTime.now());
tomorrow = time.ZonedDateTime.now().plusDays(1);
year = (tomorrow.year());
month = (tomorrow.monthValue());
day = (tomorrow.dayOfMonth());
tomorrow_00 = time.ZonedDateTime.now().withYear(year).withMonth(month).withDayOfMonth(day).withHour(0).withMinute(0).withSecond(0).withNano(0);
tomorrow_24 = time.ZonedDateTime.now().withYear(year).withMonth(month).withDayOfMonth(day).withHour(23).withMinute(59).withSecond(59).withNano(0);
console.info(tomorrow_00);
console.info(tomorrow_24);

prices_00_24 = items.getItem('elia_data').persistence.getAllStatesBetween(tomorrow_00 ,tomorrow_24, 'influxdb').map(v => ([v.timestamp, v.numericState]));
console.info(prices_00_24);

But I don’t have a clue how to continue…

Actually, it’s not a String. It’s an array of two element arrays (first element is the timestamp, second is the value). You’ll need to use a loop and variables to loop through the array to find the minimum value and tiemstamp for that Item.

It will look something like this:

I was able to get the minimum value from my array.
output of the code:

minValue: 2024-10-23T04:00+02:00[Europe/Brussels],7.256

But how can I loop this multiple times (eg 5 times) to get the 2nd lowest value, 3rd lowest… and store it in a new array?
Or maybe it’s easier to sort my array based on the value and then get the first X of them?

This.

And how do I sort this array on the 2nd element?

I tried something like this, but now I’m trying to sort only the first item of the array.

This might not be easy to do through Blockly because it is an array of key/value pairs. You might need to use the script block.

prices_00_24 = prices_00_24.sort((a, b) => {
  if(a[1] > b[1]) return 1;
  else if(a[1] < b[1]) return -1;
  return 0;
})

There are some things that Blockly doesn’t easily support related to lists and dicts.

Thanks, I don’t unstand the sorting completely, but it worked!

current output:

2024-10-23 21:22:22.704 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 14:00: 7.108
2024-10-23 21:22:22.708 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 03:00: 7.173
2024-10-23 21:22:22.712 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 13:00: 7.195
2024-10-23 21:22:22.715 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 04:00: 7.26
2024-10-23 21:22:22.719 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 02:00: 7.584
2024-10-23 21:22:22.722 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 12:00: 7.713
2024-10-23 21:22:22.726 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 05:00: 7.775
2024-10-23 21:22:22.729 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 01:00: 7.814
2024-10-23 21:22:22.733 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 16:00: 7.92
2024-10-23 21:22:22.736 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 23:00: 8.104
2024-10-23 21:22:22.739 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 15:00: 8.122
2024-10-23 21:22:22.743 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 00:00: 8.333
2024-10-23 21:22:22.746 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 06:00: 8.362
2024-10-23 21:22:22.750 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 11:00: 8.643
2024-10-23 21:22:22.754 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 20:00: 9.06
2024-10-23 21:22:22.757 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 17:00: 9.169
2024-10-23 21:22:22.760 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 21:00: 9.29
2024-10-23 21:22:22.764 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 10:00: 9.539
2024-10-23 21:22:22.768 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 22:00: 9.605
2024-10-23 21:22:22.771 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 09:00: 10.6
2024-10-23 21:22:22.775 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 07:00: 10.672
2024-10-23 21:22:22.778 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 18:00: 10.732
2024-10-23 21:22:22.782 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 19:00: 11.645
2024-10-23 21:22:22.785 [INFO ] [.automation.script.ui.getLowestPrice] - 2024-10-24 08:00: 14.097

It should return a number where:

  • A negative value indicates that a should come before b.
  • A positive value indicates that a should come after b.
  • Zero or NaN indicates that a and b are considered equal.

I could have written it shorter but I wrote it out long form to make it more clear how it works. The short form would be

prices_00_24 = prices_00_24.sort((a, b) => a[1]-b[1]);
  • a[1] and b[1]: the sort() method will call this function with two arguments, a and b which are two elements of the array. We know that the elements of the array is itself a two element array where the value is the second element. We want to sort by the second element so we compare the second element which is index [1].
2 Likes

Thanks for clarifying!