Automation Error with Playing Spotify

I have a 6am alarm, which didn’t run this morning (thankfully I was awake lol), however when I ran it manually at 6:10, the actions worked. This is the automation:

alias: Alarm - 6am Weekday
description: ""
triggers:
  - trigger: time_pattern
    hours: "6"
    minutes: "0"
    seconds: "0"
conditions:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
actions:
  - action: script.bedroom_play_music
    metadata: {}
    data: {}
  - action: light.turn_on
    metadata: {}
    data:
      effect: Sunrise
      profile: Sunrise
    target:
      device_id: ba69d9b376a97007a9e3c1f4ce75534e
mode: single

This is the script.bedroom_play_music:

alias: Bedroom - play chill music
sequence:
  - data:
      device_name: Bedroom Echo Dot
      uri: spotify:playlist:37i9dQZF1EIhgfi5yT3B9p
      random_song: true
    action: spotcast.start

And this is the error:

Logger: homeassistant.components.script.bedroom_play_music
Source: helpers/script.py:526
integration: Scripts (documentation, issues)
First occurred: 06:00:03 (1 occurrences)
Last logged: 06:00:03

Bedroom - play chill music: Error executing script. Unexpected error for call_service at pos 1: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/play?device_id=3d107b00-8bcc-4929-a6fd-910ab947ba03_amzn_1: Not found., reason: None
Traceback (most recent call last):
  File "/usr/local/lib/python3.13/site-packages/spotipy/client.py", line 271, in _internal_call
    response.raise_for_status()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/requests/models.py", line 1024, 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/play?device_id=3d107b00-8bcc-4929-a6fd-910ab947ba03_amzn_1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 526, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 764, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<9 lines>...
    )
    ^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 727, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2795, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2845, in _execute_service
    return await self._hass.async_add_executor_job(target, service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/concurrent/futures/thread.py", line 59, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/spotcast/__init__.py", line 378, in start_casting
    raise exc
  File "/config/custom_components/spotcast/__init__.py", line 349, in start_casting
    spotcast_controller.play(
    ~~~~~~~~~~~~~~~~~~~~~~~~^
        client,
        ^^^^^^^
    ...<5 lines>...
        start_position,
        ^^^^^^^^^^^^^^^
    )
    ^
  File "/config/custom_components/spotcast/spotcast_controller.py", line 389, in play
    client.start_playback(**kwargs)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/spotipy/client.py", line 1821, in start_playback
    return self._put(
           ~~~~~~~~~^
        self._append_device_id("me/player/play", device_id), payload=data
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/local/lib/python3.13/site-packages/spotipy/client.py", line 338, in _put
    return self._internal_call("PUT", url, payload, kwargs)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/spotipy/client.py", line 293, in _internal_call
    raise SpotifyException(
    ...<5 lines>...
    )
spotipy.exceptions.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/me/player/play?device_id=3d107b00-8bcc-4929-a6fd-910ab947ba03_amzn_1:
 Not found., reason: None

That’s not an automation error. It triggered, passed the conditions and ran the action.

The action script failed because the URL that the script wanted to access was not available at the time.

Suggest you don’t rely on internet access as your morning alarm…

My Sonos system sometimes behaves the same way. If it has sat idle, occasionally it will fail for whatever reason when attempting to play audio but will work on the next attempt.

If the play media action is important to you, such as in an alarm clock, you should add a continue_on_error parameter followed by a short delay action and wrap it all in a loop which retries the action if state does not update to playing within a certain time.