Templating jinja2 append question

if try to concatenate some strings in a for loop, what i found out so far is that it is not that easy with jinja2.

Here is my code
{% set output = '' %} {%- for item in items -%} {% output = output + item.name %} {% endfor %} {{ output }}

At the end my output var is still empty. I found out that is because of scope problems in jinja2.

But there is a workaround with
{% do output.appen('my string') %}

But that throws an error in hass because do is not available. I read that the is an extension that needs to be activated and it seems this one is missing.

The name of the extension is jinja2.ext.do. Or is there i way i can not see? My goal is to loop trough all items, pick thesend it to a notify component output all together.

I think you’re missing a “set”

{% set output = output + item.name %}

sorry i just rewrote the code as sample and missed the set but that makes no difference, still not working. Inside the loop my output var is handled as new var so it can not concatenate the strings. :frowning:

Yeah. Looks like the scope is inside of the loop.

I do a series of sets in and out of IF statements in my code and it works fine.

Inside loops? Can you post a sample please?

Inside of IFs, not loops.

I don’t think this will help you, but here’s where I look at my media_players and figure out the name of the song that’s playing on the active one. (would be smarter to loop this, but I didn’t)

  data_template:
    text: > 
      {% if states.media_player.squeezeplay.state == 'playing' %}

        {% if states.media_player.squeezeplay.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.squeezeplay.attributes.media_title %}
          {%- if states.media_player.squeezeplay.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.squeezeplay.attributes.media_artist%}{% endif %}
        {% else %}{% set text = "I don't know." %}
        {% endif %}

      {% elif states.media_player.living_room_chromecast.state == 'playing' %}

        {% if states.media_player.living_room_chromecast.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.living_room_chromecast.attributes.media_title %}
          {%- if states.media_player.living_room_chromecast.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.living_room_chromecast.attributes.media_artist%}{% endif %}
        {% else %}{% set text = "I don't know." %}
        {% endif %}

      {% elif states.media_player.living_room_home.state == 'playing' %}

        {% if states.media_player.living_room_home.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.living_room_home.attributes.media_title %}
          {%- if states.media_player.living_room_home.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.living_room_home.attributes.media_artist%}{% endif %}
        {% else %}{% set text = "I don't know." %}
        {% endif %}

      {% else %}{% set text = "No music is playing." %}
      {% endif %}{{ text | urlencode }}

Use if loop.last

@Danielhiversen thx, that works

Hi skycryer,

where did u use loop.last in your code.
I have the exact same issue, can you please send your code with loop.last ?