From f1967f24cd30b1fbf9955b700d959c3555377bd6 Mon Sep 17 00:00:00 2001 From: Claus Dethlefsen Date: Tue, 17 Mar 2026 06:59:06 +0100 Subject: [PATCH] fixes to get rid of startup messages --- .../automations/tesla_charge_failsafe.yaml | 18 ++++---- include/automations/tesla_smart_charging.yaml | 41 ++++++++++++----- include/automations/tesla_stop_when_full.yaml | 8 ++-- include/templates/tesla_charging.yaml | 9 ++-- include/templates/tesla_smart_charge.yaml | 45 ++++++++++--------- 5 files changed, 69 insertions(+), 52 deletions(-) diff --git a/include/automations/tesla_charge_failsafe.yaml b/include/automations/tesla_charge_failsafe.yaml index 7e71e44..d19fcd0 100644 --- a/include/automations/tesla_charge_failsafe.yaml +++ b/include/automations/tesla_charge_failsafe.yaml @@ -1,19 +1,19 @@ - alias: Tesla failsafe charge trigger: - - platform: time_pattern - minutes: "/5" - - condition: - - condition: template + - platform: template value_template: > {% set deadline_ts = as_timestamp(states('input_datetime.tesla_charge_deadline')) %} - {% set now_ts = as_timestamp(now()) %} - {{ (deadline_ts - now_ts) <= 3600 }} - + {% if deadline_ts %} + {% set now_ts = as_timestamp(now()) %} + {{ (deadline_ts - now_ts) <= 3600 }} + {% else %} + false + {% endif %} + condition: - condition: numeric_state entity_id: sensor.tesla_kwh_needed above: 0 - + value_template: "{{ states('sensor.tesla_kwh_needed')|float(0) }}" action: - service: switch.turn_on target: diff --git a/include/automations/tesla_smart_charging.yaml b/include/automations/tesla_smart_charging.yaml index cdd9fe9..cdc327e 100644 --- a/include/automations/tesla_smart_charging.yaml +++ b/include/automations/tesla_smart_charging.yaml @@ -1,44 +1,61 @@ - alias: Tesla smart charging v3 - mode: single + id: tesla_smart_charging_v3 + mode: restart trigger: - platform: time_pattern minutes: "/5" condition: - - condition: time - before: input_datetime.tesla_charge_deadline + # Ensure deadline is valid and in the future + - condition: template + value_template: > + {% set deadline = states('input_datetime.tesla_charge_deadline') %} + {{ deadline not in ['unknown','unavailable',''] and as_timestamp(deadline) > as_timestamp(now()) }} + + # Only run when car is connected + - condition: template + value_template: > + {{ states('binary_sensor.tesla_connected') == 'on' }} action: - choose: + # ------------------------- + # Charge if cheap + # ------------------------- - conditions: - - condition: state - entity_id: binary_sensor.tesla_charge_now - state: "on" + - condition: template + value_template: > + {{ states('binary_sensor.tesla_charge_now') == 'on' }} sequence: - # Reset hvis session står i connected_finished + # Reset charger if stuck in finished state - choose: - conditions: - condition: template - value_template: "{{ states('sensor.home_charger_mode') == 'connected_finished' }}" + value_template: > + {{ states('sensor.home_charger_mode') == 'connected_finished' }} sequence: - service: switch.turn_off target: entity_id: switch.home_charging - delay: "00:00:05" + # Turn charger on - service: switch.turn_on target: entity_id: switch.home_charging + # ------------------------- + # Stop charging if not cheap + # ------------------------- - conditions: - - condition: state - entity_id: binary_sensor.tesla_charge_now - state: "off" + - condition: template + value_template: > + {{ states('binary_sensor.tesla_charge_now') != 'on' }} sequence: - service: switch.turn_off target: - entity_id: switch.home_charging \ No newline at end of file + entity_id: switch.home_charging diff --git a/include/automations/tesla_stop_when_full.yaml b/include/automations/tesla_stop_when_full.yaml index 03d795d..8e0bea5 100644 --- a/include/automations/tesla_stop_when_full.yaml +++ b/include/automations/tesla_stop_when_full.yaml @@ -1,11 +1,11 @@ +# template for "Tesla stop when charged" - alias: Tesla stop when charged trigger: - platform: template value_template: > - {{ (states('sensor.snowywhite_battery') | default(0)) | float >= - (states('number.snowywhite_charge_limit') | default(100)) | float }} - + {{ (states('sensor.snowywhite_battery')|float(0)) >= + (states('number.snowywhite_charge_limit')|float(100)) }} action: - service: switch.turn_off target: - entity_id: switch.home_charging \ No newline at end of file + entity_id: switch.home_charging diff --git a/include/templates/tesla_charging.yaml b/include/templates/tesla_charging.yaml index ac01bce..edfa918 100644 --- a/include/templates/tesla_charging.yaml +++ b/include/templates/tesla_charging.yaml @@ -1,16 +1,15 @@ - sensor: - - name: tesla_kwh_needed unit_of_measurement: "kWh" state: > {% set capacity = 75 %} - {% set soc = states('sensor.snowywhite_battery')|float %} - {% set limit = states('number.snowywhite_charge_limit')|float %} + {% set soc = states('sensor.snowywhite_battery')|float(0) %} + {% set limit = states('number.snowywhite_charge_limit')|float(100) %} {{ ((limit - soc)/100 * capacity)|round(2) }} - name: tesla_charge_hours_needed unit_of_measurement: "h" state: > {% set power = 11 %} - {% set kwh = states('sensor.tesla_kwh_needed')|float %} - {{ (kwh / power)|round(2) }} \ No newline at end of file + {% set kwh = states('sensor.tesla_kwh_needed')|float(0) %} + {{ (kwh / power)|round(2) }} diff --git a/include/templates/tesla_smart_charge.yaml b/include/templates/tesla_smart_charge.yaml index 9d435ab..5d7a9c4 100644 --- a/include/templates/tesla_smart_charge.yaml +++ b/include/templates/tesla_smart_charge.yaml @@ -1,29 +1,30 @@ -binary_sensor: - - name: tesla_charge_now - state: > - {% set deadline_raw = states('input_datetime.tesla_charge_deadline') %} - {% set deadline_ts = as_timestamp(deadline_raw) if deadline_raw not in ['unknown','unavailable',''] else none %} +- binary_sensor: + - name: tesla_charge_now + state: > + {% set deadline_raw = states('input_datetime.tesla_charge_deadline') %} + {% set deadline_ts = as_timestamp(deadline_raw) if deadline_raw not in ['unknown','unavailable',''] else none %} - {% set hours_needed = states('sensor.tesla_charge_hours_needed') | float(0) | round(0, 'ceil') %} + {% set hours_needed = states('sensor.tesla_charge_hours_needed') | float(0) | round(0, 'ceil') %} - {% set prices = (state_attr('sensor.energidataservice','today') or []) - + (state_attr('sensor.energidataservice','tomorrow') or []) %} + {% set prices = (state_attr('sensor.energidataservice','today') or []) + + (state_attr('sensor.energidataservice','tomorrow') or []) %} - {# Filter valid price entries and apply deadline #} - {% set valid = [] %} - {% for p in prices %} - {% if p.hour is defined and p.hour %} - {% set ts = as_timestamp(p.hour) %} - {% if not deadline_ts or ts <= deadline_ts %} - {% set valid = valid + [p] %} + {# Filter valid price entries and apply deadline #} + {% set valid = [] %} + {% for p in prices %} + {% if p.hour is defined and p.hour %} + {% set ts = as_timestamp(p.hour) %} + {% if not deadline_ts or ts <= deadline_ts %} + {% set valid = valid + [p] %} + {% endif %} {% endif %} - {% endif %} - {% endfor %} + {% endfor %} - {# Pick cheapest hours #} - {% set cheapest = (valid | sort(attribute='price'))[:hours_needed] %} + {# Pick cheapest hours #} + {% set cheapest = (valid | sort(attribute='price'))[:hours_needed] %} - {# Current hour #} - {% set now_slot = now().replace(minute=0, second=0, microsecond=0).isoformat() %} + {# Current hour #} + {% set now_slot = now().replace(minute=0, second=0, microsecond=0).isoformat() %} - {{ cheapest | selectattr('hour','equalto',now_slot) | list | count > 0 }} + {{ cheapest | selectattr('hour','equalto',now_slot) | list | count > 0 }} +