HVAC Template Sensor

I created a template sensor to show when the cooling stage on my ac is running. The sensor reports “on” but it doesn’t report “off” for some reason. When I put the value template in the developer tools it works. Reports “on” when the sensor.hvac_action is cooling. And it reports “off” when it’s off. But the actual sensor won’t turn off. Any ideas? Here’s the config for the template sensor.

- platform: template
  sensors:
    hvac_cooling:
      value_template: >
        {% if is_state ('sensor.hvac_action', 'cooling') %}
        on
        {% else %}
        off
        {% endif %}
      friendly_name: Hvac Cooling
1 Like

Well, now it works. I changed the is_state to states and got a warning in the log about no entity to track, then I changed it back to is_state and, restarted HA and now it tracks like it’s supposed to. Weird.

What is sensor.hvac_action? Is it yet another Template Sensor?

You may already know this but, upon startup, Home Assistant examines a Template Sensor’s template and attempts to select the entity, or entities, that it should monitor for state-changes. If it is unsuccessful, it will evaluate the template once (upon startup) and then never again (until the next restart). So the template sensor appears to work once (on startup) and never again.

Home Assistant shouldn’t have a problem identifying the entity it must monitor (sensor.hvac_action). If it did, you could simple add this line to explicitly identify the entity to be monitored:

entity_id: sensor.hvac_action

What I’m wondering is if this sensor is possibly yet another Template Sensor and what its template looks like …

sensor.hvac_action is indeed another template sensor. I needed to expose an attribute of my ecobee thermostat to be able to track the “hvac action”. Cooling, Heating, off. So I created a template sensor for it.

Then I created a template sensor to turn off and on when it’s cooling or not so I can graph it over time. Which is the code I posted.

The behavior you describe is exactly what it was doing. I wasn’t aware of the way HA evaluates the template and selects the entity. I guess it would be best practice to explicitly identify the entity.

Instead of using your sensor.hvac_action template sensor for this, you could try this instead:

binary_sensor:
- platform: template
  sensors:
    hvac_cooling:
      value_template: "{{ is_state_attr('climate.thermostat', 'hvac_action', 'cooling') }}"
      friendly_name: HVAC Cooling

Then this doesn’t rely on your other template sensor. Replace climate.thermostat with your thermostat entity name.

Edit: updated with a better solution

6 Likes