Playing Spotify playlist on Sonos triggered by automation

While Im fully aware of that documentation of Spotify integration states that Sonos is not supported device in Spotify API and many threads on this forum discuss the same topic, Im wondering if there are any changes to this today? Or workaround that we might use?

I have latest version of HA (0.115.3) and integration with Spotify and Sonos (Sonos Gen 2). Playing playlist or track from Spotify on Sonos triggered by automation is highly desired.

Thanks for any ideas and suggestions!

1 Like

I kinda found a workaround for this today, as I was looking for a solution since yesterday. :partying_face:

What you need is an echo dot (or any other Spotify connect device) and of course sonos speakers.

When I played around with the frontend, I played music via my bathroom echo dot. Randomly I clicked play on my media player for my bedroom sonos one and it picked of right in the middle of the track where the echo dot in the bathroom has stopped. A little try and error with scripts and now Iā€™m able to start specific spotify tracks, albums and playlist (not yet working, but it should in theory :confused: ) directly on my Sonos speaker.

sonos_test:
  sequence:
    - service: media_player.select_source
      data:
        entity_id: media_player.spotify
# substitute with your spotify connect device name. In my case it's Echo Dot bathroom.
# I guess every Spotify connect device works, currently its only tested on echo dot
        source: "Echo Dot bathroom"
    - service: media_player.play_media
      data:
# This is the spotify integration from the frontend
        entity_id: media_player.spotify
# Get the spotify content URI via right-click on the title, playlist or album > share > Copy Spotify URI
        media_content_id: "spotify:album:7GoZNNb7Yl74fpk8Z6I2cv"
# Media_content_type is "music" for tracks and apparently "playlist" even for albums and of course playlists
        media_content_type: playlist
    - service: media_player.media_play
      data:
# Enter your sonos device media player here
        entity_id: media_player.bedroom

While it is not really elegant, it works - at least for me! This script needs probably a little more work for some cases, e.g. what happens when music is already played on the sonos (need to stop and clear the queue?), the echo dot still shows as playing while there is obviously no sound coming from it.

Any suggestions and improvements are more than welcome :slight_smile:

EDIT: it seems that sometimes the ā€˜linkā€™ from Sonos to Spotify is broken and it starts playing in the Echo Dot Bathroom (e.g. when using the native Sonos app to play something in the bedroom) This is easily fixed by creating a ā€˜linkā€™ between Sonos and spotify again by either telling Alexa to play something in the bedroom or using the Spotify App and choosing the Sonos speaker.

1 Like

Since Sonos One (2. gen) already have Google Assistant support (and I have configured it), Im able to play Spotify tracks on Sonos by talking to Google Assistant. So I might not need another Spotify connect device, like echo dot?

I donā€™t know about Google Assistant as Iā€™m using Alexa - but somehow the integration in Sonos is different as you cannot cast or stream music to the Smart Assistant functionality but only to Sonos directly. So yes - you need a different (true) Spotify Connect device in my opinion.

Ok, I will give it a try and see how it goes first.

I donā€™t know if this is what youā€™re afterā€¦ but you can add Spotify playlists to ā€œMy Sonosā€ via the Sonos app and the according playlists will show up as a source on the Sonos media_player. Those can be called very easily from automations, scripts etcā€¦

2 Likes

This works only to a certain extent as the ā€œMy Sonosā€ favorite section is limited to ~80 playlists
I reached the limit when I wanted a random audiobook player (over 200 audio books) from spotify and tried adding all of them to My Sonos.

But yes, for only a few playlists this is the easiest solution!

Thanks, that is better then nothing. But still limits different use cases using media player and the new Tags support where Im planning to create a jukebox game, where random track is played and people must guess artist and song (for example). Or just playing random track based on the mood.

I have added the ability to play Spotify URIs to the Sonos integration, will be in Home Assistant 0.117.

You can try it out with HACS now, see Trying to play Spotify album on Sonos.

1 Like

@amelchio I have installed Sonos Beta integration in HACS (master-branch version), but when calling the service below, I get following error message in logs:

Logger: custom_components.sonos.media_player
Source: custom_components/sonos/media_player.py:461
Integration: Sonos ([documentation](https://www.home-assistant.io/integrations/sonos))
First occurred: 12:46:05 AM (1 occurrences)
Last logged: 12:46:05 AM

Error on play_media with UPnP Error 800 received: from 192.168.0.14

(this is the IP of Sono One)

Service call:

- service: media_player.play_media
  data:
    entity_id: media_player.sonos_stue_1
    media_content_id: 'https://open.spotify.com/album/1bt6q2SruMsBtcerNVtpZB'
    media_content_type: music

Please try with a URI that you create yourself to make sure the music is available in your region.

I tried creating my own URI, both ā€œPlaylist linkā€ and ā€œSpotify URIā€. The error that appears in my log is:

'Logger: homeassistant.components.sonos.media_player
Source: components/sonos/media_player.py:461
Integration: Sonos (documentation, issues)
First occurred: 18:52:38 (2 occurrences)
Last logged: 19:12:55

Error on play_media with UPnP Error 714 received: Illegal MIME-Type from 192.168.50.143ā€™

I did ā€œCopy Album Linkā€ but still receiving the same error message. Do I need to delete existing Sonos Integration before installing the Sonos (Beta) Integration through HACS maybe?

A side note: Since HA canĀ“t automatically discover my Sonos One speakers I have following configuration:

sonos:
  media_player:
    hosts:
      - 192.0.0.14
      - 192.0.0.15

What does ā€œUPnP Error 800ā€ mean?

I think this means that you did not install the beta Sonos integration. It is in 0.117.

If it is not due to an invalid URI, I donā€™t know what it means.

1 Like

How about my second question?

Do I need to delete existing Sonos Integration before installing the Sonos (Beta) Integration through HACS maybe?

You donā€™t have to delete it.

Why is that?

Forget it! Just some missunderstanding from my side! I figured out that Sonos Integration actually did manage to automatically discover my Sonos player, so I no longer need to configure the IPs manually. But still receiving the same error message :frowning:

@amelchio Just checking if it is required to add the Spotify playlist in Sonos App before calling the media_player.play_media service?

That is not required.