Trying to play Spotify album on Sonos

It‘s all on S2

1 Like

Just tried it with 0.117 and it works!
Thanks @amelchio

For reference, my automation for magic cards:

- alias: Play Sonos Item from Magic Cards
  trigger:
    - platform: event
      event_type: magic_card_scanned
  condition:
    condition: template
    value_template: "{{ 'spotify:' in trigger.event.data.card_uri }}"
  action:
    - service: media_player.play_media
      data_template:
        entity_id: media_player.sonos_kitchen
        media_content_id: "{{ trigger.event.data.card_uri }}"
        media_content_type: music

1 Like

Yea just done the same thing after hours of “wasted” research! It works beautifully! Thank you

Hi guys! I have a RFID jukebox up and running, but it seems impossible to play the music on my Sonos. I was wondering if it would be possible to alter this YAML to play the music on my kitchen Sonos:


alias: NFC jukebox
description: Build our own NFC jukebox
trigger:
  - platform: event
    event_type: tag_scanned
condition:
  - condition: template
    value_template: '{{ trigger.event.data.tag_id in tags }}'
  - condition: template
    value_template: '{{ trigger.event.data.device_id in media_players }}'
action:
  - variables:
      media_player_entity_id: '{{ media_players[trigger.event.data.device_id] }}'
      media_content_id: '{{ tags[trigger.event.data.tag_id].media_content_id }}'
      media_content_type: '{{ tags[trigger.event.data.tag_id].media_content_type }}'
  - service: media_player.play_media
    data:
      entity_id: '{{ media_player_entity_id }}'
      media_content_id: '{{ media_content_id }}'
      media_content_type: '{{ media_content_type }}'
  - delay: '2'
mode: single
max_exceeded: silent
variables:
  media_players:
    8e66dd521db611eb80e95bdaeaa39894: media_player.spotify_svanjr
  tags:
    04-DE-13-BA-58-49-80:
      media_content_id: 'spotify:album:2DGzTm2R2v3G0IjnxXtP3Y'
      media_content_type: album
    77-AC-8F-A7:
      media_content_id: 'spotify:playlist:37i9dQZF1DX0q1RHoDiZBg'
      media_content_type: playlist
    FA-66-EA-80:
      media_content_id: 'spotify:album:0xtOxVRRaA0kW7X4PlbtuP'
      media_content_type: album

You do not describe what goes wrong (like any error messages) but I think you should at least use media_content_type: music.

@amelchio

Hey there! Play by URI isn’t working for me. Here is what I am trying:

service: media_player.play_media
entity_id: media_player.master_bedroom
media_content_id: https://open.spotify.com/album/1bt6q2SruMsBtcerNVtpZB
media_content_type: music

Here is what I see in my logs:

2020-11-10 22:19:49 ERROR (SyncWorker_48) [homeassistant.components.sonos.media_player] Error on play_media with UPnP Error 800 received:  from 192.168.1.103

Any tips?

That error is being investigated her: https://github.com/home-assistant/core/issues/42538

You can help by adding information about your setup.

1 Like

The UPnP Error 800 should now be fixed in Home Assistant 0.118.

This may very well be impossible, but I figured it’s worth an ask.

Is there any way to use HA to command the Sonos device play Spotify Connect? In other words, not command it to play a specific album on Sonos, but have it play what would be the same as pressing Play in the Spotify app.

If I already started playing something on that Sonos speaker through the Spotify app and then pause it, then media_player.media_play_pause does indeed resume playing the Spotify music on the Sonos speaker. But if I then play another source (for example TV on my beam Sonos speaker) and then pause it… media_player.media_play_pause won’t do anything because that speaker is in “TV” source instead of the Spotify connect source. Any ideas how to get it back to the Spotify source without using the Spotify app or without commanding Alexa to “play spotify”?

Sorry, there is no support for actions where you otherwise have to use the Spotify app.

Thanks for your response. May be slightly off-topic but can you think of a way to call “Alexa play spotify” on the speaker without actually saying it verbally? Somehow Alexa does have the API (or other) access to play Spotify Connect music on the Sonos. I’ve been searching myself and haven’t found a way to do that.

When trying to play a playlist with saved podcasts in I get an error message.

Error on play_media with UPnP Error 701 received: Transition not available from 192.168.76.124

When playing a playlist with normal songs in it it works perfectly.

This is how my code look likes.

      - service: media_player.play_media
        data:
          entity_id: media_player.kok
          media_content_id: https://open.spotify.com/playlist/50Z5z6ISa97VBrY1qRqEDC
          media_content_type: music

is there any news here? Im converting from HS4 to HA and this is the last bit of the puzzle to shut down my HS4:)

If you notice an error, create a GitHub issue with all the requested details and we will try to fix it. Feel free to tag me as I might not see it otherwise.

service: media_player.media_play
data:
  media_content_id: https://open.spotify.com/playlist/37i9dQZF1DX1bDvyV0IkZ3?si=149883ef25494d57
  media_content_type: music
  entity_id: media_player.kitchen

I get this error:
Failed to call service media_player.media_play. extra keys not allowed @ data['media_content_id']. Got None extra keys not allowed @ data['media_content_type']. Got None

EDIT: RESOLVED : NOTE there is a media_play and play_media service!

Hey @patrickse thank you for this thread. Can you please share your final config? Trying to do something similar at home.
Best,
mor3dr3ad

I am not using Spotify anymore and move all my automations to Apple Music. What exactly are you Interessted in ?

Hey thanks for the quick reply. Actually this is exactly what I am looking for. Though probably not with a push button, but a tag that they can put on a tag reader. Did you get this to work at any point? I think the random album part might be tricky…

Ok… so what I did is… I’ve created a short script which uses a list of available albums on spotify/apple music sorted by the artist.

alias: Play Random Album
icon: mdi:pirate
mode: single
max_exceeded: silent
variables:
  uris:
    paw_patrol:
      - >-
        https://music.apple.com/de/album/folge-225-der-erste-einsatz-der-moto-patrol/1618276153
fields:
  media_player:
    name: Media Player
    description: Media player to be used for playback.
    example: media_player.kueche
    required: true
    selector:
      entity:
        integration: sonos
        domain: media_player
  artist:
    name: Artist
    description: Artist for playback
    example: paw_patrol
    required: true
    selector:
      select:
        options:
          - paw_patrol
sequence:
  - variables:
      album_id: >
        {%- set plists = uris[artist] -%} {% set pindex =  (range(0, (plists |
        length - 1 ) )|random) -%} {{ plists[pindex] }}
  - service: media_player.clear_playlist
    data:
      entity_id: "{{ media_player }}"
  - service: media_player.repeat_set
    data:
      entity_id: "{{ media_player }}"
      repeat: all
  - data:
      entity_id: "{{ media_player }}"
      media_content_type: music
      media_content_id: "{{ album_id }}"
    service: media_player.play_media
  - delay: "2"

I update the list of available albums every know and then. With spotify is rather easy because you can leverage the official API. With Apple Music is actually not that straight forward.

I’m still getting that error…
But I’m new with Home Assistant:
afbeelding

I don’t find the solution…