Template sensor to check if any thermostat is on

I have 3 mqtt climate thermostats. I am trying to create a template sensor that shoud have the value of 0 none of them is on (=heating mode) and return a non-zero number if any one has the heating on. I created the following configuration, after searching a lot on the internet, but it doesn’t work:

template:
  - sensor:
      - name: "Heating_is_on"
        state: >-
          {% set hcount=0 %}
          
          {% for thermo in states.climate %}
            {% if thermo.state == 'heat' %}  
              {% set hcount = hcount + 1 %}
            {% endif %}
          {% endfor %}
          {{ hcount }}

I am very new in HA and any help will be appriciated.

 state: >
    {% set thermo = [
      states.climate.foo
      states.climate.bar
      states.climate.third
      ] %}
    {{ thermo | selectattr('state','eq','heat') | list | count }}
1 Like
        state: "{{ states.climate | selectattr('state', 'eq', 'heat') | list | count }}"

Ninja’d by tom_l

2 Likes

I think your’s updates once a second where as mine updates when any of the climate devices change. See: https://www.home-assistant.io/integrations/template/#rate-limiting-updates

Though yours automaticity takes new climate devices into account.

Swings and roundabouts.

Just added commas in the thermo array in tom_I solution and worked! Thanks

I believe Rate Limiting is designed to limit updates to a maximum of once per second. In other words, it doesn’t default to updating once per second. Or, at least, that’s my understanding of how Rate Limiting works.


FWIW, another way to do the same thing:

state: >
  {{ ['climate.first', 'climate.second', 'climate.third']
    | map('states') | select('eq', 'heat') | list | count }}
2 Likes