Spotify Integration in Home Assistant

Hi,
Is the below code correct? I am able to view what I am playing in HA (and stop start music), but now I would like to make some automations and play a playlist.

- id: '1575291428570'
  alias: Stream Test - Spotify
  description: Stream Test - Spotify
  trigger: []
  condition: []
  action:
  - data:
      entity_id: media_player.openplanspeaker
      volume_level: 0.3
    service: media_player.volume_set
  - data:
      media_content_id: spotify:user:spotify:playlist:37i9dQZF1DXcBWIGoYBM5M?si=Sco3zGxgQtyIeBC6FSoTfw
      media_content_type: playlist
    service: spotify.play_playlist

I am getting the below error:

ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.stream_test_spotify. Invalid data for call_service at pos 2: extra keys not allowed @ data['media_content_type']

From what I can gather in the docs, you need to omit
media_content_type: playlist
as that is not a valid argument for the spotify.play_playlist service

thanks Triss!!
you could be right … now I am getting a different error:

requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.spotify.com/v1/me/player/play

any idea what this means?

The documentation is at https://www.home-assistant.io/integrations/spotify/#uri-links-for-playlistsetc

At the first sight, your media_content_id: contains something that shouldn’t be there or does not exist (hence the 404 not found error)… I’m guessing you need to leave the ?si... part off

thanks for getting back to me, still not working but now I am not getting any errors.
I made the change you suggested and found another silly mistake … I had to use media_player.play_media NOT media_player.media_play.

Do I need to input a user ID or something?

I hadn’t set up this integration yet, but now did so I could experiment with it :slight_smile: Cool, but not yet sure how to make i usefull, as it seems prone to throwing up errors if you ask it to do something “illogical”, such as asking to play without some playlist loaded first :slight_smile:

as for your example, try without the ’ around the media_content_id? That did the trick for me…

I tried playing a playlist on my cellphone, possibly needs the spotify app “open” first as well, not sure yet about that.

media_content_id seems to be a mandatory field.
can you share a screenshot or code?

I really need this to work to make some automations :slight_smile:

you misunderstand - just remove the single quotes from around the spotify string, eg
media_content_id: spotify:user:spotify:playlist:37i9dQZF1DWSkkUxEhrBdF
NOT
media_content_id: 'spotify:user:spotify:playlist:37i9dQZF1DWSkkUxEhrBdF'

I think the quotes are only needed when you put an actual http url, for example.

Well, in my limited trials, even with a working servie call it seemed spotty (:wink: ) if it would work or wouldn’t, depending on if the app was already active etc - but limited testing, and only with the app on an android device.

I am getting a beep on the speaker (I assume it is waking up) … but nothing plays :frowning:

Not much time to play with it right now, but as I said for me it also wasn’t very reliable during my brief test… You could try to turn on the speaker first, maybe send something else from spotify to it (like from a cell phone) to make sure the spotify on it is “awake” (as that seems to be a factor on my phone).

Or try another device, if you have other “spotify connect” enabled device, such as an app on your cellphone maybe? Does it work if you play/pause when something is already playing?

Are you sure the device is spotify connect enabled? You can send spotify to anything with a google cast builtin for example, but it isn’t fully spotify connect, afaik (eg i can cast spotify to a chromecast, but not spotify connect it (like via hass) - even though the spotify app doesn’t show the distinction. But maybe that’s just me? :thinking:

will test it out again :slight_smile: but I do not think this will work

Upter upgrading to 0.106 (docker) sand changing the configuration, I get strange error:

Error loading /config/configuration.yaml: in “/config/configuration.yaml”, line 68, column 10: Unable to read file /config/./integrations/spofity.yaml.

Any idea why?

Thx

I am currently running a headless Hassio on a RPI3b+. I have been unable to change the audio from my HDMI cable to the 3.5mm jack. I am unable to ssh into the RPI, but have full File Editor control from Hass Frontend.

Can anyone help me with what I should be putting into the config file or yaml to get this to work?

Thanks in advance.

Hey @mouthpiec, not sure where you got to with this but I just wrote an entire tutorial on the Spotify integration with plenty of screenshots and code, maybe it can help :slight_smile:

thanks … looks very promising.
I never tried scripts but it does not look difficult.

Yes I think showing how to use a service with a simple script is the easiest way to test/demonstrate the function (of course you can also use the developer tools). Once you can confirm a simple script triggers the action, you can use it in more complex automation.

If you do try out the tutorial and run in to some issues, feel free to leave a post here as I am happy to help and always looking to improve my content :slight_smile:

1 Like