Error calling script with variables

Hi,

I am calling a script like this:

service: script.speaker_radio_play
data:
  speaker: "woonkamer"

This is the content of the script:

speaker_radio_play:
  alias: Speaker Radio Afspelen
  sequence:
    - service: media_player.play_media
      data_template:
        entity_id: media_player.speaker_{{ speaker }}
        media_content_id: >
         {% if is_state("input_select.radio_zender_{{ speaker }}", "3FM") %} http://icecast.omroep.nl/3fm-bb-mp3
         {% elif is_state("input_select.radio_zender_{{ speaker }}", "Sky Radio") %} http://playerservices.streamtheworld.com/api/livestream-redirect/SKYRADIO.mp3
         {% endif %}
        media_content_type: 'audio/mp4' 

However, this does not work. I receive the following error in the log:

2021-06-02 09:54:59 ERROR (MainThread) [homeassistant.components.script.speaker_radio_play] Speaker Radio Afspelen: Error executing script. Unexpected error for call_service at pos 1: string index out of range
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 366, in _async_step
    await getattr(self, handler)()
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 566, in _async_call_service_step
    await service_task
  File "/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1481, in async_call
    task.result()
  File "/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1516, in _execute_service
    await handler.job.target(service_call)
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/entity_component.py", line 213, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 658, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/entity.py", line 726, in async_request_call
    await coro
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 695, in _handle_entity_call
    await result
  File "/usr/lib/python3.9/site-packages/homeassistant/components/cast/media_player.py", line 459, in async_play_media
    if media_id[0] == "/":
IndexError: string index out of range
2021-06-02 09:54:59 ERROR (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Error executing script. Unexpected error for call_service at pos 1: string index out of range
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 366, in _async_step
    await getattr(self, handler)()
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 569, in _async_call_service_step
    await self._async_run_long_action(service_task)
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 530, in _async_run_long_action
    long_task.result()
  File "/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1481, in async_call
    task.result()
  File "/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1516, in _execute_service
    await handler.job.target(service_call)
  File "/usr/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 278, in service_handler
    await script_entity.async_turn_on(
  File "/usr/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 382, in async_turn_on
    await coro
  File "/usr/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 404, in _async_run
    return await self.script.async_run(variables, context)
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 1212, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 348, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 366, in _async_step
    await getattr(self, handler)()
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 566, in _async_call_service_step
    await service_task
  File "/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1481, in async_call
    task.result()
  File "/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1516, in _execute_service
    await handler.job.target(service_call)
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/entity_component.py", line 213, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 658, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/entity.py", line 726, in async_request_call
    await coro
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 695, in _handle_entity_call
    await result
  File "/usr/lib/python3.9/site-packages/homeassistant/components/cast/media_player.py", line 459, in async_play_media
    if media_id[0] == "/":
IndexError: string index out of range
2021-06-02 09:54:59 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140130171728608] Error handling message: Unknown error
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/homeassistant/components/websocket_api/decorators.py", line 21, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/lib/python3.9/site-packages/homeassistant/components/websocket_api/commands.py", line 482, in handle_execute_script
    await script_obj.async_run(msg.get("variables"), context=context)
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 1212, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 348, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 366, in _async_step
    await getattr(self, handler)()
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 569, in _async_call_service_step
    await self._async_run_long_action(service_task)
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 530, in _async_run_long_action
    long_task.result()
  File "/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1481, in async_call
    task.result()
  File "/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1516, in _execute_service
    await handler.job.target(service_call)
  File "/usr/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 278, in service_handler
    await script_entity.async_turn_on(
  File "/usr/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 382, in async_turn_on
    await coro
  File "/usr/lib/python3.9/site-packages/homeassistant/components/script/__init__.py", line 404, in _async_run
    return await self.script.async_run(variables, context)
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 1212, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 348, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 366, in _async_step
    await getattr(self, handler)()
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 566, in _async_call_service_step
    await service_task
  File "/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1481, in async_call
    task.result()
  File "/usr/lib/python3.9/site-packages/homeassistant/core.py", line 1516, in _execute_service
    await handler.job.target(service_call)
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/entity_component.py", line 213, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 658, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/entity.py", line 726, in async_request_call
    await coro
  File "/usr/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 695, in _handle_entity_call
    await result
  File "/usr/lib/python3.9/site-packages/homeassistant/components/cast/media_player.py", line 459, in async_play_media
    if media_id[0] == "/":
IndexError: string index out of range

Any suggestions wabout what I’m doing wrong?
Thanks!

What about wrapping it quotes?


      entity_id: ‘media_player.speaker_{{ speaker }}’

Thanks for the reply. I have been testing, and I think the entity_id part is not where it fails. I use that in various other scripts like this and that works. Just to be sure, I added the quotes and it still fails.

I think the part where is fails is in the media_content_id part. That part works if I use a fixed entity name:

         {% if is_state("input_select.radio_zender_woonkamer", "3FM") %} http://icecast.omroep.nl/3fm-bb-mp3
         {% elif is_state("input_select.radio_zender_woonkamer", "Sky Radio") %} http://playerservices.streamtheworld.com/api/livestream-redirect/SKYRADIO.mp3
         {% endif %}