Skip to content

Instantly share code, notes, and snippets.

@JBoye
Last active November 19, 2021 08:05
Show Gist options
  • Save JBoye/23cdeca2a5a94039cf0f2f2ad83d549d to your computer and use it in GitHub Desktop.
Save JBoye/23cdeca2a5a94039cf0f2f2ad83d549d to your computer and use it in GitHub Desktop.
Elpris WIP
{% set battery_level = states("sensor.jenny_battery_level") | float %}
{% set departure_time = 16 %} {# could be calculated or another sensor #}
{% set current_time = now().hour %}
{% set departure_time = departure_time + 24 if departure_time <= current_time else departure_time %}
{{departure_time}}
{% set battery_capacity = 10 %} {# kWh #}
{% set charging_speed = 3.7 %} {# kW #}
{% set charging_time = ((100 - battery_level) / 100) * battery_capacity / charging_speed %}
{% set ns = namespace(prices = []) %}
{% set prices = state_attr("sensor.elpris", "today") | list + state_attr("sensor.elpris", "tomorrow") | list if state_attr("sensor.elpris", "tomorrow_valid") else state_attr("sensor.elpris", "today") %}
{% set start_index = current_time | int %}
{% set end_index = ([departure_time, prices | length] | min - charging_time) | int + 1 %}
{% for index in range(start_index, end_index) -%}
{%- set price = prices[index] * charging_speed -%}
{%- set ns.current_total = 0 -%}
{%- for i in range(index, index + charging_time | int + 1) -%} {# loop every hour in charge time#}
{%- set hour_price = prices[i] * charging_speed * [1, (index + charging_time) - i] | min %}
{%- set ns.current_total = ns.current_total + hour_price %}
KL {{i % 24}}: {{"%.2f"|format(hour_price)}}kr
{%-endfor -%}
{% set ns.prices = ns.prices + [ns.current_total] %}
kl {{index % 24}} - {{(index + charging_time | round(0, 'ceil')) % 24 }}: {{"%.2f"|format(ns.current_total)}}kr
{% endfor %}
Batteri: {{battery_level}}%
Ladetid: {{ (charging_time ) | int }}t {{((charging_time * 60) % 60) | int}}m
{% if ns.prices | length > 0 %}
Lige nu: {{ "%.2f"|format(ns.prices[0]) }}kr
Billigst: kl {{'{:0>2}'.format((ns.prices.index(ns.prices | min) + start_index) % 24)}}, {{ "%.2f"|format(ns.prices | min) }}kr
Dyrest: kl {{'{:0>2}'.format((ns.prices.index(ns.prices | max) + start_index) % 24)}}, {{ "%.2f"|format(ns.prices | max) }}kr
{% endif %}
{% set charging_time = 2 %}
{% set departure_time = 8 %} {# could be calculated or another sensor #}
{% set current_hour = now().hour %}
{% set ns = namespace(prices = []) %}
{% set prices = state_attr("sensor.elpris", "today") | list %} {# nordpool sensors #}
{%if state_attr("sensor.elpris", "tomorrow_valid") %}
{% set prices = prices + state_attr("sensor.elpris", "tomorrow") | list %}
{% endif %}
{% set start_index = current_hour %}
{% set end_index = 1 + departure_time - charging_time %}
{% set end_index = end_index + 24 if departure_time <= current_hour else end_index %}
{% set end_index = [end_index, prices | length - 1] | min %}
{% for index in range(start_index, end_index) -%}
{%- set price = prices[index] -%}
{%- set ns.current_total = 0 -%}
{%- for i in range(index, index + charging_time) -%} {# loop every hour in charge time#}
{%- set ns.current_total = ns.current_total + prices[i] -%}
{%-endfor -%}
{% set ns.prices = ns.prices + [ns.current_total] -%}
{{'{:0>2}'.format(index % 24) }} - {{'{:0>2}'.format((index + charging_time) % 24) }}: {{ "%.2f"|format(ns.current_total) }}kr
{% endfor %}
{% if ns.prices | length > 0 %}
Lige nu: {{ "%.2f"|format(ns.prices[0]) }}kr
Billigst: kl {{'{:0>2}'.format((ns.prices.index(ns.prices | min) + start_index) % 24)}}, {{ "%.2f"|format(ns.prices | min) }}kr
Dyrest: kl {{'{:0>2}'.format((ns.prices.index(ns.prices | max) + start_index) % 24)}}, {{ "%.2f"|format(ns.prices | max) }}kr
{% endif %}
elpris:
template:
- trigger:
- platform: time_pattern
minutes: 0
binary_sensor:
- name: "Billigste elpris"
state: >
{% set charging_time = 2 %}
{% set departure_time = 8 %} {# could be calculated or another sensor #}
{% set current_hour = now().hour %}
{% set ns = namespace(prices = []) %}
{% set prices = state_attr("sensor.elpris", "today") | list %} {# nordpool sensors #}
{%if state_attr("sensor.elpris", "tomorrow_valid") %}
{% set prices = prices + state_attr("sensor.elpris", "tomorrow") | list %}
{% endif %}
{% set start_index = current_hour %}
{% set end_index = 1 + departure_time - charging_time %}
{% set end_index = end_index + 24 if departure_time <= current_hour else end_index %}
{% set end_index = [end_index, prices | length] | min %}
{% for index in range(start_index, end_index) -%}
{%- set price = prices[index] -%}
{%- set ns.current_total = 0 -%}
{%- for i in range(index, index + charging_time) -%} {# loop every hour in charge time#}
{%- set ns.current_total = ns.current_total + prices[i] -%}
{%-endfor -%}
{%- set ns.prices = ns.prices + [ns.current_total] -%}
{%- endfor -%}
{% if ns.prices | count == 0 or ns.prices[0] > ns.prices | min %}False{% else %}True{% endif %}
@MTrab
Copy link

MTrab commented Nov 12, 2021

Today (since 6am) the values are as follows - guess tomorrow will be empty until 12pm.

Today: [1.56, 1.53, 1.51, 1.42, 1.35, 1.27, 1.68, 1.98, 2.0, 1.93, 1.69, 1.63, 1.44, 1.37, 1.32, 1.49, 1.55, 0.96, 0.94, 0.89, 0.83, 0.83, 0.8, 0.74]
Tomorrow: []

@JBoye
Copy link
Author

JBoye commented Nov 12, 2021

Today (since 6am) the values are as follows - guess tomorrow will be empty until 12pm.

Today: [1.56, 1.53, 1.51, 1.42, 1.35, 1.27, 1.68, 1.98, 2.0, 1.93, 1.69, 1.63, 1.44, 1.37, 1.32, 1.49, 1.55, 0.96, 0.94, 0.89, 0.83, 0.83, 0.8, 0.74]
Tomorrow: []

That should be handled by {%if state_attr("sensor.elpris", "tomorrow_valid") %}

Are you still seeing the same error?

@MTrab
Copy link

MTrab commented Nov 12, 2021

tomorrow_valid: True
tomorrow: [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]

Perhaps a check for something else would be better :D

@JBoye
Copy link
Author

JBoye commented Nov 12, 2021

New version added with variable battery_level , battery_capacity and charging_speed

Probably doesn't handle tomorrows prices very well

@MTrab
Copy link

MTrab commented Nov 19, 2021

Just added a pull to Nordpool component, as the tomorrow_valid attribute always renders true, even when no data is available.
After the fix in the pull, this works like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment