Media_player.play_media not working with spotify

I’m trying to start a specific playlist with a script, but keep getting an error saying that extra keys aren’t allowed.

'1589884237793':
  alias: Play Spotify
  sequence:
  - data:
      media_content_id: "spotify:playlist:0Rl855D2r6w4SYHyLfSPHh"
      media_content_type: "playlist"
    entity_id: media_player.living_room_tv
    service: media_player.media_play

The error says Failed to call service script/1589884237793. extra keys not allowed @ data['media_content_id']

I’ve been through the documentation and examples here in the forum but nothing seems to work. Any ideas on what I might be missing?

update

I have this issue partially solved, the script below will play a specific playlist, but only if spotify is already playing something.

'1589884237793':
  alias: Play Spotify
  sequence:
  - data:
      media_content_id: spotify:playlist:0Rl855D2r6w4SYHyLfSPHh
      media_content_type: playlist
    entity_id: media_player.spotify_bruno_amaral
    service: media_player.play_media

If you look up Spotcast, it will probably explain why you can only start a playlist on a Chromecast that is already playing something.

Casting Spotify is not possible atm in a proper way, is my opinion.

Correct, there’s no way to start playing a Spotify playlist “cold” without something like Spotcast (which is currently not working). You could look into something like Logitech Media Server and Spotty.

@Emphyrio, @fuzzymistborn Thank you both, you have saved me a lot of time.

I worked around the problem by sending a sequence of button commands, get spotify to play the latest playlist, and then switch it with the one I wanted.

It seems to need a delay of 5s to make sure the interface is fully loaded on each press.

'1589881785488':
  alias: Tv on the Radio
  sequence:
  - data: {}
    entity_id: media_player.living_room_tv
    service: media_player.turn_on
  - data:
      source: Spotify Music - Listen free on LG TV
    entity_id: media_player.living_room_tv
    service: media_player.select_source
  - delay: 00:00:05
  - data:
      button: RIGHT
    entity_id: media_player.living_room_tv
    service: webostv.button
  - delay: 00:00:05
  - data:
      button: ENTER
    entity_id: media_player.living_room_tv
    service: webostv.button
  - delay: 00:00:05
  - data:
      button: ENTER
    entity_id: media_player.living_room_tv
    service: webostv.button
  - delay: 00:00:05
  - data:
      button: ENTER
    entity_id: media_player.living_room_tv
    service: webostv.button
  - delay: 00:00:05
  - data:
      button: ENTER
    entity_id: media_player.living_room_tv
    service: webostv.button
  - delay: 00:00:05
  - data:
      media_content_id: spotify:playlist:0Rl855D2r6w4SYHyLfSPHh
      media_content_type: playlist
    entity_id: media_player.spotify_bruno_amaral
    service: media_player.play_media
1 Like

Very creative :+1:

1 Like

Hi,
I also have an issue with the spotify playlist.
This is the script I have created:

spotify_eco99_last_100:
  alias: "playing eco99 last 100 songs"
  sequence:
    - service: media_player.play_media
      data:
        entity_id: media_player.denon_2
        media_content_id: "https://open.spotify.com/playlist/07QpDB6YxM6flpbmdZTZNX"
        media_content_type: playlist

This is the error I get when I try to run it:

Logger: homeassistant.components.script.spotify_eco99_last_100
Source: components/media_player/__init__.py:626
Integration: Script (documentation, issues)
First occurred: 7:49:08 PM (2 occurrences)
Last logged: 8:01:02 PM

playing eco99 last 100 songs: Error executing script. Unexpected error for call_service at pos 1:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 254, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 464, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1445, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1480, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 593, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 664, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 630, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 630, in async_play_media
    await self.hass.async_add_executor_job(
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 626, in play_media
    raise NotImplementedError()
NotImplementedError

I have tried to run it after spotify is already running but it didn’t helped.

that bit is in the wrong format. it should be something like

media_content_id: spotify:playlist:0Rl855D2r6w4SYHyLfSPHh

Thanks!
Now the script is not failing but nothing is happening.
Am I missing something else here?

Is that the whole script ? How are you triggering it and what happens ?

Not sure I can help but with that information maybe someone has an idea they can share.

That is the whole script.
For testing I’m triggering it from the scripts menu. It logs as triggered but nothing happens

And is spotify playing? There are some limitations with the API, that’s why I had to use that combination of sending key-strokes to make it start playing. Only then can you change the playlist.

Any errors in the logbook?

Have you tried setting first a source? you could try this too:

Source should contain the name of your speaker as it shows in your spotify account

1 Like

I think the issue is that you’re using media_player.media_play instead of media_player.play_media.

In case anyone wants another solution to this, I was able to track down the reason I couldn’t cold-start spotify on my roku smart tv. The tv takes a long time to show up as a source for media_player.spotify. Here is what I needed to do:

  • turn on the tv
  • open the spotify app on the tv
  • Update the media_player.spotify entity until the tv was listed as a source (you can check in devtools > states)
  • select the tv as the source for media_player.spotify
  • play playlist

Here is the configuration that worked for me. The loop where it checks for the tv as a source can take up to a minute or two sometimes:

sequence:
  - type: turn_on
    device_id: device_id
    entity_id: remote.50_hisense_roku_tv
    domain: remote
  - metadata: {}
    data:
      source: Spotify Music
    target:
      entity_id: media_player.50_hisense_roku_tv
    action: media_player.select_source
  - repeat:
      sequence:
        - target:
            entity_id: media_player.spotify
          action: homeassistant.update_entity
          data: {}
        - delay:
            hours: 0
            minutes: 0
            seconds: 3
            milliseconds: 0
      until:
        - condition: template
          value_template: >-
            {{ '50" Hisense Roku TV' in
            (state_attr('media_player.spotify', 'source_list') or [])
            }}
  - target:
      entity_id: media_player.spotify
    data:
      source: 50" Hisense Roku TV
    action: media_player.select_source
  - action: media_player.play_media
    data:
      media_content_id: >-
        https://open.spotify.com/playlist/{playlist}
      media_content_type: playlist
    target:
      entity_id: media_player.spotify
alias: Play Spotify
description: ""