Spotify 404 from API from time to time

Hi,

I have a setup which automatically plays a playlist when I press a button in Home Assistant. This works most of the time, but sometimes it doesn’t start playing. And looking in the log I find this:

Error executing service <ServiceCall media_player.volume_set: entity_id=['media_player.spotify'], volume_level=0.3>

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/spotipy/client.py", line 121, in _internal_call
    r.raise_for_status()
  File "/usr/local/lib/python3.6/site-packages/requests/models.py", line 939, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.spotify.com/v1/me/player/volume?volume_percent=30

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/app/homeassistant/core.py", line 1038, in _event_to_service_call
    await service_handler.func(service_call)
  File "/usr/src/app/homeassistant/components/media_player/__init__.py", line 441, in async_service_handler
    await getattr(player, method['method'])(**params)
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/app/homeassistant/components/media_player/spotify.py", line 214, in set_volume_level
    self._player.volume(int(volume * 100))
  File "/usr/local/lib/python3.6/site-packages/spotipy/client.py", line 976, in volume
    self._put(self._append_device_id("me/player/volume?volume_percent=%s" % volume_percent, device_id))
  File "/usr/local/lib/python3.6/site-packages/spotipy/client.py", line 190, in _put
    return self._internal_call('PUT', url, payload, kwargs)
  File "/usr/local/lib/python3.6/site-packages/spotipy/client.py", line 126, in _internal_call
    headers=r.headers)
spotipy.client.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/volume?volume_percent=30:
 No active device found

This is the automation:

  - alias: Spotify - Bedroom Boolean Switch ON = TV + Spotify ON
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: input_boolean.spotify_toggle
        to: 'on'
    condition:
      - condition: and
        conditions:
          - condition: state
            entity_id: switch.bedroom_tv
            state: 'off'
          - condition: state
            entity_id: input_select.spotify_source
            state: 'Sovrum'
    action:
      - service: homeassistant.turn_on
        entity_id: switch.bedroom_tv
      - delay:
          seconds: 15
      - wait_template: "{{'Sovrum' in state_attr('media_player.spotify','source_list')}}"
        timeout: '00:01:00'
      - delay:
          milliseconds: 50
      - service: media_player.select_source
        data_template:
          entity_id: media_player.spotify
          source: >
            {% if is_state("input_select.spotify_source", "Vardagsrum") %} Vardagsrum
            {% elif is_state("input_select.spotify_source", "Sovrum") %} Sovrum
            {% endif %}
      - service: media_player.volume_set
        data:
          entity_id: media_player.spotify
          volume_level: 0.3
      - delay:
          milliseconds: 50
      - wait_template: "{{ is_state_attr('media_player.spotify', 'volume_level', 0.3) }}"
        timeout: '00:01:00'
      - delay:
          milliseconds: 50
      - service: media_player.play_media
        data_template:
          entity_id: media_player.spotify
          media_content_type: playlist
          media_content_id: >
            {% if is_state("input_select.spotify_playlist", "Hits idag") %} spotify:user:spotify:playlist:37i9dQZF1DWTMYgB8TqtmR
            {% elif is_state("input_select.spotify_playlist", "New Music Friday") %} spotify:user:spotify:playlist:37i9dQZF1DXcecv7ESbOPu
            {% endif %}
      - delay:
          milliseconds: 50
      - service: media_player.shuffle_set
        data:
          entity_id: media_player.spotify
          shuffle: true

Why is this happening? I have even set a wait template which checks if “Sovrum” is in the source_list. Why is Spotify saying “No devices”?

I have the same issue from time to time… But i also have the feeling that the spotify service it self is a bit “spotty” in the last couple of weeks… i also have skipping of songs, or the occasional mismatch in my Spotify Connect…

Seems like the scan_interval is making things worse. When I remove it it mostly works. But with scan_interval: 5 I get errors almost every time!

Spotiify is a cloud polling service, setting changing sacn_interval is not a great idea, setting it to under 10, is potentially bad.

You must give the platform enough time to compelte the request before it sends a new one :slight_smile:

Were you able to fix this?