Random MP3 file

Found the solution, this is working perfectly:

alias: Karacsony
sequence:
  - service: media_player.play_media
    target:
      entity_id: media_player.google_home_mini
    data:
      media_content_id: >-
        media-source://media_source/media/{{ state_attr('sensor.karacsony',
        'file_list')|random }}
      media_content_type: audio/mp3
mode: single
2 Likes

Thank you for sharing the solution.
By the way, have you tried to make a script to play the list of mp3 files in local media folder in ordering by file name?
For example there are 3 mp3 files in /media folder as below:
1.mp3
2.mp3
3.mp3

The expectation is when start the script, the file 1 will be played first then file 2, then file 3 etc.

Thank you.

Hi,
I know it’s old thread but it’s not working for me .
if i send random file :

media_content_id: >-
  http://10.0.0.11:8123/media/quran/064%20-%20At-Taghabun%20-%20Abdulbasit%20Abdulsamad.mp3

and not working

if i select file manually it’s working and i see extra /local/ on file path

media_content_id: >-
  http://10.0.0.11:8123/media/local/quran/002%20-%20Al-Baqarah%20-%20Abdulbasit%20Abdulsamad.mp3

my command:

service: media_player.play_media
data:
  enqueue: play
  media_content_type: audio/mp3
  media_content_id: "{{ state_attr('sensor.quran', 'file_list')|random }}"
target:
  entity_id: media_player.chrome_cast

Same issue here, did you manage to resolve the “local” issue?

edit: I found a workaround, in your example… just add a folder “local” to “media” and copy “quran” to “local” … this breaks the manual selection of a media file but the randomise works…

1 Like

This works for me

service: media_player.play_media
data:
  media_content_type: music
  media_content_id: media-source://media_source{{ state_attr('sensor.halloween_sounds', 'file_list')|random }}
target:
  entity_id: media_player.garage

1 Like

thank you
i found another workarowend with replace

      media_content_id: >-
        {{ states('sensor.mp3file') |
        replace('/media/quran/','/media/local/quran/') }}

Hi. Would you mind showing me how I can edit this working code to play in order from folder? I am stuck on how to add the repeat.index... part. The random is repeating over and over.

    play_random_track:
      alias: Play Random Music Track
      sequence:
        - service: media_player.play_media
          data:
            media_content_id: "{{ state_attr('sensor.music', 'file_list')|random |replace('media/','media/local/') }}"
            media_content_type: audio/mp4        
          target:
            entity_id: media_player.razer_leviathan_player

Your version is working and the music starts selectively, but for some reason one track of music is played when there are several dozen tracks in the folder. How do I make all tracks play?

If I run like this, then the track playback starts selectively, but plays one track

service: media_player.play_media
data:
  enqueue: play
  media_content_type: playlist
  media_content_id: >-
    media-source://media_source{{ state_attr('sensor.music', 'file_list')|random
    }}
target:
  entity_id: media_player.mpd_respeaker

If I run like this, then all tracks are played, but starts from the very beginning, there is no selective start of the track

service: media_player.play_media
data:
  media_content_type: playlist
  media_content_id: my_music
target:
  entity_id: media_player.mpd_respeaker

To solve my problem, I resorted to automation and implemented it as follows, It works. I added an auxiliary switch “Music Playback” and it turns on simultaneously when starting music once, then as soon as the MPD player stops working and changes the status from playing to off, a trigger is triggered and runs a script that starts a selective track and it will be non-stop until we turn off the auxiliary switch “Music Playback” and the MPD player itself

alias: "ReSpeaker: Play music non-stop"
description: >-
  When we start to play music selectively, only one plays
  the track is selective and will not be played anymore. To play the next track, you need to
  re-run the script to run the sample music. This automation will be
  run the script every time the music finishes playing, provided that
the "ReSpeaker: MPD" switch is on. Play music selectively"
trigger:
  - platform: state
    entity_id:
      - media_player.mpd_respeaker
    to: "off"
    alias: MPD ReSpeaker turned off
condition:
  - condition: template
    value_template: "{{ is_state('input_boolean.respeaker_mpd_music_playback','on') }}"
    alias: Music playback is enabled
action:
  - service: script.respeaker_mpd_play_music_selectively
    data: {}
mode: single

The script itself for launching random music

service: media_player.play_media
data:
  enqueue: play
  media_content_type: music
  media_content_id: >-
    media-source://media_source{{ state_attr('sensor.music',
    'file_list')|random}}
target:
  entity_id: media_player.mpd_respeaker

I had an automation for this that was working up until a HA update a few months ago and has been broken since. Automation is

  - service: media_player.play_media
    target:
      entity_id: media_player.living_room_speaker
    data:
      media_content_id: >-
        media-source://media/{{ state_attr('sensor.playlist',
        'file_list')|random }}
      media_content_type: audio/mp3

In the logs I get the error

Failed to cast media media-source://media_source//media/example.mp3. Please make sure the URL is: Reachable from the cast device and either a publicly resolvable hostname or an IP address

Not sure what the update changed. Just trying to cast a single file without the random selection script added in still works fine.

I had the same issue but have been able to resolve it by updating the existing media_content_id with the following:

media_content_id: "/media/local/{{ state_attr('sensor.alarms', 'file_list')|random }}"

It looks like you host your MP3s directly in the media folder so try this:

media_content_id: "/media/{{ state_attr('sensor.playlist', 'file_list')|random }}"