Last active
November 19, 2021 08:05
-
-
Save JBoye/23cdeca2a5a94039cf0f2f2ad83d549d to your computer and use it in GitHub Desktop.
Elpris WIP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
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
New version added with variable battery_level , battery_capacity and charging_speed
Probably doesn't handle tomorrows prices very well
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
That should be handled by
{%if state_attr("sensor.elpris", "tomorrow_valid") %}
Are you still seeing the same error?