This can be adapted to various media_player’s I’m sure. I’m running Logitech Media Server with the official Spotify Plugin plus the Triode protocol handler. That allows me to use Spotify URIs.
The secret sauce is that Jinja templates can return a random item from an array. I got excited when I figured that out.
The script I’m posting also makes an API call to turn on shuffle-by-song so that the playlists are shuffled. Source for that below as well.
Keep in mind that you’ll need to change the entity_id (media_player.squeezeplay for me) to match your squeezebox or other media device.
script: randomsqueeze: alias: Play random playlist on Squeeze sequence: #a bash script that turns on shuffle-by-song - service: shell_command.squeeze_shuffle_songs - service: media_player.play_media data: #change this to match your player entity_id: media_player.squeezeplay media_content_type: PLAYLIST data_template: #add as many playlist or album IDs as you like media_content_id: > {{ ["spotify:user:netflixmusic:playlist:2X6z5kU0wMnKoar8i1RN6B", "spotify:user:ernestcline:playlist:5h6SYfQvJYAnr9S8Al4yOn", "spotify:user:stereogum:playlist:4VHDDPpx9BLScz0hMlFPqr"] | random}}
shell_command: squeeze_shuffle_songs: bash /home/pi/scripts/squeeze_shuffle_songs.sh
The bash script looks like this. Be sure to change the path in your shell_command to match wherever you put this (I created a scripts folder for these):
curl -X GET \ -H "Content-Type: application/json" \ -d '{"id":1,"method":"slim.request","params":["bc:5f:xx:xx:xx:xx",["playlist","shuffle",1]]}' \ http://192.168.1.110:xx/jsonrpc.js
You’ll need to change that first parameter (bc:5f:xx:xx:xx:xx) to the ID of the player that you want to trigger shuffle on.
Also, I hate leaving the player in “random” mode because I typically like to use “play next” and “play last” when I’m manually building a playlist while listening. So I have this code that turns off shuffle when music stops:
automation: - alias: SqueezePlay stops playing trigger: - platform: state entity_id: media_player.squeezeplay to: 'off' - platform: state entity_id: media_player.squeezeplay to: 'idle' action: - service: shell_command.squeeze_shuffle_off
shell_command: squeeze_shuffle_off: bash /home/pi/scripts/squeeze_shuffle_off.sh
squeeze_shuffle_off.sh looks really similar to squeeze_shuffle_songs.sh except for a zero in the shuffle payload:
curl -X GET \ -H "Content-Type: application/json" \ -d '{"id":1,"method":"slim.request","params":["bc:5f:xx:xx:xx:xx",["playlist","shuffle",0]]}' \ http://192.168.1.x:9000/jsonrpc.js
And last but not least, I have a handy script that tells my HTPC to turn on the receiver and set the right input. The mechanics of that will vary widely depending on how your setup works, but here’s the automation to get you started:
automation: ## turn on receiver when music starts ## - alias: SqueezePlay begins playing trigger: - platform: state entity_id: media_player.squeezeplay to: 'playing' from: 'idle' - platform: state entity_id: media_player.squeezeplay to: 'playing' from: 'off' action: # do whatever you want