Chromecast Radio with station and player selection

Many thanks for this approach. I have been using this for years, but it no longer works with HA versions after 0.115. Is anyone else having issues?
Problem Solved: Somehow my formatting of the script was jumbled up but still passed the configuration check. I cleaned up the formatting to how I remember I originally created it and the script is now working on 0.117.6.
See https://github.com/home-assistant/core/issues/40466

Your issue has to do with mdp if I read your ticket. So it has nothing to do with the script itself. I think your issue will be solved faster if you post your problems in threads about mdp player.

Ok heres the code.
Input select

radio_station:
  name: 'Kies Radio Station:'
  options:
    - Studio Brussel
    - Q-Music
    - MNM
    - Radio 2
    - Klara
    - Ketnet Radio
  icon: mdi:radio    
spotify_afspeellijst:
  name: 'Kies afspeellijst:'
  options:
    - Top 50 België
    - The Greatest Switch
  icon: mdi:spotify  
chromecast_speakers:
  name: 'Kies Luidspreker:'
  options:
    - Keuken
    - Slaapkamer Ouders
    - Slaapkamer X
    - Slaapkamer Y
    - Overal
  initial: Keuken
  icon: mdi:speaker-wireless   

Script

radio:
  alias: Activeer radio
  sequence:
    - service: media_player.volume_set
      data:
        entity_id: group.cast_player
        volume_level: '0.35'
    -  service: media_player.play_media
       data_template:
         entity_id: >
          {% if is_state("input_select.chromecast_speakers", "Keuken") %} media_player.keuken
          {% elif is_state("input_select.chromecast_speakers", "Slaapkamer Ouders") %} media_player.slaapkamer
          {% elif is_state("input_select.chromecast_speakers", "Slaapkamer X") %} media_player.slaapkamer_X
          {% elif is_state("input_select.chromecast_speakers", "Slaapkamer Y") %} media_player.slaapkamer_Y
          {% elif is_state("input_select.chromecast_speakers", "Overal") %} group.cast_player
          {% endif %}
         media_content_id: >
          {% if is_state("input_select.radio_station", "Studio Brussel") %} http://icecast.vrtcdn.be/stubru-high.mp3
          {% elif is_state("input_select.radio_station", "Q-Music") %} http://icecast-qmusic.cdp.triple-it.nl/Qmusic_be_live_64.aac
          {% elif is_state("input_select.radio_station", "MNM") %} http://icecast.vrtcdn.be/mnm_hits-high.mp3
          {% elif is_state("input_select.radio_station", "Radio 2") %} http://icecast.vrtcdn.be/ra2wvl-high.mp3
          {% elif is_state("input_select.radio_station", "Klara") %} http://icecast.vrtcdn.be/klara-high.mp3
          {% elif is_state("input_select.radio_station", "Ketnet Radio") %} http://icecast.vrtcdn.be/ketnetradio-high.mp3
          {% endif %}
         media_content_type: 'audio/mp4'
spotify:
  alias: Activeer Spotify
  sequence:
    -  service: spotcast.start
       data_template:
         entity_id: >
          {% if is_state("input_select.chromecast_speakers", "Keuken") %} media_player.keuken
          {% elif is_state("input_select.chromecast_speakers", "Slaapkamer Ouders") %} media_player.slaapkamer
          {% elif is_state("input_select.chromecast_speakers", "Slaapkamer X") %} media_player.slaapkamer_X
          {% elif is_state("input_select.chromecast_speakers", "Slaapkamer Y") %} media_player.slaapkamer_Y
          {% elif is_state("input_select.chromecast_speakers", "Overal") %} group.cast_player
          {% endif %}
         uri: >
          {% if is_state("input_select.spotify_playlist", "Top 50 België") %} spotify:playlist:37i9dQZEVXbJNSeeHswcKB
          {% elif is_state("input_select.spotify_playlist", "The Greatest Switch") %} spotify:playlist:3dMgR1GWfuSLJeUUvUTZXV
          {% endif %}
         random_song: 'true'     
         shuffle: 'true'         

Group

cast_player:
  name: Chromecast_player
  entities:
    - media_player.keuken
    - media_player.slaapkamer
    - media_player.slaapkamer_Y
    - media_player.slaapkamer_X

Then the UI

type: custom:vertical-stack-in-card
    title: Luidprekers
    cards:
      - type: entities
        entities:
          - input_select.radio_station
          - input_select.spotify_afspeellijst
          - input_select.chromecast_speakers
      - type: horizontal-stack
        cards:
          - type: custom:button-card
            show_icon: false
            show_label: true
            tap_action:
              action: call-service
              service: script.radio
          - type: custom:button-card
            show_icon: false
            show_label: true
            tap_action:
              action: call-service
              service: script.spotify
      - type: custom:swipe-card
        parameters:
          spaceBetween: 1
        cards:
          - type: conditional
            conditions:
              - entity: media_player.keuken
                state_not: on
            card:
              type: custom:mini-media-player
              entity: media_player.keuken
              artwork: cover
          - type: conditional
            conditions:
              - entity: media_player.slaapkamer
                state_not: on
            card:
              type: custom:mini-media-player
              entity: media_player.slaapkamer
              artwork: cover

You say with radio its normal i dont get art, is it a possibility to use tunein then for radio instead of urls?

1 Like

Regarding radio: there is no TuneIn integration in Home Assistant. And there is no way to cast TuneIn to Chromecast, unless someone creates that component (just like spotcast).

Regarding Spotify:

I still don’t know what your issue is… like I explained, I want to help and you can post code but you still haven’t explained that the issue is. You said:

I click the cast the radio (and/or cast spotify) and then somethings starts playing but i get the idea its not 100% what i ask to do…

I have no idea what it is you ask it to do (or you think you’re doing) and what is does then. So what is the issue?

See my code up

The problem is:

  • radio gets no cover but you explained so thats “ok”
    It takes long too to start playing the mp3 , is that normal?
  • spotify problem: he doenst play my playlist, yesterday i started a playlist… and stopped it, i just tried to start playing the top 50 but the hub shows the playlist from yesterday and doesnt play anything so it doesnt start correct playlist…
  • It takes long too to start playing the mp3 , is that normal?

This depeneds on the stream. Some work instantly, other might take longer to buffer. This is handled by the core Chromecast integration in the end. So if you think it’s a bug, you can create a ticket at HA github → Issues · home-assistant/core · GitHub

  • spotify problem: he doenst play my playlist, yesterday i started a playlist… and stopped it, i just tried to start playing the top 50 but the hub shows the playlist from yesterday and doesnt play anything so it doesnt start correct playlist…

Then your code is not right in the script and/or input_select. All the script does is execute the spotcast service and use the speaker and playlist combination you wrote under input_select and the script.

One thing I do notice, is that you also have group.cast_player as one of he speaker options. I don’t know if that’s supported by spotcast. I created Chromecast group speakers within Google Home app, and then used that as a media_player entity, not group. For example, I have media_player.sony_cast, media_player.badkamer_cast and media_player.multispeakers_cast. The Multispeaker mediaplayer consists of Sony and Badkamer, but that’s handled by Google Home. I don’t group them using Home Assistant.

I can’t really help you with this myself. I just edited the code that this thread provided and used my own Spotify playlist URI’s. And created the UI card and mixed stuff to create something for myself.

If you’re having issues with the spotcast service call, I advise you to ask in that thread - > Spotcast - custom component to start playback on an idle chromecast device

So if you’re issue is: it plays, but not the playlist I asked it to play. Then you need to check your Spotify URI’s and make sure the input_select is correct. I advise to first test the playlist URI codes and make sure it’s the correct one. Use developer tools for that to execture service calls. If it fails you can pinpoint what the issue is, by trying a different Chromecast media player and URI playlist. If still fails, mention it over at the spotcast thread if it’s related to spotcast.

hey ok good tips
thx for the help mate

1 Like

Can you tell me how to check the spotify url using the developer tools to excute the service call?
Thx

Just paste the code inside the service tab. This is located -> https://ur-of-your-ha:8123/developer-tools/service. There just use the service_data (or paste below code in there) and press the button to call the service.

entity_id: media_player.keuken
uri: spotify:playlist:37i9dQZEVXbJNSeeHswcKB

Change the entity_id and uri based on the one you want to test and use different Spotify uri’s to see if it’s the correct one. If you have any errors or if the issue is with spotcast, make sure to visit that thread I linked above so people can help you.

i keep trying but it says : cannot call service, extra keys not allowed

service: media_player.play_media
entity: media_player.keuken
uri: spotify:playlist:37i9dQZEVXbJNSeeHswcKB

That’s because you’re not using the correct service. It’s not play_media service call, but spotcast.start.

You do have spotcast installed right? Please read spotcast documents so you understand how it works.

Sorry my mistake, indeed stupid of me, spotcast start

It works if i do it like that
Also radio (although it takes quite long)

So theres nothing wrong with the urls and stuf

then there should be something wrong with my script or input select… its like the input select doesnt interact with the script

edit: think i found the bug :stuck_out_tongue:
input select for spotify is spotif_.afspeellijst but my script points to spotify_playlist
lol

Sorry for that mate

Glad you finally found the issue! Don’t need to apologize mate. In the end you now know what to do different next time and you learned some more about HA . That’s a win to me :wink: !

1 Like

Ok i get an error (conditions are invalid) when i use state_not : off

- type: custom:swipe-card
            parameters:
              spaceBetween: 1
            cards:
              - type: conditional
                conditions:
                  - entity: media_player.keuken
                    state_not: off
                card:
                  type: custom:mini-media-player
                  entity: media_player.keuken
                  artwork: full-cover
                style: |
                  ha-card {
                    --primary-text-color: var(--sidebar-icon-color)
                  }   

If its unavailable or not playing i want it to show, otherwise not…

Try state_not: ‘off’ So put off inside apostrophes. I’ve read somewhere that on and off needs to be formatted like that when it as state in YAML. That’s what have always done with all states and variables.

I would also add a second line for unavailable. Because that’s a real state (if device is disconnected). If you only use the off then it WILL show when it’s actually unavailable.

                  - entity: media_player.keuken
                    state_not: ‘off’
                  - entity: media_player.keuken
                    state_not: ‘unavailable’

This means it will ONLY show when it’s NOT off OR unavailable. Every other state (idle, playing, paused) will show it.

Works thanks again !

1 Like

Hi,
Newbie here.
First, thanks for sharing project.
It’s really nice.

I try it on my own, it works fine.
But I would like to add minor changes in my UI lovelace outfit which currently looks something like this:

RadioStations

I am using entity card for UI output:

type: entities
entities:
  - entity: input_select.radio_station
  - entity: input_select.radio_player
  - entity: input_number.radio_volume
  - type: call-service
    icon: 'mdi:play'
    name: Play
    action_name: Play
    service: script.radio_play
    service_data:
      entity_id: script.radio_play
  - type: call-service
    icon: 'mdi:stop'
    name: Stop
    action_name: Stop
    service: script.radio_stop
    service_data:
      entity_id: script.radio_stop

I would like to have buttons for start and stop scripts instead of START/STOP string and I don’t know how to do that.
I looked here, but it’s very big topic with several different approaches and didn’t completely understand everything neither found something that works for my case.
I looked at some other topics as well but still didn’t found changes that works for me.
Appreciate if someone could help.

One way you could neaten it up is to add a ‘footer’ to you entities card and have the play and stop buttons in the footer.

1 Like

Thank you very much!
Works fine and looks nice.

Hi Will,

thank you for the code. But can I ask a stupid question?
How can I get this on my lovelace dashboard?
I thank you in advance.
yours,