Multiple Spotify Accounts with Sonos speaker in automation

I created a NFC Tag reader to let my kids select which playlist is played on their sonos speaker.

This works pretty well with this automation:

alias: NFC play music tag url nfckizi1
description: NFC play music tag with url from nfckizi1 on Kinderzimmer 1
trigger:
  - platform: event
    event_type: esphome.music_tag
    event_data: {}
condition:
  - condition: template
    value_template: "{{ trigger.event.data.reader == nfckizi1 }}"
action:
  - service: media_player.play_media
    target:
      entity_id: media_player.kinderzimmer_1
    data:
      media_content_id: "{{ trigger.event.data.url }}"
      media_content_type: music
    metadata: {}
  - service: light.turn_on
    target:
      entity_id: light.nfckizi1_2a0ee8_nfc_kizi1_led
    data:
      rgb_color:
        - 0
        - 0
        - 255
      flash: long
mode: queued
max: 10

Unfortunatelly most times it uses my main account which causes to interrupt the playback if a second stream starts.

So I want to select which account is used and added all accounts with his integration: Spotify - Home Assistant

But it seems that this integration currently does not support sonos speakers.
I’m able to use chromecast devices but the sonos speakers are not listed as source under media_player.spotify_accountname

I tried several attempts to workaround to be able to controll spotify accounts and sonos speakers.

I there anybody with a solution I may try?
May GitHub - music-assistant/hass-music-assistant: Turn your Home Assistant instance into a jukebox, hassle free streaming of your favorite media to Home Assistant media players. works?

Hey, I was thinking about doing something similar. Can you explain the above logic a bit further? Where did you get the “value_template” value from?

Ideally, spotify would be called through sonos, because sonos can play multiple streams on one account if called directly. But afaik this is not possible atm.

@mor3dr3ad
sure :slight_smile:
The NFC Tag reader decides on knows Tags to call event “esphome.music_tag” for all other “homeassistant.tag_scanned”

Using any https://open.spotify.com hyperlinks will cause the other players to stop.
Using the x-sonos-spotify:spotify media_id can only playback one singe track. I was not able find any documentation about this type of links. Does someone have any documentation?

I event tried the Spotify URI with spotify:trach:{TRACKID} spotify:playlist:{PLAYLISTID} and spotify:album:{ALBUMID} which seem sometimes tp work with parallel (at least used over on account)
Unfortunatelly spotify:user:{USERNAME}:playlist:{PLAYLISTID} is not working.

Any hint is welcome

Just came across this post, facing a related issue. Did you ever find a solution?

Unfortunately not a reliable version.
If you find any solution any share is welcome :pray:

1 Like

Seems to work fine for me. See the thing is this, Sonos can play media from Spotify in (at least) two ways: Either through Spotify Connect, or completely independently.

Make sure you are not already playing something through Spotify Connect, then use Sonos own app to navigate to a track in Spotify and play it. If you now look in the Spotify client, you will notice that it cannot control the stream, because Spotify Connect is not used.

How to make use of this in an automation? Don’t copy media id’s from the Spotify client, which begin with https://open.spotify.com/…, as those will probably always play through Spotify Connect which only supports a single stream. Instead use media ids from Sonos which will begin with spotify://… (media type should also begin with spotify://…).

How to find these media ids? Easiest is to use the media picker in Home Assistant. Create a blank automation, click Add action > Media player > Play media (the topmost alternative, not the one further down!), select a Sonos speaker and pick your media, then choose Edit in YAML from the … menu and copy the media id and type from there.

Thanks for the reply.

In cases I use the Sonos app the media id look like following:
media_content_id: x-sonos-spotify:spotify%3atrack%3a[ID]?sid=9&flags=8232&sn=2
I seem with sn I can select the spotify family account.
This however only allows me to play a single track.

Do you have any hint, how to select an album or playlist?

Following is rejected: Sonos Playlist could not be found

action: media_player.play_media
target:
  device_id: 40367999156a4d7b8156df649698f493
data:
  media_content_id: >-
    x-sonos-spotify:spotify%3aplaylist%3a0yDagEjoveJC0dVWBJecU2?sid=9&flags=8232&sn=1
  media_content_type: playlist

Again, Home Assistant > blank automation > Add action > Media player > Play media (the topmost alternative, not the one further down!), select a Sonos speaker and pick your media, then choose Edit in YAML from the … menu and copy the media id and type from there. The media picker will show tracks, albums, artists, etc. that you have added to your library in Spotify.