Creating a script with cheapest hour needed

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

It depends on what you want to do with this information.
If you just want to display it, use a markdown card.
If you want a sensor with attributes (or multiple sensors), template sensor is the way to go.

By default, a template sensor’s state updates each time one of the referenced entities’ state changes. If you don’t want that, you can use a trigger-based template sensor.

THX the thing I so far haven’t figured out is how do I put the attributes into the sensor without have to write the math several times?

if I want this:

charge_made: {{ now().strftime('%H:%M den %d/%m') }}
charge_time: {{ ev_car_charge_time_hour - 1 }} time(r) og {{ ev_car_charge_time_min  % 60 }} min
kWh_needed: {{ ev_car_charge_kwh | round(1) }} kWh
charge_price: {{ (ev_car_charge_kwh * (best_interval_price| average))| round(0, 'ceil') }} kr
kWh_avg_price: {{ (best_interval_price| average)| round(2) }} kr
start_charger: {{ ev_car_start_charge_datetime.strftime('%Y-%m-%dT%H:%M') }}
stop_charger: {{ ev_car_stop_charge_datetime.strftime('%Y-%m-%dT%H:%M') }}

in as attributes and then update the sensor when the car is pluged in. And then I have some treshold values in helpers I want to use + call the template with values, eg: car_bat_size, car_type. We have 2 EV cars

I don’t think it is currently possible to have a variable shared between multiple attribute templates.
There’s a feature request for variables in template sensors:

For now, I think you’ll have to live with some code duplication.
Maybe you could split the main logic to a “base” sensor and derive the attributes off of it’s state.