Multi Zone Chromecast Web Radio

I am fairly new with all of this and I have come to a roadblock with my limited experience.

The idea is to have two input_selects, one to pick the location of the chromecast and one to pick a radio station to stream

My input-selects looks like this

radio_station:
  name: Radio Station
  options:
    - "-"
    - Hot108
    - The Beat
  initial: "-"
  icon: mdi:radio

radio_station_location:
  name: Radio Location
  options:
    - Kitchen
    - Backyard
  initial: "Kitchen"
  icon: mdi:radio

and my radio automation looks like this:

alias: Radio
trigger:
  platform: state
  entity_id: input_select.radio_station
action:
  - service: script.turn_on
    data_template:
      entity_id:
        - service: media_player.play_media
          data_template:
            entity_id: >
              {% if input_select.radio_station_location == 'Kitchen' %}
                media_player.kitchen
              {%-elif input_select.radio_station_location == 'Backyard' %}
                media_player.backyard
              {% endif %}
            media_content_id: >
              {% if input_select.radio_station == 'Hot108' %}
                "http://jbmedia-edge1.cdnstream.com:80/hot108"
              {%-elif input_select.radio_station == 'The Beat' %}
                "http://live.leanstream.co/CKBTFM-MP3"
              {% endif %}
            media_content_type: 'audio/mp3'
  - service: input_select.select_option
    data:
      entity_id: input_select.radio_station
      option: "-"

and when I attempt to actually use it I see a large error:

17-02-26 11:08:27 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 99, in async_render
    return self._compiled.render(kwargs).strip()
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/jinja2/sandbox.py", line 385, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'input_select' is undefined

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
        result = next(coro)
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 294, in async_trigger
        yield from self._async_action(self.entity_id, variables)
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/automation/__init__.py", line 376, in action
        yield from script_obj.async_run(variables)
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/script.py", line 117, in async_run
        yield from self._async_call_service(action, variables)
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/script.py", line 147, in _async_call_service
        self.hass, action, True, variables, validate_config=False)
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 88, in async_call_from_config
        config[CONF_SERVICE_DATA_TEMPLATE]))
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in _data_template_creator
        for key, item in value.items()}
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in <dictcomp>
        for key, item in value.items()}
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 81, in _data_template_creator
        return [_data_template_creator(item) for item in value]
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 81, in <listcomp>
        return [_data_template_creator(item) for item in value]
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in _data_template_creator
        for key, item in value.items()}
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in <dictcomp>
        for key, item in value.items()}
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in _data_template_creator
        for key, item in value.items()}
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 84, in <dictcomp>
        for key, item in value.items()}
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/service.py", line 86, in _data_template_creator
        return value.async_render(variables)
      File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/template.py", line 101, in async_render
        raise TemplateError(err)
    homeassistant.exceptions.TemplateError: UndefinedError: 'input_select' is undefined

I have hit a complete road block as to what the next step is.

Use:
{% if is_state('input_select.radio_station_location', 'Kitchen') %}

Edit:
Go to your_ip:8123/dev-template to validate and debug a template

1 Like

This is my script (which works well):

  radio538:
    alias: Play Radio on Chromecast Audio
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.ca_hall
          volume_level: '0.35'
      -  service: media_player.volume_set
         data:
           entity_id: media_player.ca_bathroom
           volume_level: '0.20'
      -  service: media_player.volume_set
         data:
           entity_id: media_player.ca_livingroom
           volume_level: '0.30'
      -  service: media_player.play_media
         data_template:
           entity_id: media_player.home_group
           media_content_id: >
            {% if is_state("input_select.radio_station", "Radio 538") %} http://vip-icecast.538.lw.triple-it.nl:80/RADIO538_MP3
            {% elif is_state("input_select.radio_station", "Q-Music") %} http://icecast-qmusic.cdp.triple-it.nl/Qmusic_nl_live_96.mp3
            {% elif is_state("input_select.radio_station", "3FM") %} http://icecast.omroep.nl/3fm-bb-mp3
            {% elif is_state("input_select.radio_station", "100% NL") %} http://stream.100p.nl/100pctnl.mp3
            {% elif is_state("input_select.radio_station", "Veronica") %} http://8543.live.streamtheworld.com/VERONICACMP3
            {% elif is_state("input_select.radio_station", "Sky Radio") %} http://8623.live.streamtheworld.com:80/SKYRADIOAAC_SC
            {% elif is_state("input_select.radio_station", "Arrow Classic Rock") %} http://91.221.151.155/listen.mp3
            {% elif is_state("input_select.radio_station", "Classic FM") %} http://19143.live.streamtheworld.com/CLASSICFM_SC
            {% elif is_state("input_select.radio_station", "BNR Nieuwsradio") %} http://icecast-bnr.cdp.triple-it.nl/bnr_mp3_96_03
            {% elif is_state("input_select.radio_station", "SLAM! Hardstyle") %} http://vip-icecast.538.lw.triple-it.nl/WEB17_MP3
            {% elif is_state("input_select.radio_station", "Sleep Radio") %} http://37.59.28.208:8722/stream
            {% elif is_state("input_select.radio_station", "Ambient Sleeping Pill") %} http://perseus.shoutca.st:8447/h
            {% elif is_state("input_select.radio_station", "Radio Art - Sleep") %} http://live.radioart.com/fSleep.mp3
            {% elif is_state("input_select.radio_station", "Ambi Nature Radio") %} http://94.23.252.14:8067/stream
            {% elif is_state("input_select.radio_station", "Calm Radio - Sleep") %} http://streams.calmradio.com/api/39/128/stream
            {% elif is_state("input_select.radio_station", "Dinamo.FM Sleep") %} http://channels.dinamo.fm/sleep-aac
            {% endif %}
           media_content_type: 'audio/mp4' 

Like Daniel said, your “IF/ELSE” statement is implemented wrong.

3 Likes

Thank you for the help everyone. I have made a bunch of changes and I have had some good luck. Switching the location and picking a radio station plays the first selection “hot108” no matter what radio station I pick. Both locations are like this.

trigger:
  platform: state
  entity_id: input_select.radio_station
action:
  - service: media_player.play_media
    data_template:
      entity_id: >
        {% if is_state('input_select.radio_station_location', 'Kitchen') %}
          media_player.kitchen
        {% elif is_state('input_select.radio_station_location', 'Backyard') %}
          media_player.backyard
        {% endif %}
      media_content_id: >
        {% if 'input_select.radio_station', 'Hot108' %}
          http://jbmedia-edge1.cdnstream.com:80/hot108
        {% elif 'input_select.radio_station', 'The Beat' %}
          http://live.leanstream.co/CKBTFM-MP3
        {% endif %}
      media_content_type: 'audio/mp3'

I am soooo close haha.

1 Like

Does it help if you use " instead of ’ ?

Update: looks like you didn’t change your if/else syntax on the media_content_id part (is_state):wink:

1 Like

You guys are beautiful.

alias: Radio
trigger:
  platform: state
  entity_id: input_select.radio_station
action:
  - service: media_player.play_media
    data_template:
      entity_id: >
        {% if is_state('input_select.radio_station_location', 'Kitchen') %}
          media_player.kitchen
        {% elif is_state('input_select.radio_station_location', 'Backyard') %}
          media_player.backyard
        {% endif %}
      media_content_id: >
        {% if is_state('input_select.radio_station', 'Hot108') %}
          http://jbmedia-edge1.cdnstream.com:80/hot108
        {% elif is_state('input_select.radio_station', 'The Beat') %}
          http://live.leanstream.co/CKBTFM-MP3
        {% endif %}
      media_content_type: 'audio/mp3'

This works fantastic.

3 Likes

You should do a full write up of your configs in the Share Your Projects section. I’m sure people will love it; I know I would.

I’m going to give something like this a try in the near future, just from seeing it. I never thought about doing the input switch in the same framework as the streaming source. Really makes sense.

@rpitera here you go: Chromecast Radio with station and player selection

2 Likes

I cant seem to get the radio working on my chromecast A, is there something special I need to do?
(Im thinking that my rasp is on wired network, thats causing the problem?)

My pi is also wired and I have had no issues in that regard. I have auto discover turned off but made sure I set a static IP in my router to each Chromecast audio. Then just added the typical lines to my config

platform: cast
host: 192.168.X.X

I made my chrome static, and it seem to work, thanks, great radio :smiley: