How to build an entity_name from variables

I have the following code:

   {% 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”)}}

{{state_attr(“scene.wohnen_” + scene, “entity_id”)}}

cool! Vielen Dank!

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.

For example, this produces cat5

{{ 'cat' ~ 5 }}

This produces an error.

{{ 'cat' + 5 }}
2 Likes

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}})

You’re not obligated to use 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 }}
{{ entities | map('states') | select('eq', 'on')
  | list | count }}

There are certain applications that require the use of a for-loop but, more often than not, the desired result can be achieved without it.

If you would optimize my complete HA code, then I think the whole thing is not bigger that a DIN A4 page! Respect!

1 Like

I’m willing to help you streamline another example … but not all of your code. :wink:

Also “optimize” in what respect?

Readable code is often worth orders of magnitude more than code that performs 0.1% better or that takes up slightly less space.

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.

The pattern that we suggest should be one that is intuitive—not merely one that happens to be “employed across the forum.”

1 Like

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.

Good luck.

1 Like

Hi all,

there is a question left:

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") %}

What I normally suggest is based on leveraging Jinja2’s strengths. So do many other volunteers and that’s why it’s found throughout the forum.

1 Like

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.

I’m unfamiliar with Mushroom Cards (and most other custom cards) so, unfortunately, I can’t help you with that issue.