Counting Air Conditioner

I have a couple of Air Conditioners on my home, some of them control using Broadlink other their native Home Assistant Integration. I want to count how many are in a “cool” and/or “heat” state. Is there a way to count an identity if one of the states are on?

This it what i have con my .yaml

# AV On
- platform: template
  sensors:
    number_of_ac_on: 
#      unit_of_measurement: 'on'
      value_template: >
        {% set climate = [
          states.climate.clima_invitados,
          states.climate.clima_recamara,
          states.climate.clima_sala,
          states.climate.clima_sofia,
          ] %}
        {{ climate | selectattr('state','eq','cool') | list | count }}

Please use the new template sensor format for new sensors. e.g. configuration.yaml:

 template:
   - sensor:
       - name: Number of ac on
         state: >
           {% set climate = [
             states.climate.clima_invitados,
             states.climate.clima_recamara,
             states.climate.clima_sala,
             states.climate.clima_sofia,
             ] %}
           {{ climate | selectattr('state','eq','cool') | list | count }}

Tested as working.

Hi Tom

Can this template be adjusted for any situation that is rather then off. I have a group of climates and I want to count the number of the ones that are currently on. But the because my climates only have hvac_modes rather then an on switch. So can the above code be converted for showing only working climates ?

Probably be something like this:

{{ climate | rejectattr('state','eq','off') | list | count }}