Passing templated lists to input_select.set_options?

I’m trying to configure an input_select component that allows me to set the default audio to play during my wakeup routine. This select is to be dynamically populated from the source list available on SONOS devices, and ultimately should be synced between the two.

I created a script to periodically update the list from SONOS’ source_list but cannot get it to actually match up the two. Instead of being passed as a list, I’m getting the markup as the first, and only option — any ideas? Comma separation doesn’t appear to work here.

script:
    update_sonos_faves:
        sequence:
          - service: input_select.set_options
            data_template:
                entity_id: input_select.wakeup_audio
                options: >
                    {%- set stte -%}
                        {%- for stte in states.media_player if "sonos" in stte.object_id -%}
                            {{ stte.entity_id if loop.first }}
                        {%- endfor -%}
                    {%- endset -%}
                    {%- set stte = states|selectattr("entity_id", "equalto", stte)|list|first -%}

                    {{ stte.attributes.source_list }}

Comma seperation between square brackets should work.

Not having any luck on my end, either adding |tojson or using:

[{{ stte.attributes.source_list|join(",") }}]

Do you have an example you could provide?

Thanks.

Think you’ll need to include single quotes around your items

Nope. Still just getting the string literal with:

{%- set stte -%}
    {%- for stte in states.media_player
        if "sonos" in stte.object_id -%}
        {{ stte.entity_id if loop.first }}
    {%- endfor -%}
{%- endset -%}
{%- set stte = states|selectattr("entity_id", "equalto", stte)|list|first -%}

{%- set join = joiner() -%}
[{%- for item in stte.attributes.source_list -%}
    {{ join() ~ "'" ~ item ~ "'" }}
{%- endfor -%}]

Agreed. Just been playing with it myself. Was sure I’d got this working previously but don’t have a working example at the moment.

I’ve been beating my head trying to get this to work too. Can’t seem to find a way to do this. Anyone have any luck with it recently?

I didn’t get anywhere with it; just abandoned the idea in the end. It wasn’t mission critical…

Templates itself cannot return lists. If you want to return a list, construct the data_template to contain lists and they will be preserved. Example of using data_template to create a script to set color based on input value:

script:
  dynamic_light:
    sequence:
      service: light.turn_on
      entity_id: light.kitchen_lights
      data_template:
        rgb_color:
          - "{% if color == 'red' %}255{% else %}0{% endif %}"
          - "{% if color == 'green' %}255{% else %}0{% endif %}"
          - "{% if color == 'blue' %}255{% else %}0{% endif %}"