Spotcast - custom component to start playback on an idle chromecast device

Yeah, you can definitely do some advanced stuff with the Sonos component, and the ability to send URIs is a great improvement. However I suspect it is still not possible to do some of the things I wanted, which includes multiple Spotify accounts. (i.e. start playback from Spotify account A to Sonos speaker 1, and then start playback from Spotify account B to Sonos speaker 2).

1 Like

Cool, thanks!

hi, i have a problem and i don’t know if it’s spotcast or spotify’s fault.
My problem is that after one hour the stream is interrupted (play song on chrmecast that are plugged into tv, all go well until, after about 1h, chromecast switched from playing to idle mode).
I did not find any error in the logs.
has anyone had the same problem as me? how can i solve?
thanks for all

See: Spotcast - custom component to start playback on an idle chromecast device

And my quote of fondberg there as well. This isn’t anyone’s ‘fault’ per se. It’s just how Spotify works and how spotcast is written in order to use it. Short answer: there is nothing you can do about this and hopefully Spotify someday releases a decent API other people can use.

Yeah, this is a known limitation because of how this component currently works.

However I think it should be possible to work around this if you only use spotcast to “wake up” the device you want to play to, and then use the normal “media_player.play_media” service to play media to the device which should be available as a source after it’s woken up by spotcast.

Have anyone tested this? If not I will report back when I have gotten the time to test it.

Won’t work. When you started a stream with spotcast, it will interrupt after 60 minutes of playback, regardless of stopping and starting again through spotcast or a different device. The token will expire after 60 minutes, even if you play it on a different device and it will stop anyway. That is what I experienced at least always.

I haven’t used spotcast for the last month now, I stopped with Chromecast alltogether so I don’t know if stuff has changed on Spotify’s end last month.

I create an automation that after 50 Min restart spotcast (before I check if Chromecast is on on Spotify).
It’s workaround but for now it’s ok
UPDATE
if i put 50 min timer it doesent work (i think that when i call for the second time spotcast it see that there are a valid token and it reuse that)
i think it should work if the timer is set to exactly 60 minutes with the eventual cut of the song you are listening at the 60th minute

Hi, can you provide the source for that automation please?

Where do i place that? in the configuration.yaml or sensor.yaml

and where do i have to place this?

devices_json: [{"name": "Kök", "cast_type": "audio", "model_name": "Google Home", "uuid": "xxxxx", "manufacturer": "Google Inc."}, {"name": "Högtalare uppe", "cast_type": "group", "model_name": "Google Cast Group", "uuid": "xxxx", "manufacturer": "Google Inc."}, {"name": "Vardagsrum", "cast_type": "cast", "model_name": "HK Citation 300", "uuid": "xxxx", "manufacturer": "Harman Kardon"}]

last_update: 2019-05-01T15:27:49.828553+02:00

friendly_name: Chromecast Devices

The sensors.yaml is a file included by your configuration through the line

sensor: !include sensors.yaml

That means all your sensors go there without the need of the “sensor:” part.
So in your sensors.yaml simply write

- platform: spotcast
devices_json: [{"name": "Kök", "cast_type": "audio", "model_name": "Google Home", "uuid": "xxxxx", "manufacturer": "Google Inc."}, {"name": "Högtalare uppe", "cast_type": "group", "model_name": "Google Cast Group", "uuid": "xxxx", "manufacturer": "Google Inc."}, {"name": "Vardagsrum", "cast_type": "cast", "model_name": "HK Citation 300", "uuid": "xxxx", "manufacturer": "Harman Kardon"}]

last_update: 2019-05-01T15:27:49.828553+02:00

friendly_name: Chromecast Devices

These are just the attributes of the entity sensor.chromecast_devices which gets created through the above sensor definition.

EDIT: I don’t write in forums that often, can someone telle me why the code appears red?

So I have this script which I use to play a gym playlist and turn on a certain light when I say “Gym time” to Google Home and that works fine except when Spotify hasn’t been played recently. Then I get the error that Spotify isn’t active.

Can I use Spotcast to sort out that problem so that I don’t have to have Spotify up and running before executing the script?

Here is how my script looks like now:

spotify_play:
  alias: Spotify Play
  description: Play playlist in Spotify
  sequence:
  - alias: Spotify Play
    service: media_player.play_media
    data:
      entity_id: media_player.spotify_username
      media_content_type: playlist
      media_content_id: spotify:playlist:4knHDJEaM4cueHsD5NEQGQ
  - type: turn_on
    device_id: 20546655988b525735444d869421ffed
    entity_id: light.kitchen_1
    domain: light
    brightness_pct: 60
  - service: tts.google_translate_say
    entity_id: media_player.living_room_speaker
    data:
      name: gym time
  mode: single

Any solution for

but the entity ‘media_player.chromecast_living_room’ remains ‘off’.

Use the spotcast component.

@Emphyrio I’ve installed the spotcast component from HACS. And I’m calling the service via developer tools for testing. Am I using the correct spotcast component? Or can you share some steps to guide me?

Hi @kriss1897, it seems you are asking about the general use of the spotcast component. What are you trying to accomplish exactly? Did you read the general docs? If you can specify your question, that would help.

Okay @Emphyrio . I’m trying to play a playlist from spotify on my Nest Mini. I’ll trigger this automation from an NFC tag. I was successfully able to play the playlist using spotcast.start with proper payload.

The issue I’m facing is that the media is played on the device, but the Lovelace card for the Nest Mini does not show the media playing. (The state for media.player.google_nest_mini is off). Can we do something to update this state?

Does this help?

I actually moved that template to a ‘universal media player’ later on.

Also, for starting a playlist I’m using a script. I can’t remember why I start with 1 second of silence, but I must have had a reason for it when I wrote that :slight_smile:

play_playlist:
  alias: 'Play a playlist'
  sequence:
    - service: spotcast.start
      data:
        device_name: 'Stereo'
        uri: 'spotify:track:5V3b2UB9tCAHuqXj2b2EP7'  # track with 1 second of silence 5V3b2UB9tCAHuqXj2b2EP7
    - service: media_player.turn_on
      entity_id: media_player.livingroom_audio
    - service: spotcast.start
      data:
        device_name: 'Stereo'
        uri: "spotify:playlist:{{ playlist_id }}"
        random_song: 'true'
2 Likes

Just wanted to say thanks for this component! I wasted so many hours reading and trying various things to just play a spotify playlist on my google home group from an automation and nothing seemed to work. This component is fantastic, was easy to set up and haven’t had any issues with it at all. Saved me from so much frustration :+1:

Yes. This worked. I don’t know what I was missing earlier, but now the speaker’s state is updated even when I call spotcast.start without any other calls.
Thanks @Emphyrio

Seem to be having an error today:

Error executing service: <ServiceCall spotcast.start (c:8fa8e6d577bfd1335cb90b75386b1d89): entity_id=media_player.onkyo_bedroom_speaker, uri=spotify:playlist:2ti9VjfEpCoGbpmFBkWiQg, random_song=True, shuffle=False, repeat=off, force_playback=False, start_volume=101, offset=0>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1461, in catch_exceptions
    await coro_or_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1484, in _execute_service
    await self._hass.async_add_executor_job(handler.job.target, service_call)
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/spotcast/__init__.py", line 295, in start_casting
    spotify_cast_device.startSpotifyController(access_token, expires)
  File "/config/custom_components/spotcast/__init__.py", line 458, in startSpotifyController
    sp.launch_app()
  File "/usr/local/lib/python3.8/site-packages/pychromecast/controllers/spotify.py", line 88, in launch_app
    raise LaunchError(
pychromecast.error.LaunchError: Timeout when waiting for status response from Spotify app

Anyone else having this trouble or just me? Any possible solution?