How to play a random audio file in automation

I am trying to create an automation which plays an audio file when I open a door sensor.

I created a media folder in HASS with random AI voice files of celebrities saying the phrase I’d like them to when this automation is triggered.

I don’t want them to constantly repeat the same one voice and file, how would I create a randomizer when it comes to playing that audio file?

1 Like

Something like this should work, simply list the files and add the random element to the end:


  - service: media_player.play_media
    data:
      media_content_type: music
      media_content_id: |
        {{ ['/media/local/morning/morning1.mp3', 
            '/media/local/morning/morning2.mp3',
            '/media/local/morning/morning3.mp3',
            '/media/local/morning/morning4.mp3', 
            '/media/local/morning/morning5.mp3', 
            '/media/local/morning/morning6.mp3', 
            '/media/local/morning/morning7.mp3', 
            '/media/local/morning/morning8.mp3', 
            '/media/local/morning/morning9.mp3', 
            '/media/local/morning/morning10.mp3', 
            '/media/local/morning/morning11.mp3', 
            '/media/local/morning/morning12.mp3'] | random}} 
    target:
      entity_id: media_player.kitchen_hub

I keep getting UpPnp errors

not work bro in 27 june 2024

That will work. Post what’s not working for you.

That is still correct as of 2024.6