This is probably easier than I’m making it in my head…
I have an entity with an attribute that is an array of numbers (hourly energy prices with the Nordpool integration). I can figure out how to store each hour into a separate entity using a for loop.
But what I want to do is rank them. So let’s say 6am is the cheapest rate, I want [0] to be “6”. Any tips on how to go about this? I would end up with 24 entities per day, from cheapest electricity to most expensive.
Then what I want to achieve is that I can tell an automation to run only if “rate < 5” for example, or not to run if rate > X.
This is a step more than simply telling an automation to run during the cheapest hour, because that’s quite simple, but won’t achieve my goals.
Anyone done this before or have any tips how to procees?
That was a bit harder than expected. Drop this in the template editor and have a play. First line is a guess at what you need to replace the hardcoded second line I was playing with.
{% set prices = state_attr('sensor.nordpool_kwh_nl_eur_4_10_021','today') %}
{% set prices = [0.1332,0.1246,0.1168,0.109,0.0997,0.1077,0.1249,0.178,0.2097,0.2016,0.1693,0.1367,0.1573,0.1588,0.1735,0.1815,0.1814,0.2226,0.2352,0.2178,0.2017,0.219,0.1741,0.1851] %}
{% set ns = namespace(str="{") %}
{% for price in prices %}
{% set ns.str = ns.str ~ "\"" ~
price+[0,(loop.index0/1000000)][(prices.count(price) > 1)] ~ "\":" ~
loop.index0 ~ "," %}
{% endfor %}
{% set ns.str = ns.str[:-1] ~ "}" %}
{{ ns.str|from_json|dictsort|map(attribute=1)|list }}
There are a couple of simpler options, but they fall over if two time slots are the same price. I’ve worked around this here by adding a different tiny amount to any identical prices before doing the ranking, so earlier identical price slots are favoured. This assumes that the adjusted amounts are not going to form a new identical pair!
Sadly, our options for constructing dictionaries are limited in Jinja2, so I have faked it up by building a string of {price: hour} pairs and converting it to a JSON object before sorting by price and pulling out the hours.
Thanks, this is helping me a lot to get my head around how these templates work and how to manage these entities.
While playing around I also came up with a different method. This then doesn’t create a new list, but instead searches it every time it’s needed. Not yet sure how it would handle multiples of the same value, but that shouldn’t happen anyways, I’m using up to 5 decimals.
Useful for other contexts because I’ll still need the sorted list for telling me “14:00 - 15:00 is the cheapest hour” I think.
{% set prices = state_attr('sensor.nordpool_kwh_nl_eur_4_10_021','today')|sort() %}
{% set currentrank = prices.index(states('sensor.nordpool_kwh_nl_eur_4_10_021')|float()) %}
{% if currentrank <= 12 %}
{% endif %}
I haven’t played with them both yet, will try it out first before I ask so at least if I need to my question will be clear Now that I’ve figured out this part, I need to figure out the logic part for the automation first…