Error randomizing a script

Hi,
I’m trying to use a template in a script to randomize a list of mp3s but seems to doing something wrong with template parsing.

random_music:
  sequence:
  - action: media_player.play_media
    metadata: {}
    target:
      entity_id: media_player.mserver2_mpd
    data:
      media:
        media_content_id: '{{ url }}'
        media_content_type: audio/mpeg
        metadata:
          title: '{{ file }}'
          thumbnail:
          media_class: music
          children_media_class:
          navigateIds:
          - {}
          - media_content_type: app
            media_content_id: media-source://media_source
          - media_content_type: ''
            media_content_id: media-source://media_source/local/a
          browse_entity_id: media_player.mserver2_mpd
  alias: random_music
  description: 'Script: Picks a random song from the list'
  variables:
    files: >-
      [ "a.mp3", "b.mp3", "c.mp3", "d.mp3" ]
    file: '{{ ( files | random ) }}'
    url: 'media-source://media_source/local/a/{{ file }}'

The trace shows some Assertion Error:

Media player 'Play media' on mServer2_mpd
Executed: January 8, 2026 at 9:59:13 PM
Error: AssertionError
Result:
params:
  domain: media_player
  service: play_media
  service_data:
    entity_id:
      - media_player.mserver2_mpd
    media_content_type: audio/mpeg
    media_content_id: media-source://media_source/local/a/h
  target:
    entity_id:
      - media_player.mserver2_mpd
running_script: false

I’m not sure what that means, let me know if there are any simple fixes for this one.
Thanks.

That’s defining files as a string not a list. You want either YAML:

  variables:
    files:
      - "a.mp3"
      - "b.mp3"
      - "c.mp3"
      - "d.mp3"

or Jinja:

  variables:
    files: >-
      {{ [ "a.mp3", "b.mp3", "c.mp3", "d.mp3" ] }}
1 Like

@Troon thanks! There is no way, with my rookie knowledge of coding, I could’ve picked this subtlety in any parallel universe :grinning: