Skip to content

Instantly share code, notes, and snippets.

@stefan736
Last active December 30, 2025 12:05
Show Gist options
  • Select an option

  • Save stefan736/d752ddb3fc1dfa7419c5aae5e941b5df to your computer and use it in GitHub Desktop.

Select an option

Save stefan736/d752ddb3fc1dfa7419c5aae5e941b5df to your computer and use it in GitHub Desktop.
HomeAssistant Template für Sensor Ermittlung günstigste E-Auto Ladezeit
{% set charging_duration = states('sensor.mokka_automatisch_laden_dauer_berechnet') | float %}
{% set prices = state_attr('sensor.tibber_prices', 'tomorrow') %}
{% set ns = namespace(
cheapest_index = 0,
cheapest_costs = 999999,
charging_window_costs = 0
) %}
{# How many prices are in the charging window? #}
{% set charging_window_size = (charging_duration / 15) | round(0, 'ceil') %}
{# How many prices do we need to check between 0:00 -6:00 ? #}
{% set prices_to_check = (6 * 4) - charging_window_size %}
{# For each 15min timeslot #}
{% for i in range(0, prices_to_check + 1) %}
{# Calculate the charging costs in this window #}
{% set ns.charging_window_costs = 0 %}
{% for chunk in range(0, charging_window_size) %}
{% set ns.charging_window_costs = ns.charging_window_costs + prices[i + chunk].total %}
{% endfor %}
{# Remember if calculated window is cheaper than before #}
{% if ns.charging_window_costs < ns.cheapest_costs %}
{% set ns.cheapest_index = i %}
{% set ns.cheapest_costs = ns.charging_window_costs %}
{% endif %}
{% endfor %}
{# Calculate the starttime #}
{{ (today_at('00:00:00') + timedelta(minutes=(ns.cheapest_index * 15))).strftime('%H:%M') }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment