Play a random Spotify playlist on Squeezebox

FYI: I’m running the AIO on a Pi3. I haven’t had any permissions issues. I took a look and all of my scripts are owned by pi.

Thanks again - might be time for a clean install test since a simple copy script fails too. Time to order some spare SD cards.

Chances are I have broken something somewhere with my fiddling.

I bought a couple extra cards, but in practive, I’ve just been making IMG backups every once in a while. Cheaper.

Unfortunately, I’ve been doing that in theory rather than practice.

The plan was, clean install, get all basics working then image, (and incremental image each time I make major changes).
The day after I did my AIO clean install the async update came through and I’ve been fighting with it ever since.

I thought you might like to see how I adapted your script to make a radio station I turn on and off with Alexa. Here’s a simplified version of my music robot yaml.

robot_radio:
  alias: robot radio
  sequence:
    - service: script.morning_easy
    - service: script.evening_gentry
    - service: script.nicht_muzik
### Morning ###
morning_easy:
  alias: play random music in morning
  sequence:
    - condition: time
      before: '14:00:00'
      after: '06:00:01'
    - service: shell_command.squeeze_shuffle_songs
    - service: media_player.play_media
      data:
        entity_id: media_player.hoolio
        media_content_type: PLAYLIST
      data_template:
        media_content_id: >
         {{ ["PlaylistM1", "PlaylistM2", "PlaylistM3", "PlaylistM4", "PlaylistM5", "PlaylistM6", "PlaylistEM", "PlaylistM8", "PlaylistM9", "PlaylistM10"] | random}}

### Evening ###
evening_gentry:
  alias: play random music in evening
  sequence:
    - condition: time
      before: '21:00:00'
      after: '14:00:01'
    - service: shell_command.squeeze_shuffle_songs
    - service: media_player.play_media
      data:
        entity_id: media_player.hoolio
        media_content_type: PLAYLIST
      data_template:
        media_content_id: >
         {{ ["PlaylistE1", "PlaylistE2", "PlaylistE3", "PlaylistE4", "PlaylistE5", "PlaylistE6", "PlaylistE7", "PlaylistE8", "PlaylistE9", "PlaylistE10"] | random}}

### Night ###
nicht_muzik:
  alias: play random music at night
  sequence:
    - condition: time
      before: '23:59:00'
      after: '21:00:01'
    - service: shell_command.squeeze_shuffle_songs
    - service: media_player.play_media
      data:
        entity_id: media_player.hoolio
        media_content_type: PLAYLIST
      data_template:
        media_content_id: >
         {{ ["PlaylistN1", "PlaylistN2", "PlaylistN3", "PlaylistN4", "PlaylistN5", "PlaylistN6", "PlaylistN7", "PlaylistN8", "PlaylistN9", "PlaylistN10"] | random}}


robot_stop:
  alias: kill the radio
  sequence:
  - service: media_player.turn_off
    entity_id: media_player.hoolio

I have a bunch of playlists set up on my squeezeserver because my internet connection is rubbish. These playlists are split into different times of day based on my listening preferences. For an easy start to the day I have 10 different playlists called “PlaylistM1” “PlaylistM2” …M10 and the same thing setup for other times of day. Some of those playlists contain hundreds of songs so I get a lot of variety.

So via Ha-Bridge I say “Alexa, turn on robot radio” which runs the robot_radio script and the turn off command is linked to the robot_stop script at the end of the file.

I’m using include script: !include_dir_merge_named scripts in my config file so no hyphens on the scripts.

I wasn’t able to work out multiple instances of the time condition in a single script so I have worked around it by using scripts running sequentially within a script. If one fails to meet the condition it moves onto the next script. I suspect there is a far better way to do this.

2 Likes

I have worked out my shell command / .sh problems finally. I used a clean blank install and trial and error until I got it working and then replicated on my full install. Really I think I just needed to step away for a few days, too frustrated to see what was right in front of me perhaps…

For anyone else who has the same issue see here

Hi @ih8gates,

I was trying to use your script to play spotify over my chromecast (via LMS).
I managed to play single songs, but not being able to use playlists.
Also, every if i entered multiple song URIs in the script (to replace playlists), it can play only one song, not to mention shuffling. Any idea how i can make it work? Thanks!

If you get the Spotify URI for a playlist and load it with media_content_id, that should work. And does work for me every day.

I don’t think it’ll work with multiple separate tracks - each one will replace the first.

Thanks! May I know what plug-in you use for Spotify in LMS?

I use the Spotify 1.0 from Logitech and the Spotify Protocol Handler 1.1.1 (Triode).

I am using the same plug-in, but somehow i still cannot play playlist. But can get album to play. However, i cannot get a “random” alum to play. It is always the same album. (the first one). Any idea where i did wrong?

  randomsqueeze:
    alias: "Morning music"
    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.lms_google_home
          media_content_type: PLAYLIST
        data_template:  
           #add as many playlist or album IDs as you like
          media_content_id: >
              {{ ["spotify:album:5dN7F9DV0Qg1XRdIgW8rke", "spotify:album:7hPq9fDWwXPo1tT0oi3XcM", "spotify:album:2rQ135imvelvp89D8eEZOi", "spotify:album:7xl50xr9NDkd3i2kBbzsNZ", "spotify:album:0vNPAhooQJWujYOZrsOJw8", "spotify:album:2MKNnI6Ttt2kF8kHWWG930", "spotify:album:2MKNnI6Ttt2kF8kHWWG930"] | random}}

This is related to an issue with the random filter not working. If you restart HASS, you’ll get a different random position initially, then it’ll keep using that position until HASS restarts. Very annoying - add your voice to the conversation in hopes that it gets fixed soon:

But you should be able to get a playlist to play if you can get an album to play by its URI.
Playlist URIs look like: spotify:user:ih8gates:playlist:32kolpL0K7jto6GQZE2skl

Get the URI for one of your own playlists by right-clicking in the Spotify app and choose “Copy Spotify URI”

Got it! Will go that discussion later.
About the URI, i actually thought the same. However, no matter i tried my own playlist, or the Spotify playlist, they all won’t work.

spotify:user:spotifytaiwan:playlist:1JsaeXIvhkTntGh4SjUd0q
spotify:user:yunglinglee:playlist:1ntqr5uKpEk7CwMmdHPZ51

No idea why…

That’s very strange.

What if you try via the services dev tool?

Use media_player.play_media with this payload (changing media_player.squeezeplay to match your device’s entity_id):

{“entity_id”:“media_player.squeezeplay”,“media_content_id”:“spotify:user:yunglinglee:playlist:1ntqr5uKpEk7CwMmdHPZ51”,“media_content_type”:“PLAYLIST”}

Have tried this…the log returns

[00:04:03.043451] log_message:81 log: 16:04:03.043 E [ap:4210] ChannelError(0, 1, playlist)

But if you do the same exact thing (using the dev tool) and put an album URI in in-place of the playlist URI, it works fine?

Sounds like maybe it’s an issue with the protocol handler. If I look at the settings for mine (in LMS), the version of the Libspotify API I have is: 12.1.51.g86c92b43 Release win32

Yes. The album URI works fine in the dev tool.
Let me go back to check the Libspotify version. Will share later. Thanks a lot!

Just checked my Spotify plug in, it is 1
2.1.103.gd51f6226 Release Linux-armv6-bcm2708hardfp

I am using it on a raspberry pi. Is this where the problem comes from?

I would hope that running on Linux shouldn’t be the problem. Looks like you’ve got a newer revision of the API than I have. So I don’t expect that’s the issue.

Let’s take a look at the LMS server directly and see if the problem’s there. Create a shell script on your PI with this as the contents:

curl -X GET \
    -H "Content-Type: application/json" \
    -d '{"id":1,"method":"slim.request","params":["bc:5f:f4:e9:44:4a",["playlist","play","spotify:user:ih8gates:playlist:35Q88WL7PvvRzLTuoy60hA"]]}' \
    http://192.168.1.110:9000/jsonrpc.js

You’ll need to replace the “bc:5f:f4:e9:44:4a” part with the MAC of your player as seen by LMS. You can find this by opening “settings” on your LMS interface, then go to the player tab. There should be a drop-down with a list of players. Choose one and the mac will display under “player info”.

Then change the IP address in that last line to match the IP of your LMS server. You can experiment with different Spotify URIs in that setting that starts with “spotify:user…”

run the script (“bash scriptname.sh”) and see what happens.

Albums should work - curious to see if playlists do.

Got it! Let me try after work today, thanks for your kind help!