- alias: Tesla set charge deadline at 13:00
  id: tesla_set_charge_deadline_daily
  trigger:
    - platform: time
      at: "13:05:00"

  condition:
    - condition: template
      value_template: >
        {% set deadline = states('input_datetime.tesla_charge_deadline') %}
        {{ deadline in ['unknown','unavailable',''] or as_timestamp(deadline) <= as_timestamp(now()) }}

  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: input_datetime.tesla_charge_deadline
      data:
        datetime: >
          {{ (now() + timedelta(days=1))
             .replace(hour=7, minute=0, second=0, microsecond=0) }}

  mode: single


- alias: Tesla set charge deadline when plugged in
  id: tesla_set_charge_deadline_on_plug
  trigger:
    - platform: state
      entity_id: binary_sensor.snowywhite_charger
      to: "on"

  condition:
    - condition: template
      value_template: >
        {% set deadline = states('input_datetime.tesla_charge_deadline') %}
        {{ deadline in ['unknown','unavailable',''] or as_timestamp(deadline) <= as_timestamp(now()) }}

  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: input_datetime.tesla_charge_deadline
      data:
        datetime: >
          {{ (now() + timedelta(days=1))
             .replace(hour=7, minute=0, second=0, microsecond=0) }}

  mode: single
