Template light - how to handle the delay between the action and state change

Hi all:
I’m new to HA and I need some help to configure a template light with mimic my car charger, I can then expose the light to HomeKit as a way to control on/off of my charger. Basically I’d like to achieve the following:

  • The light allows user or automation to turn on/off, set level change should be ignore unless it’s for turning on/off the light
  • It gets the realtime power from the charger and display as brightness level, so the level should be read only

I have come up with the following template:

- light:
    - name: "Car Charger"
      unique_id: outdoor_evcharger
      turn_on:
        - service: input_boolean.turn_on
          target:
            entity_id: input_boolean.charge_car
        - delay: "00:02:00"
      turn_off:
        - service: input_boolean.turn_off
          target:
            entity_id: input_boolean.charge_car
        - delay: "00:02:00"
      set_level:
      state: "{{ is_state('sensor.carcharger_status', 'charging') }}"
      level: >-
        {{
          ((states('sensor.carcharger_power') | float(0)) / 11.5 * 255) | round(0)
        }}

The issue is there is a significant delay between when the action is sent to the charger and when the charger updates its state to reflect ON/OFF/Level states. As you can see I have added some delay in the action but it doesn’t seems to help. So ideally what I’d like is:

  • When the light is turned on (from off), it keeps the state on with whatever level (doesn’t matter) for x minutes, before it updates the state/level from the charger
  • When the light is turned off (from on), it keeps the state off for x minutes, before it updates the state/level from the charger

Is it doable with a template? I think it’s a general issue with delay between action and state update, how does Home Assistant handle such delay?
Many thanks in advance!

Try this:

      turn_on:
        - action: input_boolean.turn_on
          target:
            entity_id: input_boolean.charge_car
        - delay: "00:02:00"
        - action: homeassisant.update_entity
          entity_id: sensor.carcharger_status
      turn_off:
        - action: input_boolean.turn_off
          target:
            entity_id: input_boolean.charge_car
        - delay: "00:02:00"
        - action: homeassisant.update_entity
          entity_id: sensor.carcharger_status
1 Like

Hi @tom_l :
Thank you for your reply! I tried the action but received the below error:
Action light.turn_on uses action homeassisant.update_entity which was not found.

For completeness, below is the template:

- light:
    - name: "Car Charger"
      unique_id: "car_charger"
      # Light is 'on' when the charger is in 'charging' state
      state: "{{ is_state('sensor.carcharger_status', 'charging') }}"
      level: "{{ ((states('sensor.carcharger_power') | float(0)) / 11.5 * 255) | round(0) }}"
      turn_on:
        sequence:
          - repeat:
              while:
                - condition: state
                  entity_id: sensor.carcharger_status
                  state: "awaiting_start"
                # Don't do it too many times
                - condition: template
                  value_template: "{{ repeat.index <= 2 }}"
              sequence:
                - action: easee.action_command
                  data:
                    device_id: xxxxxxxx
                    action_command: override_schedule
                - delay: "00:02:00"
          - action: homeassisant.update_entity
            entity_id: sensor.carcharger_status
      turn_off:
        sequence:
          - repeat:
              while:
                - condition: state
                  entity_id: sensor.carcharger_status
                  state:
                    - charging
                    - completed
                # Don't do it too many times
                - condition: template
                  value_template: "{{ repeat.index <= 2 }}"
              sequence:
                - action: easee.action_command
                  data:
                    device_id: xxxxxxxx
                    action_command: pause
                - delay: "00:02:00"
          - action: homeassisant.update_entity
            entity_id: sensor.carcharger_status
      set_level:

I have implement retry as my command to the charger is not always working (it’s a cloud solution hence the delay). Any suggestion where to look?

Also a general question: I assume it’s common to template something with state update based on polling, how to handle such delay between command/action and state?

Spelling mistake

homeassisant.update_entity

Should be:

homeassistant.update_entity

Sorry I should have checked the spelling…
However it doesn’t work looks like. So the current state is OFF because the charge is waiting to start. If I turn on the light, it sends command, but the light stays in OFF mode (maybe because it uses the existing value from sensor.carcharger_status).

Removing the state template state: “{{ is_state(‘sensor.carcharger_status’, ‘charging’) }}”, ignores the change from sensor.carcharger_status, which is also not what I want, for example when start charging failed the light should be off after some minutes - however there is another tricky case here, when the light is updated to OFF it shouldn’t trigger another command to pause charging…

Well you are doing a very hacky thing, so expect hacky results?

Personally I’d just ditch HomeKit altogether and make a nice HA dashboard.

1 Like

Yeah I realised that - it’s a complex solution to do a simple thing :slight_smile:
Anyways, as a beginner of HA I’m still puzzled how it works with template to interact with slow devices (maybe a restful API or anything internet based).
You can turn on/off the switch, however the real state of the thing is changed only after some time later (there is also a risk of failure). So how to make the HA switch in sync with the real thing and handles the delay between the action and state in practice?

In case anybody is interested, the solution I find is to ignore the state in template (assuming on/off always work), then schedule a delayed sync with automation. Be aware the action loop:

  1. Turn on
  2. Wait for x seconds, remote state is still off
  3. Automation turn off the light
  4. Here you don’t want to send off command (as it’s already synced) because your command might still be processed

One thing lack in HA is fine control of action/state in very rare case like mine :wink:
It’s for simplification and fully understandable.