Nordpool - X cheapest hours in a row

This code creates the three cheapest hours in a row.
I want to be able to change the number of hours with a ‘helper’.
Anyone know how to do it?

  • platform: template
    sensors:
    nordpool3billigaste:
    friendly_name: “Nordpool 3 cheapest”
    value_template: >
    {% set tod=state_attr(‘sensor.nordpool_kwh_se4_sek_3_095_025’,‘raw_today’) %}
    {% set ns = namespace(value=0, amount=1000, index=0) %}
    {% for i in range(0, 22) %}
    {% set ns.value = tod[i].value + tod[i+1].value + tod[i+2].value %}
    {% if (ns.value < ns.amount) %}
    {% set ns.index = i %}
    {% set ns.amount = ns.value %}
    {% endif %}
    {% endfor %}
    {{ tod[ns.index].start.strftime("%H:%M") }} - {{ tod[ns.index+2].end.strftime("%H:%M") }}
2 Likes