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