Shuffle_set media player service does not work on Spotify

I frequently use Spotify integration to play playlists from my account. Since playlists always start in “regular” mode by default rather than shuffle mode, I include the “media_player.shuffle_set” service in my automation and script sequences to ensure my playlists are shuffled. This service is applied to the media player entity in my Spotify integration setup.

In the past month, however, I’ve noticed that this service fails to activate randomly—sometimes it works perfectly, but other times it encounters errors. When this happens, the remaining actions in the sequence don’t execute, causing additional problems.

1

You might give my SpotifyPlus integration a try if you continue to have issues with the Spotify integration.

The issue could be a number of things. Any messages in the system log stand out? Or any other issues on your network happening around the same time?

Hello Todd,

Thank you for your response and mainly for your integration. I gave it a try but nothing happened after I called the service. However interestingly when I switched to shuffle mode using other control methods (my TV remote control in this case), when I called the service it “unshuffled” back again. Something strange is going on here. I think there is some kind of dysfunction in the service itself, and it has nothing to do with your integration, Spotify’s native integration, or any other integration for this matter.

Do you have any clue of what could have gone wrong here?

Can you post your full script?

If you are doing multiple calls that in turn call the Spotify Web API, then they may get out of sync if there is no delay between the calls. The order of execution by the Spotify Web API is not guaranteed.

Another thing you might try is enabling debug mode on the Spotify integration. That may provide more clues in the system log about what is happening.

Here’s the set-shuffle service definition for the Spotify integration - pretty simple, so I don’t think there’s anything there that is causing issues.

    @spotify_exception_handler
    def set_shuffle(self, shuffle: bool) -> None:
        """Enable/Disable shuffle mode."""
        self.data.client.shuffle(shuffle)

Thanks Todd,

I did enable the debugging mode of this integration as problems started, about a month ago. Since I’m a relatively newbie, I have no idea how to reach these logs. Maybe you can help - as suggested - to find any clue in the script itself. So here is the script. Hopefully, you might spot something.

Thank you so much, Todd!

The debugging output show up in the System Log. You can view that by navigating to Settings \ System \ Logs, of you can use a direct link of http://homeassistant.local:8123/config/logs (change the prefix to your HA link). Once you are there, click on the “Load Full Logs” button to display the full log. It should look something like this (note that my log is not very full, as I just updated / rebooted HA this morning):

Are you just wanting to play a Spotify playlist with shuffle enabled (on)? If so, then it’s a 2 step script - enable shuffle, and play media.

Note that if you set shuffle true AFTER playing the media, then the first track will always play first.

If you set shuffle true BEFORE playing the media, then a random track will be selected for play first.

Like so:

alias: Spotify Playlist with Shuffle On
sequence:
  - service: media_player.shuffle_set
    data:
      entity_id: media_player.spotify_todd_l
      shuffle: true
  - service: media_player.play_media
    data:
      entity_id: media_player.spotify_todd_l
      media_content_id: spotify:playlist:37i9dQZF1E39vTG3GurFPW
      media_content_type: playlist
mode: single
icon: mdi:music

I am not sure what you are trying to accomplish by the “if … then … else” logic in the script you presented. Were you having issues with the shuffle not working maybe?