Trying to randomly play mp3 files

I am trying to play one mp3 file at random as part of an automation of unlocking the front door. This is what I have as my action:

service: media_player.play_media
    data:
      media_content_type: music
      media_content_id: |
      {{ [
      "media-source://media_source/local/im-afraid-we-need-to-use.mp3",
      "media-source://media_source/local/bad-to-the-bone.mp3",
      "media-source://media_source/local/bill-nye-theme.mp3",
      "media-source://media_source/local/maths.mp3",
      "media-source://media_source/local/what the dog doing.mp3",
      "media-source://media_source/local/rick-flair-wooo.mp3",
      "media-source://media_source/local/silence_64kb.mp3"  ] | random }} 
      target:
      entity_id: media_player.living_room_nest_audio
    metadata:
      title: Random.mp3
      thumbnail: null
      media_class: music
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://media_source

And I keep getting an error at the end with the random piece. Specifically ] | random }}

Error in parsing YAML: missed comma between flow collection entries (line: 38, column: 62)

Not seeing what im missing

Hello franksantoro,

Notice how target: is still yellow in your posted code?

That tells me you have a formatting error. The pipe symbol is collecting that key.
Play with the formatting there.
Perhaps loose the pipe and add a single quote there and to the end of the template might work.
I’m not the greatest template guru, but I saw that and thought it might be the problem.

As SG pointed out, there’s some indentation issues. The template should be indented more and target less. I wouldn’t bother with the metadata unless your media player requires it. You can also remove a lot of the repetitive file path info by placing it outside the expression delimiters:

service: media_player.play_media
data:
  media_content_type: music
  media_content_id: |
    media-source://media_source/local/{{- [
    "im-afraid-we-need-to-use",
    "bad-to-the-bone",
    "bill-nye-theme",
    "maths",
    "what the dog doing",
    "rick-flair-wooo",
    "silence_64kb"  ] | random -}}.mp3
target:
  entity_id: media_player.living_room_nest_audio
metadata: {}

Thank you. I was able to save the yaml successfully. Now doing a trace because im not getting any audio. Its error’ing out with unknown source directory. Still troubleshooting

Executed: July 25, 2025 at 11:52:31 AM
Error: Unknown source directory.
Result:
params:
  domain: media_player
  service: play_media
  service_data:
    media_content_type: music
    media_content_id: media-source://media_source/local/maths.mp3
    entity_id:
      - media_player.living_room_nest_audio
  target:
    entity_id:
      - media_player.living_room_nest_audio
running_script: false

Playing around some more, this ended up working for me. I also put copies of the files inside of configs/www

data:
  media_content_type: music
  media_content_id: |
    https://IP.ui.nabu.casa/local/ {{- [
    "im-afraid-we-need-to-use",
    "bad-to-the-bone",
    "bill-nye-theme",
    "rick-flair-wooo",
    "maths",
    "wassupbaby",
    "welcome_Jurassic_Park",
    "dmb-scat-4",
    "BashBrother"
    "silence_64kb"  ] | random -}}.mp3
target:
  entity_id: media_player.living_room_nest_audio
metadata: {}
action: media_player.play_media