Skip to content

Instantly share code, notes, and snippets.

@elden1337
Last active March 2, 2023 10:07
Show Gist options
  • Save elden1337/90724cb233b9bb354dd4e1f744c17cd6 to your computer and use it in GitHub Desktop.
Save elden1337/90724cb233b9bb354dd4e1f744c17cd6 to your computer and use it in GitHub Desktop.
Home assistant sensors to show cheapest average period
#requires Nordpool integraiton through HACS
- platform: template
sensors:
next_dryer:
value_template: >
{%set duration = 3%}
{%set ret_dict = namespace(value = []) %}
{%set grow = namespace(value=0)%}
{%set prices = state_attr('sensor.nordpool_kwh_se3_sek_3_10_025','today')|list%}
{%set tomorrow = state_attr('sensor.nordpool_kwh_se3_sek_3_10_025','tomorrow')|list%}
{%set currency = state_attr('sensor.nordpool_kwh_se3_sek_3_10_025','currency')%}
{%if tomorrow | length > 1%}
{%set prices = prices + tomorrow%}
{%endif%}
{%for p in prices%}
{%if loop.index+duration <= prices|length and loop.index >= now().hour%}
{%set _internal_counter = -1%}
{%set _outer_loop = loop.index%}
{%for n in range(duration)%}
{%set grow.value = grow.value + prices[_outer_loop+_internal_counter+n]%}
{%endfor%}
{%set _start = (loop.index - 24) if loop.index > 23 else loop.index%}
{%set _end = ((loop.index+duration)- 24) if (loop.index+duration) > 23 else loop.index+duration%}
{%set ret1 = ('0' +_start|string) if _start < 10 else _start|string%}
{%set ret2 = ('0' +_end|string) if _end < 10 else _end|string%}
{%set rret1 = ret1+':00' if _start >= now().hour else ret1 + ':00⁺¹' %}
{%set rret2 = ret2+':00' if _end >= now().hour else ret2 + ':00⁺¹' %}
{%set val = grow.value|float/duration %}
{%set ret = rret1 +'-'+ rret2 + ' (' +val|round(3)|string +' ' +currency+')' %}
{%set ret_dict.value = ret_dict.value + [(val, ret)]%}
{%set grow.value = 0%}
{%endif%}
{%endfor%}
{% set data = dict.from_keys(ret_dict.value) %}
{% set data = data.items()|sort %}
{{data[0][1]}}
attribute_templates:
all: >
{%set duration = 3%}
{%set ret_dict = namespace(value = []) %}
{%set grow = namespace(value=0)%}
{%set prices = state_attr('sensor.nordpool_kwh_se3_sek_3_10_025','today')|list%}
{%set tomorrow = state_attr('sensor.nordpool_kwh_se3_sek_3_10_025','tomorrow')|list%}
{%set currency = state_attr('sensor.nordpool_kwh_se3_sek_3_10_025','currency')%}
{%if tomorrow | length > 1%}
{%set prices = prices + tomorrow%}
{%endif%}
{%for p in prices%}
{%if loop.index+duration <= prices|length and loop.index >= now().hour%}
{%set _internal_counter = -1%}
{%set _outer_loop = loop.index%}
{%for n in range(duration)%}
{%set grow.value = grow.value + prices[_outer_loop+_internal_counter+n]%}
{%endfor%}
{%set _start = (loop.index - 24) if loop.index > 23 else loop.index%}
{%set _end = ((loop.index+duration)- 24) if (loop.index+duration) > 23 else loop.index+duration%}
{%set ret1 = ('0' +_start|string) if _start < 10 else _start|string%}
{%set ret2 = ('0' +_end|string) if _end < 10 else _end|string%}
{%set rret1 = ret1+':00' if _start >= now().hour else ret1 + ':00⁺¹' %}
{%set rret2 = ret2+':00' if _end >= now().hour else ret2 + ':00⁺¹' %}
{%set val = grow.value|float/duration %}
{%set ret = rret1 +'-'+ rret2 + ' (' +val|round(3)|string +' ' +currency+')' %}
{%set ret_dict.value = ret_dict.value + [(val, ret)]%}
{%set grow.value = 0%}
{%endif%}
{%endfor%}
{% set data = dict.from_keys(ret_dict.value) %}
{% set data = data.items()|sort %}
{% set data2 = namespace(value =[])%}
{%for d in data%}
{%set data2.value = data2.value + [d[1]]%}
{%endfor%}
{{data2.value}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment