Template attributes formatting

Hi,

I’ve managed to create a template sensor that adds values to an attribute, but I’m struggling to get the right format. Ideally, I’d create an array here instead, and it would probably make this more user-friendly later.
Is there a way to make this into an array, or comma-separated value if a result = true, or something like that?

          {% set today = now().strftime('%d/%m/%Y') %}
          {% set food = states.sensor.matavfall.state %}
          {% set paper = states.sensor.papir.state %}
          {% set rest = states.sensor.restavfall.state %}
          {% if food in today or paper in today or rest in today %}
            {% if food in today %} food {% endif %}
            {% if paper in today %} paper {% endif %}
            {% if rest in today %} residual {% endif %}
          {% else %}
            nothing
          {% endif %}
{% set today = now().strftime('%d/%m/%Y') %}
{% set dates = expand('sensor.matavfall', 'sensor.papir', 'sensor.restavfall') %}
{% set dates = dates | selectattr('state', 'search', today) | map(attribute='name') | list %}
{{ dates or ['nothing'] }}
1 Like

Nailed it!
I learn something new every day… This is awesome. Thank you!

I was about to give up, but I finally found the last piece to the puzzle.
I didn’t know how to properly access the values, but apparently it was as simple as this:
today | join(’, ')

{%- set today = state_attr('sensor.waste_pickup','today') %}
{%- set waste_today = today | join(', ')%}
{%- set tomorrow = state_attr('sensor.waste_pickup','tomorrow') %}
{%- set waste_tomorrow = tomorrow | join(', ')%}

{% if waste_tomorrow != "nothing" -%} {{'🔜'}} <b>{{waste_tomorrow | capitalize | regex_replace(',([^,]*)$',' and\\1')}} waste gets picked up tomorrow</b>
{%- elif waste_today != "nothing" -%} {{'🚛'}} <b>{{waste_today | capitalize | regex_replace(',([^,]*)$',' and\\1')}} waste gets picked up today</b>
{%- endif %}

Then again, I’m not sure if this is the most elegant solution, but I thought I’d share the result.
Please feel free to add any pointers if there are better ways of doing this. Otherwise, thanks again.