Template sensor: Can I listen to some events and not others?

I have a template sensor (code below) that is trying to calculate the appropriate target amperage for my Level 2 charger to send to my car during solar overproduction.

I’m testing it and it seems to be working well, except my whole home power consumption data (via sunpower) doesn’t update immediately upon any changes.

This means I can’t reliably automate this because each time the charger amperage updates, the calculation will just recalculate against the same power consumption so the target will spiral either upwards or downwards until the power consumption data updates again.

In the Homeassistant dashboard, the template says it will listen to events for:

  • sensor.power_2 (whole home consumption)
  • switch.charger_charge_control (charger on/off)
  • number.charger_maximum_current (charger max amperage)

Is it possible to only have the template listen to sensor.power_2 and switch.charger_charge_control ?

The other option, I suppose, would be to only run an automation against this template sensor once every 5 minutes or so, giving the values time to stabilize and recalculate.


    {% if states('sensor.power_2')|float %}
      {% if is_state('switch.charger_charge_control', 'on') %}
        {{ ((
          ((states('sensor.power_2')|float*-1000)) 
          + (240*(states('number.charger_maximum_current')|int))
          ) / 240) | round(0, 'floor')
        }}
      {% else %}
        {{ (((states('sensor.power_2')|float*-1000)) / 240) | round(0, 'floor') }}
      {% endif %}
    {% endif %}

Yes, by converting to a trigger-based template sensor, using the states of the desired entities as your triggers.

Thank you! I did this and it works a treat. New configuration is done in YAML; not sure if it can be done via the “Helpers” panel, if so I didn’t see a good way to do so. Anyway the new configuration if anyone else finds it is something like this:

configuration.yaml:
templates: !include templates.yaml

templates.yaml:

- trigger:
  - platform: state
    entity_id:
      - sensor.power_2
      - switch.charger_charge_control
  sensor:
    - name: 'Target Charge Amperage'
      unique_id: 'sensor.target_charge_amperage'
      unit_of_measurement: 'A' 
      state: '                          
      {% if states("sensor.power_2")|float %}
        {% if is_state("switch.charger_charge_control", "on") %}
          {{ ((                
          ((states("sensor.power_2")|float*-1000)) 
          + (240*(states("number.charger_maximum_current")|int))
          ) / 240) | round(0, "floor")
          }}
        {% else %}
          {{ (((states("sensor.power_2")|float*-1000)) / 240) | round(0, "floor") }}
        {% endif %}
      {% endif %}'