{% set total = namespace(value=0) %}
{% set scene = states ("input_select.active_scene_wohnen") %}
{% for entity in state_attr("scene.wohnen_ambiente", "entity_id") %}
{% set total.value = total.value + 1 %}
{% endfor %}
{{total.value}}
{{scene}}
{{state_attr("scene.wohnen_ambiente", "entity_id")}}
the scene name is build by “scene.wohnen_xxx” and xxx is given by the scene variable. How can I build the correct entity name for my {{state_attr(“scene.wohnen_xxx”, “entity_id”)}}
Another way to do the same thing as your template but without using a for-loop.
{% set scene = states("input_select.active_scene_wohnen") %}
{% set entity = "scene.wohnen_" ~ scene %}
{% set entities = state_attr(entity, "entity_id") %}
{{ scene }}
{{ entity }}
{{ entities }}
{{ entities | count }}
NOTE
When combining strings, it’s preferable to use the tilde ~ operator instead of the plus + operator. Unlike + which can be used for adding numbers or concatenating strings, ~ is unambiguously for concatenation.
ok perfect, thanks for your code. But I also wanted to count the enties with state=‘on’ And then I need to use the loop.
{% set total = namespace(value=0) %}
{% set on = namespace(value=0) %}
{% set scene = states ("input_select.active_scene_wohnen") %}
{% for entity in state_attr("scene.wohnen_" ~ scene, "entity_id") %}
{% set total.value = total.value + 1 %}
{% if states(entity)=="on" %}
{% set on.value = on.value + 1 %}
{% endif %}
{% endfor %}
({{on.value}}/{{total.value}})
The built-in functions are more efficient in terms of performance, which can be important if you have a lot of templates.
True, so that is up to you to decide, but there’s also a balance: The pattern you see Taras using is quite often employed across the forum, so it will only help you to get used to it. As with many things, there’s a balance to be kept between learning and simplicity.
It is intuitive for people that have worked with declarative languages. These functional patterns are very common across many programming languages nowadays (and actually has been for quite a long time). There’s nothing esoteric about it. Some users may just not be familiar with it.
As I said, you have the option either way and can decide. My point was merely that you will see this quite often – right or wrong, preferred or not. I’m saying it to help, but if you want to go against the stream, I cannot stop you.
is there a way to set the variables globally within a card (e.g. custom:mushroom-template-card)?
I am using the vars for different settings within the card and in order to make it easier I would like to define them only once.
{% set scene = states("input_select.active_scene_wohnen") %}
{% set entity = "scene.wohnen_" ~ scene %}
{% set entities = state_attr(entity, "entity_id") %}
Ok, thanks for the hint. Maybe I didn’t search properly on this topic. What I have found so far indicates that you cannot define variables globally for a card, but only globally for Homeassistant. This doesn’t help in so far as I want to use the same mechanism for different cards in HA and then I can’t use variables twice.
If I’ve overlooked something, it would be great if you share a link from the forum that deals with the topic.