Building list of values in template for garbage collection announcement

Hi - I’m using a garbage plugin to work out which bins need putting out. It’s working nicely. The next step is creating a script to alert me (when asked) as to what bins to put out. I want to build an array (or a list) of the bins to go out, so I can then announce that list. Rather than saying “the following bins need to go out. black, green” I want it more natrual sounding, so “this week, the black and green bins need to go out”.

This is what I have so far

script:
  alexa_what_bin_this_week:
    sequence:
    - service: script.telegram_notification
      data: 
        message: >
            {% set black_bin_days = state_attr('sensor.black_bin', 'days') %}
            {% set blue_bin_days = state_attr('sensor.blue_bin', 'days') %}
            {% set brown_bin_days = state_attr('sensor.brown_bin', 'days') %}
            {% set green_bin_days = state_attr('sensor.green_bin', 'days') %}
            This week, the following bins need to go out
            {% if (black_bin_days < 7) %} 
              black,
            {% endif %}
            {% if (brown_bin_days < 7) %}
              brown,
            {% endif %}
            {% if (blue_bin_days < 7) %}
              blue,
            {% endif %}
            {% if (green_bin_days < 7) %}
              green,
            {% endif %}

Ideally, it would be something like this (using random pseudo code from my head).

            {% set black_bin_days = state_attr('sensor.black_bin', 'days') %}
            {% set blue_bin_days = state_attr('sensor.blue_bin', 'days') %}
            {% set brown_bin_days = state_attr('sensor.brown_bin', 'days') %}
            {% set green_bin_days = state_attr('sensor.green_bin', 'days') %}
            
            {% set bins = [] %}

            {% if (black_bin_days < 7) %} 
              {% bins += "black" %}         #bins["black"]
            {% endif %}
            {% if (brown_bin_days < 7) %}
              {% bins += "brown" %}         #bins["black", "brown"]
            {% endif %}
            {% if (blue_bin_days < 7) %}
              {% bins += "blue" %}         #bins["black", "brown", "blue"]
            {% endif %}
            {% if (green_bin_days < 7) %}
              {% bins += "green" %}         #bins["black", "brown", "blue", "green"]
            {% endif %}

            #turn the array to a list and add commas for speech
            This week, the {% implode(bins) %} need to go out

Basically, I’ve been trying to find out how to create a list within the YAML template. Is it possible?? Thanks!

OK, I’ve managed to get somewhere… but it’s probably not elegant in the slightest!!

alexa_what_bin_this_week:
    sequence:
    - service: script.announce_what_bin_this_week
      data: 
        message: >
              {% set black_bin_days = state_attr('sensor.black_bin', 'days') %}
              {% set blue_bin_days = state_attr('sensor.blue_bin', 'days') %}
              {% set brown_bin_days = state_attr('sensor.brown_bin', 'days') %}
              {% set green_bin_days = state_attr('sensor.green_bin', 'days') %}

              {% set bin_string = "" %}
              {% if (green_bin_days < 7) %} 
                {% set bin_string = bin_string + "green," %}
              {% endif %}  
              {% if (black_bin_days < 7) %} 
                {% set bin_string = bin_string + "black," %}
              {% endif %}       
              {% if (blue_bin_days < 7) %} 
                {% set bin_string = bin_string + "blue," %}
              {% endif %}  
              {% if (brown_bin_days < 7) %} 
                {% set bin_string = bin_string + "brown," %}
              {% endif %}  

              {% set bin_string = bin_string[:-1] %}
              {% set bin_list = bin_string.split(",") %}
              {% set bins_str = namespace(value="") %}

              {% for bin in bin_list -%}
              {% if not loop.last %}
                {% set bins_str.value = bins_str.value + bin + " " %}
                {% else %}
                {% set bins_str.value = bins_str.value + "and " + bin %}
                {% endif %}
              {%- endfor %}

              {% if now().weekday() == 2 %}
                {% set output_str = "Tomorrow, the" %}
              {% elif now().weekday() == 3 %}
                {% set output_str = "Today, the" %}
              {% else %}
                {% set output_str = "This week, the" %}
              {% endif %}

              {{ output_str }} {{ bins_str.value }} bins need to go out

That will then call the other script which simply outputs the message to other devices and the TV etc.
The script is triggered by asking Alexa “what bins need to go out”. It will work out if it’s “today” or “tomorrow” or just this week. Hopefully anyway!!