Concatenation of Strings insde {{}} (e.g. for Variable Evaluation in Loops)

I have a loop with an until condition in which I want to evaluate values of an array like so (obviously doesn’t work):

variables:
  room:
    - room1
    - room2
sequence:
- repeat:
    for_each: "{{ room }}"
    sequence:
        - service: notify.notify
          data:
            message: "{{ repeat.item }}: {{ climate.room_climate_{{ repeat.item }}.state }}; "

In the last line, you can see that I’m trying to use a variable inside the {{ }} brackets, and that’s my question: How would this be done? It’s basically a concatenation of strings that itself should be interpreted as an object name.

message: "{{ repeat.item }}: {{ states('climate.room_climate_' ~ repeat.item) }}"
1 Like

Thanks, Troon!

I did read the Script Syntax but didn’t find a solution for my use case anywhere. You presented the solution by using “~”. Where would I find this documented?

Also, it seems like a concatenation is not possible without using a function()?

Here: Template Designer Documentation — Jinja Documentation (3.2.x)

You could also use + if both parts are already strings, but the important point is that we’re using states('your_entity') and building up the string reference in that, as opposed to states.your_entity.state (or the invalid your_entity.state you were trying to use). This is almost always better: see the warning box from here.

The reason I linked the Script Syntax page is that you must use {{ repeat.item }} to reference the current room, as {{ room }} is the list of rooms as you defined it (and I see you’ve edited the original post now :slight_smile: ).

Not sure what you mean? You can only concatenate strings — you can’t build direct references to objects if that’s what you mean.

1 Like

You can only concatenate strings — you can’t build direct references to objects if that’s what you mean.

That’s what I mean! Thanks!

you must use {{ repeat.item }} to reference the current room

Yeah - I corrected my OP because even I realized my mistake there :slight_smile:

Other options:

{{- states['climate.room_climate.'~repeat.item].state }}
variables:
  room:
    - room1
    - room2
sequence:
- repeat:
    for_each: "{{ room }}"
    sequence:
        - variables:
            climate: "{{ 'climate.room_climate_'~repeat.item }}"
        - service: notify.notify
          data:
            message: "{{ repeat.item }}: {{ states(climate) }}; "
1 Like

You can also define the variable like this:

        - variables:
            climate: "climate.room_climate_{{ repeat.item }}"
1 Like

This is basically what I already did and it seems it’s the easiest wasy to deal with nested variable names and their combinations.

Thumbs up for your help!

Not exactly.

You did this which is invalid because you cannot nest templates.

{{ climate.room_climate_{{ repeat.item }}.state }}
                        ^^^^^^^^^^^^^^^^^
                 This template is nested inside
             another template and that's not permitted

Didgeridrew’s suggested solution is to define a separate script variable (named climate) and then use the script variable in the template.

The only subtle difference between how he defined the script variable and my suggestion is that he used string concatenation (the tilde operator) within a template whereas I used just a template. Two ways to produce the same result.

No, I really did, but here: Sensor Reaction Robustness by Means of While Loop - #15 by 123

:wink:

  1. When you said “I really did”, it’s normal to assume you “really did” in this topic and not somewhere else in the community forum.

  2. In the other topic, you originally did this which is invalid.

        entity_id: climate.room_climate_&"{{ wohnung[repeat.index - 1] }}" 
  1. This topic is very similar to the other topic. Originally, you had also duplicated the other topic and were instructed by a moderator to stop duplicating topics.

  2. If a topic is solved by someone’s reply, mark their reply with the Solution tag. This places a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps other users find answers to similar questions. For more information about the Solution tag, refer to guideline 21 in the FAQ.