How to trigger a binary sensor based template sensor

I am creating a template sensor as below, based on the state of a binary sensor.

I am sure I should not be using time_pattern as the trigger - instead I should be triggering based on the state of the binary sensor (binary_sensor.shelly_plus_1_spa_heater_state)? Does that sound right? And how would I go about setting this trigger?

  - trigger:
      - platform: time_pattern
        seconds: "/30"  
    sensor:
      - name: "Spa Solar Watts"                
        unit_of_measurement: "W"
        device_class: power
        state: >
          {% if (states("sensor.day_night_elec_indicator") == "day") %} 
            {% if states('binary_sensor.shelly_plus_1_spa_heater_state') == "on" %}
            {% set spa_watts = 1500 %}       
            {% else %}
            {% set spa_watts = 0 %}  
            {% endif %}
            {% if spa_watts == 1500 %}        
             {% if (states("sensor.phase_2") | float) <= 0 %}
             {{spa_watts}}
             {% else %}
               {% if (states('sensor.phase_2') | float) >= 1500 %}
               0
               {% else %}
               {{((states('sensor.phase_2')|float - spa_watts | float) | abs)}}
               {% endif %}
             {% endif %}
            {% else %}  
            0      
            {% endif %}
          {% else %}  
          0      
          {% endif %}

If you don’t include any trigger it will automatically update the template whenever any of the entities in the template update. You already have an if statement in there checking the binary sensor is on.

1 Like

Should the template be updated quickly (within seconds or milliseconds?) when any of the entities in the template update?

Milliseconds at most.

1 Like