I’m having a problem getting the length of a loop to show one time . I’m guessing it’s a syntax issue. Here’s my code:
{% for l in config.entities %}
{% if ("state.l.entity", 'on') %}
Lights on: {{ loop.length }}
{% endif %}
{% endfor %}
{% for l in config.entities if ("state.l.entity", 'on') %}
{{ l.name }}
{%- endfor %}
Why is the length repeating? Sorry, first time Python, and new to Home Assistant. I read the Jinja and Markdown Card docs and still can’t seem to figure it out. Thanks to anyone who can point me in the right direction.
Harry13
(George Georgas)
January 21, 2020, 8:55am
2
Your lights on needs to be outside
venture
January 21, 2020, 11:27am
3
Like this?
{% for l in config.entities %}
{% if ("state.l.entity", 'on') %}
{% endif %}
Lights on: {{ loop.length }}
{% endfor %}
Tried it, but it returns the same. Also tried outside the for loop, but that breaks it.
Thanks for your help. I really appreciate it. Unfortunately still not able to get it.
{% set count = 0 %}
{% for l in config.entities %}
{% if ("state.l.entity", 'on') %}
{% set count = count + 1 %}
{% endif %}
{% endfor %}
This results in 0
printed once, which makes sense because count
is outside of the scope.
{% for l in config.entities %}
{% set count = 0 %}
{% if ("state.l.entity", 'on') %}
{% set count = count + 1 %}
{% endif %}
{{ count }}
{% endfor %}
This results in 1
printed as many times as looped through.
No need for a loop to count lights that are on.
Take a look in this topic for multiple examples.
Seeing that group.all_* was removed from states, here are some useful canned items that will get you the same result as before.
Using Services to perform an action on all entities
Using all inside the entity_id field will allow you to use turn everything on or off inside a domain. You just need to pair it with the correct command. This should work for pretty much every service that has entity_id.
Replaces
# Do not use, this is out of date.
- service: homeassistant.turn_on
entity_id: grou…
Lights that are on:
{{ states['light'] | selectattr('state','eq', 'on') | list | count }}
I figured there would be a better way. Saving that link, it’s super helpful. Thank you!