Help with counting in templates

Hi,

I’m trying to count how many ACs are on through a template.
Here is what I’ve got so far (quite new to this).
{% for state in states.climate %}
{% if is_state(‘climate???’, ‘off’) -%}
– here should come the counting–
{%- else -%}
{{state}}
{%- endif %}
{% endfor %}

Every device starts with climate.[name] and the I’m looking to count those that are not ‘off’.

Cany you please help with to modify it to work?
Thanks.

Here is the output I’m getting:
<template TemplateState(<state climate.noya=off; hvac_modes=[<HVACMode.COOL: ‘cool’>, <HVACMode.HEAT: ‘heat’>, <HVACMode.FAN_ONLY: ‘fan_only’>, <HVACMode.DRY: ‘dry’>, <HVACMode.HEAT_COOL: ‘heat_cool’>, <HVACMode.OFF: ‘off’>], min_temp=16, max_temp=30, target_temp_step=1, fan_modes=[‘low’, ‘medium’, ‘high’, ‘auto’, ‘strong’], swing_modes=[‘stopped’, ‘fixedtop’, ‘fixedmiddletop’, ‘fixedmiddle’, ‘fixedmiddlebottom’, ‘fixedbottom’, ‘rangetop’, ‘rangemiddle’, ‘rangebottom’, ‘rangefull’], current_temperature=25.1, temperature=23, current_humidity=68.9, fan_mode=high, swing_mode=stopped, friendly_name=2-ac-noya, supported_features=425 @ 2024-03-29T16:53:22.609583+03:00>)>

 <template TemplateState(<state climate.guy=off; hvac_modes=[<HVACMode.COOL: 'cool'>, <HVACMode.HEAT: 'heat'>, <HVACMode.FAN_ONLY: 'fan_only'>, <HVACMode.DRY: 'dry'>, <HVACMode.HEAT_COOL: 'heat_cool'>, <HVACMode.OFF: 'off'>], min_temp=16, max_temp=30, target_temp_step=1, fan_modes=['low', 'medium', 'high', 'auto', 'strong'], swing_modes=['stopped', 'fixedtop', 'fixedmiddletop', 'fixedmiddle', 'fixedmiddlebottom', 'fixedbottom', 'rangetop', 'rangemiddle', 'rangebottom', 'rangefull'], current_temperature=24, temperature=30, current_humidity=68.5, fan_mode=auto, swing_mode=stopped, friendly_name=2-ac-guy, supported_features=425 @ 2024-03-27T15:38:16.064065+02:00>)>

 <template TemplateState(<state climate.raz=off; hvac_modes=[<HVACMode.COOL: 'cool'>, <HVACMode.HEAT: 'heat'>, <HVACMode.FAN_ONLY: 'fan_only'>, <HVACMode.DRY: 'dry'>, <HVACMode.HEAT_COOL: 'heat_cool'>, <HVACMode.OFF: 'off'>], min_temp=16, max_temp=30, target_temp_step=1, fan_modes=['low', 'medium', 'high', 'auto', 'strong'], swing_modes=['stopped', 'fixedtop', 'fixedmiddletop', 'fixedmiddle', 'fixedmiddlebottom', 'fixedbottom', 'rangetop', 'rangemiddle', 'rangebottom', 'rangefull'], current_temperature=22.5, temperature=20, current_humidity=69.6, fan_mode=auto, swing_mode=stopped, friendly_name=2-ac-raz, supported_features=425 @ 2024-03-29T15:18:35.639643+03:00>)>

 <template TemplateState(<state climate.parents=off; hvac_modes=[<HVACMode.COOL: 'cool'>, <HVACMode.HEAT: 'heat'>, <HVACMode.FAN_ONLY: 'fan_only'>, <HVACMode.DRY: 'dry'>, <HVACMode.HEAT_COOL: 'heat_cool'>, <HVACMode.OFF: 'off'>], min_temp=16, max_temp=30, target_temp_step=1, fan_modes=['low', 'medium', 'high', 'auto', 'strong'], swing_modes=['stopped', 'fixedtop', 'fixedmiddletop', 'fixedmiddle', 'fixedmiddlebottom', 'fixedbottom', 'rangetop', 'rangemiddle', 'rangebottom', 'rangefull'], current_temperature=24, temperature=22, current_humidity=70.8, fan_mode=auto, swing_mode=stopped, friendly_name=2-ac-parents, supported_features=425 @ 2024-03-29T09:28:24.321880+03:00>)>

Here is a list of the output I’m getting

When you use a for loop the way you have in your example, what is returned is something that looks kind of like a list, but is really a list-shaped string… the important data structures have been flattened. It is possible to use namespaces to extract information from the loop and maintain its structure, but, whenever possible, you should use the built-in filters rather than using loops because they are both more efficient and less complicated.

{{ states.climate | rejectattr('state', 'eq', 'off') | list | count }}
1 Like