- alias: Tesla smart charging v4 id: tesla_smart_charging_v4 mode: restart trigger: - platform: time_pattern minutes: "/5" condition: # Valid future deadline - condition: template value_template: > {% set deadline = states('input_datetime.tesla_charge_deadline') %} {{ deadline not in ['unknown','unavailable',''] and as_timestamp(deadline) > as_timestamp(now()) }} # Car must be connected - condition: state entity_id: binary_sensor.snowywhite_charger state: "on" action: - variables: charge_now: "{{ is_state('binary_sensor.tesla_charge_now', 'on') }}" charger_on: "{{ is_state('switch.home_charging', 'on') }}" - choose: # ------------------------- # START charging (cheap hour) # ------------------------- - conditions: - condition: template value_template: "{{ charge_now and not charger_on }}" sequence: - service: system_log.write data: message: "Tesla: START charging (cheap hour)" level: info - service: switch.turn_on target: entity_id: switch.home_charging # ------------------------- # STOP charging (not cheap) # ------------------------- - conditions: - condition: template value_template: "{{ not charge_now and charger_on }}" sequence: - service: system_log.write data: message: "Tesla: STOP charging (not cheap)" level: info - service: switch.turn_off target: entity_id: switch.home_charging # ------------------------- # Default: do nothing # ------------------------- - service: system_log.write data: message: > Tesla: No action (charge_now={{ charge_now }}, charger_on={{ charger_on }}) level: debug