Spotcast - custom component to start playback on an idle chromecast device

I never gpt scripts to work for anything in HA. If someone with knowledge ca help you it would be good seeing as it works outside a scripts.

Also I think this question with answers are posted in this thread a couple of times already

Perhaps I’m mistaken, but your playlist ‘ Solo Bocinas Inteligentes’ is missing the URI link. Basically you’re saying to play a playlist, without providing the link right?

I have a similar setup where I used BobNL’s Radio card and made a Spotify card (where I can select playlist and speakers).

My script works flawlessly: Chromecast Radio with station and player selection

1 Like

Yeah, I thought about that. But it wasn’t it. I removed that section of the elif and just left a single value (which when executed on the development tools template editor did return a valid spotify URI and it would still fail.

I did remove the random_song: true parameter from my script and now it does play the playlist on the selected speaker as expected, but it will still complain on the homeassistant.log about the AttributeError: ‘Spotify’ object has no attribute ‘playlist’ dunno what is that about, I believe that is inside the spotify component (/usr/src/homeassistant/homeassistant/components/spotify/media_player.py)

I noticed in your script you used random_song: ‘true’ and shuffle: ‘true’ with single quotes, maybe I was missing that.

@fondberg, can you please explain how to use the new websocket methods from home assistant

I’m no HASS scripting expert and have had very limited success with my scripts for other things. I can therefor not help. sorry

Hi,
This is my working script for Spotcast, I hope it will get you started:

spotify_music:
  sequence:
  - data_template:
      entity_id: '{% if is_state("input_select.spotify_source", "All Speakers") %}
        media_player.all_speakers {% elif is_state("input_select.spotify_source",
        "Kitchen") %} media_player.kitchen {% elif is_state("input_select.spotify_source",
        "Orins Room") %} media_player.orins_room {% elif is_state("input_select.spotify_source",
        "Living Room") %} media_player.living_room {% elif is_state("input_select.spotify_source",
        "Master Bedroom") %} media_player.master_bedroom {% elif is_state("input_select.spotify_source",
        "Lians Room") %} media_player.lians_room {% elif is_state("input_select.spotify_source",
        "Google Outdoor") %} media_player.google_outdoor {% elif is_state("input_select.spotify_source",
        "Bathroom") %} media_player.bathroom {% endif%}

        '
      random_song: true
      uri: '{% if is_state("input_select.spotify_playlist", "Summer Hits") %} spotify:user:spotify:playlist:37i9dQZF1DWZKxDdpAkEjR
        {% elif is_state("input_select.spotify_playlist", "80s Dance") %} spotify:playlist:2rdqJc9UxEbt5Ff8qrS5pB
        {% elif is_state("input_select.spotify_playlist", "Bob Marley") %} spotify:playlist:0NnnXJtgjnzkpdT9BNRlY3
        {% elif is_state("input_select.spotify_playlist", "Julio Iglesias") %} spotify:playlist:0gpDsLoGK0ylw3ColHhx5i
        {% elif is_state("input_select.spotify_playlist", "Piano in the Background")%}
        spotify:user:spotify:playlist:37i9dQZF1DX7K31D69s4M1 {% elif is_state("input_select.spotify_playlist",
        "Happy Hits") %} spotify:user:spotify:playlist:37i9dQZF1DXdPec7aLTmlC {% endif
        %}

        '
    service: spotcast.start
  alias: Play Music

You can see how it looks like in my github:
https://github.com/tomerbs/home-assistant-config/blob/745e834fdfa81428b63c1380fae82655ff6df7a9/README.md

3 Likes

Those APIs are only exposed to code like lovelace cards, python scripts or custom components

What is your use case?

Nice!! Def want my HA to look like that. I haven’t fixed a nice UI even after using HA for 1.5 years

Nice! I have that as well (I used Bob_NL’s Radio as an example for the codes), but I combined it with multiple conditional mini media player cards inside a vertical stack-in-card (depending on what speaker is on) and use it as a popup card for Spotify on my Lovelace view :grinning::+1: (Also wrapped all the mini cards inside a swipe card so I can swipe media players in case different sources are on). Pressing the Spotify button (added a button-card inside the vertical stack-in-card between the dropdown menu and mini media player cards) starts the script.

4 Likes

Hey, are you able to share your final config for this, looks awesome and would love to use it myself!

I really like it, do you maybe have the popup card code yaml also? I can’t get the input selectors in a nice popup card style like yours… Thanks a lot @tomerbs!

This has been thought out really nice. Are you willing to share the gui-yaml code of the popup?

Thanks a lot!

@martheijnen and @mitch Hi, thank you! It’s impossibe for me to share that for the time being. I have split my configuration in to multiple yaml files (like more than 40 files). I don’t have the time to search them all an put them in a file or to share my entire setup. I can share the code of what you see there, but you’ll be missing a lot of info and files. It’ll give you an idea how I created that and should help you on your way though!

    card:
      layout: vertical
      type: 'custom:layout-card'
      column_width: 96%
      cards:
        - content: |
            # Maak je keuzes en tik op het Spotify icoontje.
          style: !include /config/templates/smartphone/markdown-sub-style.yaml
          type: markdown      
        - cards:    
            - entities:
                - entity: input_select.spotify_playlist
                - entity: input_select.google_cast
              type: entities
              style: |
                ha-card {
                  --paper-card-background-color: var(--background-color) !important;
                  box-shadow: none !important;
                }            
            - deep_press: true
              entity: media_player.sony_cast
              hold_action:
                action: more-info
                haptic: heavy
              icon: 'mdi:spotify'
              label: Spotify
              name: Tik hier om te starten!
              aspect_ratio: 4/1
              size: 100%
              show_icon: true
              show_label: false
              show_last_changed: false
              show_name: false
              show_state: false
              state:
              styles:
                card:
                  - font-size: 18px
                  - background-color: var(--background-color)
                name:
                  - font-weight: bold                  
                  - font-family: Helvetica                
                label:
                  - font-size: 10px      
                  - filter: opacity(0%)
              tap_action:
                action: call-service
                haptic: medium
                service: script.turn_on
                service_data:
                  entity_id: script.spotify
              type: 'custom:button-card'                
            - !include /config/templates/mediaplayers/card_sony_cast.yaml
          mode: vertical
          keep:
            background: true
            border_radius: false
            margin: false        
            outer_padding: false
          type: 'custom:stack-in-card'
              
    deviceID:
      - this                      
    style:
      align-items: center
      background: var(--background-popcard)
      icon: 'mdi:apple'
      justify-content: center
      width: 400px
      box-shadow: none
    title: Spotify

And if you have multiple media players you want to swipe between just wrap those cards inside a swiper-card (install the plugin first through HACS). I removed that from the code above, because it would be confusing (since I also deleted other media players to showcase the code more clearly)

- cards:
    - INSERT CARDS HERE
    - INSERT CARDS HERE
    - INSERT CARDS HERE
  parameters:
    spaceBetween: 20 
    pagination:
      type: bullets
  type: 'custom:swipe-card'    

Make sure to view the post of Rob_NL I mentioned above to have the menu/script actually functional. I created my own script using his example: Chromecast Radio with station and player selection

1 Like

Thanks, I managed to get the script working but now want the simple entity card i have in my ‘homekit style’ layout. It’s not that easy to get that done :’)

thanks, hope to can get something of your code working

Ah ok, yeah I don’t use the Homekit Syle card (you mean the one from DBuit right?)

Yes! correct. With my own additions onto it ofc

Yes, I’m not sure how that one works. I like his work, but don’t want to be dependable on someone elses work to create my interface. That’s why I decided to not use his and create my own interface. I do use his popup sliders though. They work pretty good, though I want to create my own version later on.

1 Like

Wish I had the same experience in yaml as you have. Did you share your config somewhere to just dig in and learn :slight_smile: ?

I started using Home Assistant since last december (so just over 7 months ago). I switched from Homey (smart home platform) after using that for around 1,5 years. I started using YAML mode like 3-4 months ago. I am by no means a programmer (I studied marketing and am working at communications/marketing). I just try a lot and figure out as I go.

I don’t have my config published right now. I might do that down the road, but I’d need to clean a lot up (have a lot of personal info and comments everywhere to help me code). Right now I don’t have the time to publish that unfortunately. There are a lot of other people who have their complete UI’s programmable, like Dwaine and Jimmy.

1 Like

Hey, I’ve started using spotcast and think its excellent! I’ve been able to set up some easy one click buttons in HA that are great for my family to use to get music into the room they’re in!

However, I’m noticing a small problem in that it randomly stops playing back after approx ~30minutes. One click of the button gets it going again, but as a result we end up hearing a lot of the same tracks even with the “random” songs set to true.

Just wondering if I have done something incorrectly?

I have this configured on a template switch.

      spotcast_everywhere:
        friendly_name: "Everywhere Music"
        value_template: "{{ is_state('media_player.everywhere', 'playing')}}"
        turn_on:
          - service: spotcast.start
            data:
              device_name: Everywhere
              random_song: true
              uri: 'spotify:playlist:xxx'
        turn_off:
          - service: media_player.turn_off
            entity_id: media_player.everywhere

Thanks in advance for any assistance.