Radio Script not excecuting

Hello,

i found this guide on youtube on how to integrate a radio station:

Everything so far showed as working, but the only thing is that the script doesn’t want to execute. When i klick execute it also stops immidiatly. Any idea what could cause it ?


#####
# Radio player - script to start playing radio with selected values.
# 
#####

radio_play:
  alias: Play Radio on Chromecast Audio
  sequence:
  - service: media_player.volume_set
    data:
      entity_id: >
        {% if is_state("input_select.radio_speaker", "Bedroom display") %} media_player.bedroom_display
        {% endif %}
      volume_level: '{{  states("input_number.radio_volume")  }}'
  - service: media_player.play_media
    data:
      entity_id: >
        {% if is_state("input_select.radio_speaker", "Bedroom display") %} media_player.bedroom_display
        {% endif %}
      media_content_id: >
        {% if is_state("input_select.radio_station", "NPO 2") %} https://icecast.omroep.nl/radio2-bb-mp3 
        {% elif is_state("input_select.radio_station", "Qmusic") %} https://icecast-qmusicnl-cdp.triple-it.nl/Qmusic_nl_live_96.mp3
        {% elif is_state("input_select.radio_station", "Slam!") %} https://22533.live.streamtheworld.com/SLAM_MP3_SC?
        {% elif is_state("input_select.radio_station", "100% NL") %} https://20853.live.streamtheworld.com/100PNL_MP3_SC?
        {% endif %}
      media_content_type: 'music'

Does it generate an error in configuration / logs ?

it shows:

Play Radio on Chromecast Audio: Error executing script. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']

and

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1026, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 242, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 250, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 457, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1399, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 218, in __call__
    return self._exec((Schema(val) for val in self.validators), v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 341, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 337, in _exec
    v = func(v)
  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/validators.py", line 215, in _run
    return self._exec(self._compiled, value, path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 341, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 339, in _exec
    v = func(path, v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(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: not a valid value for dictionary value @ data['entity_id']

You don’t have an else in your if statements for the entity_id. What entity_id do you want to select if the state of the input_select is NOT “Bedroom display” ?

Did you create this input select?

And is it set to: Bedroom display ?

As you only have one option, you could change this:

  - service: media_player.volume_set
    data:
      entity_id: >
        {% if is_state("input_select.radio_speaker", "Bedroom display") %} media_player.bedroom_display
        {% endif %}

To:

  - service: media_player.volume_set
    data:
      entity_id: media_player.bedroom_display

i added a 2e speaker

radio_play:
  alias: Play Radio on Chromecast Audio
  sequence:
  - service: media_player.volume_set
    data:
      entity_id: >
        {% if is_state("input_select.radio_speaker", "Bedroom display") %} media_player.bedroom_display
        {% elif is_state("input_select.radio_speaker", "Home Group") %} media_player.home_group
        {% endif %}
      volume_level: '{{  states("input_number.radio_volume") }}'
  - service: media_player.play_media
    data:
      entity_id: >
        {% if is_state("input_select.radio_speaker", "Bedroom display") %} media_player.bedroom_display
        {% elif is_state("input_select.radio_speaker", "Home Group") %} media_player.home_group
        {% endif %}
      media_content_id: >
        {% if is_state("input_select.radio_station", "NPO 2") %} https://icecast.omroep.nl/radio2-bb-mp3 
        {% elif is_state("input_select.radio_station", "Qmusic") %} https://icecast-qmusicnl-cdp.triple-it.nl/Qmusic_nl_live_96.mp3
        {% elif is_state("input_select.radio_station", "Slam!") %} https://22533.live.streamtheworld.com/SLAM_MP3_SC?
        {% elif is_state("input_select.radio_station", "100% NL") %} https://20853.live.streamtheworld.com/100PNL_MP3_SC?
        {% endif %}
      media_content_type: 'music'

i made a file input_select.yaml with

# Radio Stations

radio_station:
  name: 'Select Radio Station'
  options:
    - 'Radio 538'
    - 'Npo 2'
    - 'Radio Veronica'
    - 'Slam!'
    - '100% NL'
    - 'QMusic'
  initial: Npo 2
  icon: mdi:radio
  
radio_speaker:
  name: 'Select Speaker'
  options:
    - 'Home Group'
    - 'Bedroom Display'
  initial: 'Home Group'
  icon: mdi:speaker-wireless

Have you reloaded your scripts?

Does it work now?

If not, what error are you seeing?

it does not the 2 logs are

Logger: homeassistant
Source: components/cast/media_player.py:491
First occurred: 15:17:18 (1 occurrences)
Last logged: 15:17:18

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1026, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 242, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 250, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 457, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1445, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1480, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 593, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 664, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 630, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/cast/media_player.py", line 491, in async_play_media
    if media_id[0] == "/":
IndexError: string index out of range
Logger: homeassistant.components.script.radio_play
Source: components/cast/media_player.py:491
Integration: Script (documentation, issues)
First occurred: 15:17:18 (1 occurrences)
Last logged: 15:17:18

Play Radio on Chromecast Audio: Error executing script. Unexpected error for call_service at pos 2: string index out of range
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 250, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 457, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1445, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1480, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 593, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 664, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 630, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/cast/media_player.py", line 491, in async_play_media
    if media_id[0] == "/":
IndexError: string index out of range

Ok, you fixed the first problem with selecting an entity id to adjust the volume. Now you have a problem selecting the source to play.

Your if statements for this miss two options your input_select radio_station could be set to:

    - 'Radio 538'  # not in your if/else list.
    - 'Npo 2' # not the same as 'NPO 2'.

So:

      media_content_id: >
        {% if is_state("input_select.radio_station", "Npo 2") %} https://icecast.omroep.nl/radio2-bb-mp3 
        {% elif is_state("input_select.radio_station", "Radio 538") %} YOU NEED A URL HERE 
        {% elif is_state("input_select.radio_station", "Qmusic") %} https://icecast-qmusicnl-cdp.triple-it.nl/Qmusic_nl_live_96.mp3
        {% elif is_state("input_select.radio_station", "Slam!") %} https://22533.live.streamtheworld.com/SLAM_MP3_SC?
        {% elif is_state("input_select.radio_station", "100% NL") %} https://20853.live.streamtheworld.com/100PNL_MP3_SC?
        {% endif %}
      media_content_type: 'music'
2 Likes

ok, i removed the 538 one, and renamed the Npo 2,

Logger: homeassistant
Source: components/cast/media_player.py:491
First occurred: 15:37:38 (1 occurrences)
Last logged: 15:37:38

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1026, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 242, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 250, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 457, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1445, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1480, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 593, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 664, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 630, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/cast/media_player.py", line 491, in async_play_media
    if media_id[0] == "/":
IndexError: string index out of range
Logger: homeassistant.components.script.radio_play
Source: components/cast/media_player.py:491
Integration: Script (documentation, issues)
First occurred: 15:37:38 (1 occurrences)
Last logged: 15:37:38

Play Radio on Chromecast Audio: Error executing script. Unexpected error for call_service at pos 2: string index out of range
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 250, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 457, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1445, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1480, in _execute_service
    await handler.job.target(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 593, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 664, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 630, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/cast/media_player.py", line 491, in async_play_media
    if media_id[0] == "/":
IndexError: string index out of range

i DID hear and see it make a connection with the display but there was no sound and the screen was black

Can you show what your script looks like now please?

ofcourse:

#####
# Radio player - script to start playing radio with selected values.
# 
#####

radio_play:
  alias: Play Radio on Chromecast Audio
  sequence:
  - service: media_player.volume_set
    data:
      entity_id: >
        {% if is_state("input_select.radio_speaker", "Bedroom display") %} media_player.bedroom_display
        {% elif is_state("input_select.radio_speaker", "Home Group") %} media_player.home_group
        {% endif %}
      volume_level: '{{  states("input_number.radio_volume") }}'
  - service: media_player.play_media
    data:
      entity_id: >
        {% if is_state("input_select.radio_speaker", "Bedroom display") %} media_player.bedroom_display
        {% elif is_state("input_select.radio_speaker", "Home Group") %} media_player.home_group
        {% endif %}
      media_content_id: >
        {% if is_state("input_select.radio_station", "Npo 2") %} https://icecast.omroep.nl/radio2-bb-mp3 
        {% elif is_state("input_select.radio_station", "Qmusic") %} https://icecast-qmusicnl-cdp.triple-it.nl/Qmusic_nl_live_96.mp3
        {% elif is_state("input_select.radio_station", "Slam!") %} https://22533.live.streamtheworld.com/SLAM_MP3_SC?
        {% elif is_state("input_select.radio_station", "100% NL") %} https://20853.live.streamtheworld.com/100PNL_MP3_SC?
        {% endif %}
      media_content_type: 'music'

# Radio Stations

radio_station:
  name: 'Select Radio Station'
  options:
    - 'Npo 2'
    - 'Radio Veronica'
    - 'Slam!'
    - '100% NL'
    - 'QMusic'
  initial: Npo 2
  icon: mdi:radio
  
radio_speaker:
  name: 'Select Speaker'
  options:
    - 'Home Group'
    - 'Bedroom display'
  initial: 'Home Group'
  icon: mdi:speaker-wireless

I have tried the Home Group speaker and it Works! just the bedroom display doesnt.

You are missing an if/else for this radio station option too:

ah yes, i removed that one as well as the links were not correct.
The bedroom display not giving any audio is beyond me. It is streaming music but no sound comes out.
I dont get any error logs anymore so it should in theory be working.

Thank you so much Tom for helping me out with this. Much appreciated!

1 Like

Have you tried turning the bedroom media player off and on again?

Fully, from the wall switch, not the equipment power button.

i just tried it but to no avail, it says streaming default media receiver.
you can see a time being played but just no sound.

i also switched radio channels to make sure

I have a very similar issue, did you ever fix this Ryukenden?

Never mind, I found a spelling mistake in one of my radio station names and after changing that it works now :slight_smile: