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.
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
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.
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.
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
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.