-
-
Save JBoye/23cdeca2a5a94039cf0f2f2ad83d549d to your computer and use it in GitHub Desktop.
{% 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 I have fixed and issue that appeared when tomorrows prices where unavailable, but I'm not sure if that is what you are seeing.
How does your
state_attr("sensor.elpris", "today")
state_attr("sensor.elpris", "tomorrow")
look like?
[1.56, 1.5, 1.49, 1.42, 1.44, 1.3, 1.49, 1.52, 2.04, 2.01, 1.33, 1.29, 1.35, 1.4, 1.41, 1.49, 2.01, 2.22, 2.34, 2.16, 2.03, 1.87, 1.67, 1.4]
[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]
Before 12 my tomorrow attrib was blank, hence the error. Perhaps tomorrow prices are only populated after 12?
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: []
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?
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.
You are right, try setting departure_time to 20