A little help with random playlist script

Hi
I have the following script which is working ok.

play_music:
  alias: 'Play Music'
  sequence:
    - service: remote.send_command
      data:
        entity_id: remote.living_room_ac_remote
        command: b64:"JgBmAJOVExMRFBE5EzcTEhI4ExMRFBEUERQSOBM3ExMRORITERQSFBE4EzcTOBI4EhMRFBITEjgTExEUERQSExI4EzcTNxMABfSWkhI4EgAMU5aSEzcTAAxTlpESORIADFOXkRM3EgANBQAA"
   
    - delay: '00:00:08'
    - service: spotcast.start
      data:
        entity_id: media_player.bose
        uri: 'spotify:playlist:37i9dQZF1DXa1BeMIGX5Du'

I would like to choose randomly the playlist from 3 selections. I tried the following but it is not working.
Which is the correct syntax?

play_music:
  alias: 'Play Music'
  sequence:
    - service: remote.send_command
      data:
        entity_id: remote.living_room_ac_remote
        command: b64:"JgBmAJOVExMRFBE5EzcTEhI4ExMRFBEUERQSOBM3ExMRORITERQSFBE4EzcTOBI4EhMRFBITEjgTExEUERQSExI4EzcTNxMABfSWkhI4EgAMU5aSEzcTAAxTlpESORIADFOXkRM3EgANBQAA"
   
    - delay: '00:00:08'
    - service: spotcast.start
      data:
        entity_id: media_player.bose
        data_template: >
            {{ ["uri: 'spotify:playlist:37i9dQZF1DXa1BeMIGX5Du'", "uri: 'spotify:playlist:37i9dQZF1DWZeKCadgRdKQ'", "uri: 'spotify:playlist:37i9dQZF1DX3rxVfibe1L0'"] | random}}

Don’t template the key of the key: value pair, just the value.

    - service: spotcast.start
      data:
        entity_id: media_player.bose
        data_template:
          uri: >
            {{ ['spotify:playlist:37i9dQZF1DXa1BeMIGX5Du', 'spotify:playlist:37i9dQZF1DWZeKCadgRdKQ', 'spotify:playlist:37i9dQZF1DX3rxVfibe1L0'] | random}}

Also if you are using version 0.115 or later, data_template: can be just data:

Thanks, I see the problem. However I just tried it and it didn’t worked. I got the following error.

Logger: homeassistant
Source: core.py:1289
First occurred: 12:17:03 AM (3 occurrences)
Last logged: 8:59:37 AM

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 985, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 239, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 247, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 454, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1289, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: extra keys not allowed @ data['data_template']

I am on 0.116 but I don’t think that the problem now is tha data template instead of data

I just assumed you had the correct service call setup. You don’t:

Looking at the documentation it should be:

    - service: spotcast.start
      data:
        entity_id: media_player.bose
        uri: >
            {{ ['spotify:playlist:37i9dQZF1DXa1BeMIGX5Du', 'spotify:playlist:37i9dQZF1DWZeKCadgRdKQ', 'spotify:playlist:37i9dQZF1DX3rxVfibe1L0'] | random}}

It is not working, but nevermind. This is not mandatory
I will keep one playlist and move on.

Log Details (ERROR)
Logger: homeassistant.core
Source: custom_components/spotcast/__init__.py:452
First occurred: 1:48:05 PM (5 occurrences)
Last logged: 1:55:42 PM

Error executing service: <ServiceCall spotcast.start (c:340a5126106611eb9d6a67184dd42624): entity_id=media_player.bose, uri=spotify:playlist:37i9dQZF1DWZeKCadgRdKQ, random_song=False, force_playback=False, repeat=off, offset=0, shuffle=False>
Error executing service: <ServiceCall spotcast.start (c:c73b33fc106611eba504c1c4ba6b0002): entity_id=media_player.bose, uri=['spotify:playlist:37i9dQZF1DXa1BeMIGX5Du'], random_song=False, force_playback=False, repeat=off, offset=0, shuffle=False>
Error executing service: <ServiceCall spotcast.start (c:f2ac8f16106611ebb360d574b9916720): entity_id=media_player.bose, uri=spotify:playlist:37i9dQZF1DXa1BeMIGX5Du, random_song=False, force_playback=False, repeat=off, offset=0, shuffle=False>
Error executing service: <ServiceCall spotcast.start (c:207b6c1c106711ebaec375656d9fabbd): entity_id=media_player.bose, uri=spotify:playlist:37i9dQZF1DXa1BeMIGX5Du, random_song=False, force_playback=False, repeat=off, offset=0, shuffle=False>
Error executing service: <ServiceCall spotcast.start (c:41bebcaa106711eb94bf3b0a6ab9091a): entity_id=media_player.bose, uri=spotify:playlist:37i9dQZF1DXa1BeMIGX5Du, random_song=False, force_playback=False, repeat=off, offset=0, shuffle=False>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1351, in catch_exceptions
    await coro_or_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1374, in _execute_service
    await self._hass.async_add_executor_job(handler.func, 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 302, in start_casting
    spotify_cast_device.startSpotifyController(access_token, expires)
  File "/config/custom_components/spotcast/__init__.py", line 452, 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 ap

.

1 Like

Give it one last try:

    - service: spotcast.start
      entity_id: media_player.bose
      data:
        uri: >
          {{ ['spotify:playlist:37i9dQZF1DXa1BeMIGX5Du', 'spotify:playlist:37i9dQZF1DWZeKCadgRdKQ', 'spotify:playlist:37i9dQZF1DX3rxVfibe1L0'] | random}}

Some integrations can be picky about placing the entity_id in the data key.

4 Likes

@tom_l thanks for the help here. Although the OP didn’t reply any further, I tried your solution and it works perfectly fine.

So thanks for the effort.

1 Like