I have limited experience with templates and never used a for loop so might be missing something very obvious here. I have a group created using batter sensors from multiple devices.
I’d like to iterate through the entities in this group and get a count of how many are running battery level set in the helper.
{% set count = 0 %}
{% for battery in state_attr('group.monitored_batteries', 'entity_id') %}
{% if (states(battery) | int) < (states('input_number.battery_levels') | int) %}
{{ states(battery) }}
{% set count = count + 1 %}
{% endif %}
{% endfor %}
{{count}}
In the template editor, it is showing all the right values that are less than the value set in helper (> 70%) but the count still comes as “0”.
You have defined count outside the loop as = 0, in order to extract what happens inside the loop after the loop ends you would use a namespace (see below). However, you can acheived what you are trying to do without using a loop, by using the built-in Jinja filters…
This will give you the count: EDIT: See @123’s post below
{{expand(‘group.monitored_batteries’) | selectattr(‘state’, ‘lt’, states(‘input_number.battery_levels’)) | list | count}}
If you wanted to use the loop you would do the following:
{% set ns = namespace(count = 0) %}
{% for battery in expand('group.monitored_batteries') | map(attribute='state') | list %}
{% if battery | int < states('input_number.battery_levels') | int %}
{{ battery }} {# remove this line if you don't want it to print the level values #}
{% set ns.count = ns.count + 1 %}
{% endif %}
{% endfor %}
{{ ns.count }}
The following screenshot shows the difference between performing a string vs number-based comparison. The threshold is 5 so only 1 of the group’s four values qualifies but the string-based comparison reports 2.
But I was not able to tweak the template code to output the friendly_name for the entity. Is there a way to get friendly_name as part of the template output in such scenarios?
I completely missed the “map(attribute=‘name’)” part so no surprises why it did not work but I did try combinations like:
{{state_attr('battery', 'name')}}
{{battery['name']}} # thinking it might be a JSON object
And few other variants
Looking at your code I understand that “battery” is not an entity but an instance of a temporary object within the context of “for loop”. So normal features of entity like “states” or “state_attr” will not work here. Is this correct?
But why is “{{ battery.name }}” working but not “{{battery[‘name’]}}”?
Thanks, this discussion has been very educational for me!
That’s great, at least I was thinking correctly about the whole concept but must’ve typed something incorrectly into the template editor. Thanks for confirming