Hi there
Trying to get a script that can calculate the time my charger for the car needs to run before tomorrow, but I am kind of lost, have this running in template, but how do I get it to only run when I want to (do not need a template)? and how do I get the diffrent values into som sensors???
{% set today_price = states.sensor.nordpool_kwh_dk1_dkk_2_095_025.attributes.today %}
{% set tomorrow_price = states.sensor.nordpool_kwh_dk1_dkk_2_095_025.attributes.tomorrow %}
{% set ev_car_battery_size = 89 -%}
{% set ev_car_end_charge_level = 100 -%}
{% set ev_car_battery_charge_speed = 10 -%}
{% set ev_car_charge_kwh = (ev_car_battery_size / 100 * (ev_car_end_charge_level - states('sensor.ford_hv_battery_percent') | float)) %}
{% set ev_car_charge_time_min = ((ev_car_charge_kwh / ev_car_battery_charge_speed) * 60) | round(0) -%}
{% set ev_car_charge_time_hour = (ev_car_charge_time_min / 60) | round(0, 'ceil') %}
{% set future_price = today_price[now().hour:] + tomorrow_price -%}
{% set inteval_start_tomorrow = 0 -%}
{% set interval_start_hour = now().hour + 1 -%}
{% set interval_end_tomorrow = 1 -%}
{% set interval_end_hour = 23 -%}
{% set low_interval_length = ev_car_charge_time_hour -%}
{% set all_price = today_price + tomorrow_price -%}
{% set interval_price = all_price[interval_start_hour + 24 * inteval_start_tomorrow : interval_end_hour + 24 * interval_end_tomorrow] -%}
{% set last_considered_interval_index = (interval_price | length) - low_interval_length + 1 -%}
{% set ns = namespace(current_best_average=1000, current_best_index=-1) -%}
{% for price in interval_price[:last_considered_interval_index] -%}
{% set current_average = interval_price[loop.index0:loop.index0+low_interval_length] | average -%}
{% if current_average < ns.current_best_average -%}
{%-set ns.current_best_average = current_average -%}
{% set ns.current_best_index = loop.index0 -%}
{% endif -%}
{% endfor -%}
{%- set start_index = ns.current_best_index %}
{%- set best_interval_price = interval_price[start_index:start_index + low_interval_length] %}
{% set best_interval_start_hour = 24 * inteval_start_tomorrow + interval_start_hour + start_index -%}
{%- set best_interval_price = interval_price[start_index:start_index + low_interval_length] %}
{% set ev_car_start_charge_datetime = (now() + timedelta(hours=best_interval_start_hour - now().hour)) - timedelta(minutes = now().minute) %}
{% set ev_car_stop_charge_datetime = ev_car_start_charge_datetime + timedelta(minutes=ev_car_charge_time_min+10) %}
{% if ev_car_end_charge_level > states('sensor.fordhv_battery_percent') | float -%}
Mustang GT Ladning: {{ states('sensor.ford_hv_battery_percent') | float }}% til {{ ev_car_end_charge_level }}%
Beregning udført : {{ now().strftime('%H:%M den %d/%m') }}
Ladetid : {{ ev_car_charge_time_hour - 1 }} time(r) og {{ ev_car_charge_time_min % 60 }} min
Hvor meget strøm : {{ ev_car_charge_kwh | round(1) }} kWh
Det koster ca : {{ (ev_car_charge_kwh * (best_interval_price| average))| round(0, 'ceil') }} kr
kWh prisen er : {{ (best_interval_price| average)| round(2) }} kr
Start lader : {{ ev_car_start_charge_datetime.strftime('%Y-%m-%dT%H:%M') }}
Stop lader : {{ ev_car_stop_charge_datetime.strftime('%Y-%m-%dT%H:%M') }}
{% else -%}
Mangler ikke ladning
{% endif -%}
Hopefull sombody can help me in a directons, just needs a idea or direction how to solve it. I need a lot more than this, but this is to get me started.
BTW it is my first template…
BR
Tue