How to play random mix from a specific folder using Home Assistant

Multiroom audio - PiCorePlayer- Squeezebox LMS
How to play random mix from a specific folder using Home Assistant

I’ve been playing around with Volumio and snapcast for some time but the Volumio paywall and stability issues put me off. When I installed PiCorePlayer with the Squeezebox Logitech Media Server (LMS) on a Raspberry Pi 3b+ with HiFiBerry MiniAmp I was very happy. It is very fast, stable, flexible, has a nice Home Assistant integration and the multiroom sync function works flawlessly. So all my wishes came true :star_struck: ….well there is one issue. Random mix.
It took me a long time to figure this out because many posts address this problem but none had a full solution, only parts. So I decided to create a long read and save others the time I invested in this.

Understanding the problem
I have a NAS with a shared folder that contains mp3 music files. My LMS has access to this folder (NFS) and is able to access about 6000 mp3 files which are stored in 250 folders.
One of those 250 folders is called “Rock 2000” and I want to be able to play a random mix of mp3 files from within that specific “Rock 2000” folder (with 2000 mp3’s). Furthermore I also want to play a random mix of mp3 files from all 250 folders (6000 mp3’s) and a clear playlist option. And all of this has to be controlled from Home Assistant with these buttons.

In the LMS I have a media folder which is mounted to “/mnt/music” and contains all 6000 mp3 files. When I select “Random Mix” it plays a random mix of all 6000 files and that is not what I want.
I could mount “/mnt/music/Rock 2000” as the media folder in LMS and that indeed plays a random mix of that specific folder, but than all other 249 folders are inaccessible for the LMS. Now What?
Next I created a “Rock 2000” playlist and dumped all 2000 mp3’s in it, but that causes enormous performance issues and was unacceptably slow.
After reading lots of posts it turned out that the “Random Mix” feature of the LMS was really the only way to go and indeed it is superfast. But, it’s only able to create a random mix based on “genres”. See LMS documentation http://localhost:9000/html/docs/cli-api.html (replace “localhost” with your LMS server’s address). Logitech calls this the “new-school” method, but I find this stupid because not a lot of people use ‘genre’ as a way of creating a random playlist. I want to create a mixed playlist based on folders so I can manually add and delete mp3’s in that folder and I know exactly what mp3’s can be selected in the random mix. This is easy, fast and flexible, so the old-school method is preferred on all these points, but LMS doesn’t work with that method because of the perfomance issues :sleepy:.
The genre method of the LMS forces me to set the genre of every individual mp3 to a specific genre (e.g. Soft Rock, Rock, Hard Rock, Metal, etc.). That is a whole lot of work! And doesn’t solve the problem that I want to play my favourite Top 2000 rock songs, regardless of the specific rock genre. So how to get that done anyway?

My Solution
The LMS has an “Unknown” genre and I use that to create the “Rock 2000” random mix which I am able to control from Home Assistant. First I had to make sure that no excising mp3’s already had an “Unknown” genre and then add the “Unknown” genre to the all the mp3’s in the Rock 2000 folder. I use the free MP3tag software for that.

  • In the LMS Home screen – select My music- Genre
  • Enable only the “Unknown genre”. This will display all mp3’s with the Unknown genre set and have to be changed.
  • Download and install MP3tag software.
  • Select all mp3 files which have the “Unknown” genre and open them in MP3tag.
  • Select all mp3 files in MP3tag and in the left pane you can delete the genre or select any genre you like as long as it is not “Unknown”. Save and close the MP3tag software.
  • Now check the LMS Home screen – select My music- Genre -“Unknown genre”, make sure there are none left before continuing.
  • In the MP3tag software load all the 2000 mp3’s in the Rock 2000 folder.
  • Select all mp3’s and in the left pane type in the genre “Unknown”, than save and close. All mp3’s in the Rock 2000 folder now have the “Unknown” genre.
  • Now you have to rescan the Media Library.
  • In the LMS Home screen – select settings (right corner)
  • In the Rescan Media Library option select “clear library and rescan everything” and click Rescan. Wait a few minutes, check the Information tab to see if it’s finished.
  • In the LMS Home screen – select “Random Mix” and enable only the “Unknown” option, then click save at the bottom of the page.
  • On the top of the page, next to “Random Songs” click the play button.
  • A random mix of songs from the Rock 2000 folder will start playing.

Now we only need to control this from Home Assistant.

Home Assistant setup

First of all, the configuration is often Capital Sensitive, so make sure you get it exactly right otherwise it won’t work. First we create a script with 3 actions to clear all genres from the random mix, than we select only the “Unknown” genre and last we start playing media player.

  • In the LMS Home screen – clear the current playlist.
  • Login to Home Assistant and select settings – automations and scenes
  • Select the tab “Scripts” and Create new script
  • Name : piCorePlayer - randomplay genre Unknown
  • Icon : mdi:shuffle-variant
  • Mode : single
  • Add Action : Call Service
  • Service : Squeezebox (Logitech Media Server): Call method
  • Targets : [Choose Entity and select your Mediaplayer]
  • Command : randomplaygenreselectall
  • Parameters 1: - 0 (a minus sign, then a space and then a zero)
  • Add Action : Call Service
  • Service : Squeezebox (Logitech Media Server): Call method
  • Targets : [Choose Entity and select your Mediaplayer]
  • Command : randomplaychoosegenre
  • Parameters 1: - Unknown (a minus sign, then a space and then Unknown)
  • Parameters 2: - 1 (a minus sign, then a space and then 1)
  • Add Action : Call Service
  • Service : Squeezebox (Logitech Media Server): Call method
  • Targets : [Choose Entity and select your Mediaplayer]
  • Command : randomplay
  • Parameters 1: - tracks (a minus sign, then a space and then tracks)
  • Click save script
alias: piCorePlayer - randomplay genre Unknown
sequence:
  - service: squeezebox.call_method
    metadata: {}
    data:
      command: randomplaygenreselectall
      parameters:
        - 0
    target:
      entity_id: media_player.woonkamer
  - service: squeezebox.call_method
    metadata: {}
    data:
      command: randomplaychoosegenre
      parameters:
        - Unknown
        - 1
    target:
      entity_id: media_player.woonkamer
  - service: squeezebox.call_method
    metadata: {}
    data:
      command: randomplay
      parameters:
        - tracks
    target:
      entity_id: media_player.woonkamer
    enabled: true
mode: single
icon: mdi:shuffle-variant

Now we create a second script with 2 actions to select all genres and start playing the media player.

  • Login to Home Assistant and select settings – automations and scenes
  • Select the tab “Scripts” and Create new script
  • Name : piCorePlayer - randomplay genres All
  • Icon : mdi:shuffle-variant
  • Mode : single
  • Add Action : Call Service
  • Service : Squeezebox (Logitech Media Server): Call method
  • Targets : [Choose Entity and select your Mediaplayer]
  • Command : randomplaygenreselectall
  • Parameters 1: - 1 (a minus sign, then a space and then a one)
  • Add Action : Call Service
  • Service : Squeezebox (Logitech Media Server): Call method
  • Targets : [Choose Entity and select your Mediaplayer]
  • Command : randomplay
  • Parameters 1: - tracks (a minus sign, then a space and then tracks)
  • Click save script
alias: piCorePlayer - randomplay genres All
sequence:
  - service: squeezebox.call_method
    metadata: {}
    data:
      command: randomplaygenreselectall
      parameters:
        - 1
    target:
      entity_id: media_player.woonkamer
  - service: squeezebox.call_method
    metadata: {}
    data:
      command: randomplay
      parameters:
        - tracks
    target:
      entity_id: media_player.woonkamer
    enabled: true
mode: single
icon: mdi:shuffle-variant

Now we create a Grid card that contains 3 buttons.

  • In Home assistant – overview – select the 3 dots menu (top right) and edit dashboard.
  • Add Card: Grid card
  • Columns: 3
  • Search cards: Button
  • Entity : piCorePlayer - randomplay genre Unknown
  • Name: Rock 2000 mix
  • Click Save and you now have a button that starts a random mix from the Rock 2000 folder.

Now we add the “All songs mix option” that starts a random mix from all folders (6000 mp3’s).

  • Edit the grid card that holds the previous “Rock 2000 mix” button
  • In the top menu there is a “+” sign, click it
  • Search cards: Button
  • Entity : piCorePlayer - randomplay genres All
  • Name: All songs mix
  • Click Save and you now have a second button that starts a random mix from all 250 folders.

Now we add the “Clear playlist option” that clears the playlist.

  • Edit the grid card that holds the previous “Rock 2000 mix” button
  • In the top menu there is a “+” sign, click it
  • Search cards: Button
  • Entity : [select your Mediaplayer]
  • Name : Clear Playlist
  • Icon : mdi:playlist-remove
  • Tap action : Call Service
  • Service : Media player: Clear playlist
  • Targets : [Choose Entity and select your Mediaplayer]

Click Save and you now have a third button that clears the playlist.

If you get the hang of it and would like to add more commands to control your LMS from Home Assistant, see a list of all LMS API commands at : http://[YOUR_LMS_SERVER_IP_ADRESS]:9000/html/docs/cli-api.html?player=#Supported%20Commands

1 Like

Hello - thanks for the interesting post, though I have to say I wouldn’t do it that way!

First disclaimer - my LMS library is all in FLAC files, not MP3s. But a key feature of LMS is that it allows multiple Genre tags - I’m not entirely sure whether this works in MP3s. But in FLACs it definitely does, and could be liberating for you - then you can have multiple overlapping genres and therefore playlists, using your method. Also a lot more flexible than using folders.

Hi Hillman10, thanks for your reply. I’m not sure that multiple genres also works with mp3’s when controlling it from Home assistant (using the LMS CLI), but I think it does according to the documentation: e.g. "randomplaychoosegenre Rock Pop 1 ". Unfortunately that doesn’t help me with the problem I described. I want to play a random mix of music files based on folders so I can manually add and delete mp3’s in that specific folder, regardless of the genre. This way I know exactly what mp3’s can be selected in the random mix. The ‘randomplaychoosegenre’ command selects all music files in your entire library with the genre(s) you select, regardless of the folder that they’re in.

Hi - your example slightly misses the point, I think - that is telling LMS to make a random mix of all songs that have either the genre Rock or the genre Pop.

I’m thinking of tagging songs with multiple genres, eg “Rock” and “SoftRock” and “MySuperPlaylist1” or whatever. Home Assistant should neither know nor care that this is going on. The only issue is whether MP3s can hold multiple Genre tags: my suspicion is that LMS will be happy, but other consumers of those same MP3s might not.

Your problem is that if you insist on using folders to delineate playlists then you are committed to either duplicating files or not allowing any one file to be in more than one playlist.

It’s up to you, of course, but I just thought I’d point out to other readers that there are other ways to do this!

Can you plz guide how to send sort command via homeassistant to LMS.

Eg to sort a playlist/queue by title