Using your Spotify account in a HA dashboard to select speakers and broadcast

Using your Spotify account in a HA dashboard to select speakers and broadcast anything in your Spotify account devices {and Alexa if you linked your Spotify Account in your Alexa app} in Home assistant. This works best with a paid Spotify account but I think works a free account.


Spotify dashboard

First you need Spotify integration set up in HA and Spot cast from HACS and optional spotify card.
Spotify Spotify - Home Assistant

Spot cast GitHub - fondberg/spotcast: Home assistant custom component to start Spotify playback on an idle chromecast device as well as control spotify connect devices

Spotify-card GitHub - custom-cards/spotify-card: Spotify playlist card for Home Assistant card

There are many ways of doing this by getting the URL of the playlist or songs in your Spotify account and doing a bunch of fumbling around. But I found this is a much more simple way by using the HA guys awesome media-browser dashboard that you can choose from Spotify then anything you want like songs, categories or playlist from your Spotify account linked to HA.

Create a speaker Drop down list {Speakers}
Make a drop down list to be used in Device selected automation.
with Helpers or a input select by hand In your input select: configuration.yaml section or file.
Drop down list using Helpers.


speakers HELPER screenshot

Input select yaml by hand.

input_select:
   speakers:
     name: speakers
     options:
       - "Family room"
       - "Living Room speaker"
       - "Living Room TV"
       - "Kitchen display"
       - "Bedroom speaker"
     icon: mdi:cast-audio

Make sure you use the names exactly {case sensitive} of Chromecast, Alexa or other devices or speaker groups in your SPOTIFY Account or Google home account not the name you gave in HA.
You can get this list in your Spotify device list.
Or looking at custom:spotify-card speaker list spot cast HACS add on.


spotify device list screen shot


spotcast card

Create a media selection (music playlist) List drop down {Music Playlist }
Make a drop down list to be used in Music Playlist automation.
with Helpers or a input select by hand In yaml.
Use any name you would like for each Media selection for it will be used in the automation to select Media from Spotify in the automation.

My media selection Drop down list using Helpers


music playlist screen shot

Create a script to activate spot cast when speakers are selected using YAML or HA UI in automations & Scenes tab.
This script has my options of setting volume to 30 and shuffle you can change or remove.


start spotcast script screenshot

start spotcast script

service: spotcast.start
data:
  device_name: "{{ states('input_select.speakers') }}"
  force_playback: true
  start_volume: 30 
  shuffle: true 

Device Selected automation…

alias: "Spotify: Device Selected2"
description: Starts music playing on the selected Chromecast Audio device
trigger:
  - platform: state
    entity_id: input_select.speakers
condition: []
action:
  - service: script.turn_on
    data: {}
    target:
      entity_id:
        - script.spotify_start_playback
mode: single


device selected automation screenshot

Play list automation
Linking your Music Playlist Drop down list to your Spotify Media selection.
The target action media player is your Spotify HA account media player. Using the media-browser the HA staff created.
( entity_id: media_player.spotify_smarthome1)


spotify intergration SCREEN SHOT.


media browser screenshot

Music playlist Automation
The first automation below works great. But if Spotify is not already started or active on a device and you select music playlist you need to click the RUN next to Spotify:Start playback after selecting.


start spotify screenshot

Place this code in a UI editor automation editor
Or in your own automation.yaml.


UI playlist automation screenshot

yaml

alias: "Music Playlist "
description: ""
trigger:
  - platform: state
    entity_id:
      - input_select.playlist
    to: "Rock out "
    id: "Rock "
  - platform: state
    entity_id:
      - input_select.playlist
    to: "Cleaning "
    id: Cleaning
  - platform: state
    entity_id:
      - input_select.playlist
    to: Relax
    id: "Relax "
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "Rock "
        sequence:
          - service: media_player.play_media
            target:
              entity_id: media_player.spotify_smarthome1
            data:
              media_content_id: spotify:playlist:2gvmi685aThtmb1cirWNRS
              media_content_type: spotify://playlist
            metadata:
              title: "rods playlist "
              thumbnail: https://i.scdn.co/image/ab67616d0000b27343de9079ae02773c1fae9a26
              media_class: playlist
              children_media_class: track
              navigateIds:
                - {}
                - media_content_type: spotify://current_user_playlists
                  media_content_id: current_user_playlists
                - media_content_type: spotify://playlist
                  media_content_id: spotify:playlist:2gvmi685aThtmb1cirWNRS
      - conditions:
          - condition: trigger
            id: Cleaning
        sequence:
          - service: media_player.play_media
            target:
              entity_id: media_player.spotify_smarthome1
            data:
              media_content_id: spotify:playlist:286CJ3BIOOiiXBWcXdK7gI
              media_content_type: spotify://playlist
            metadata:
              title: "our playlist "
              thumbnail: >-
                https://mosaic.scdn.co/640/ab67616d0000b2731336b31b6a1799f0de5807acab67616d0000b27334559f859e2522719de48ff3ab67616d0000b2734408c01a8909ee878c2a42f2ab67616d0000b273b98aacb957a40d613445f92f
              media_class: playlist
              children_media_class: track
              navigateIds:
                - {}
                - media_content_type: spotify://current_user_playlists
                  media_content_id: current_user_playlists
                - media_content_type: spotify://playlist
                  media_content_id: spotify:playlist:286CJ3BIOOiiXBWcXdK7gI
      - conditions:
          - condition: trigger
            id: "Relax "
        sequence:
          - service: media_player.play_media
            target:
              entity_id: media_player.spotify_smarthome1
            data:
              media_content_id: spotify:playlist:6KrFnLwZfBHeJc8FAAUFji
              media_content_type: spotify://playlist
            metadata:
              title: 70's Soft Rock Essentials
              thumbnail: https://i.scdn.co/image/ab67616d0000b273ac2fd4d587d556ae41bd64d7
              media_class: playlist
              children_media_class: track
              navigateIds:
                - {}
                - media_content_type: spotify://current_user_playlists
                  media_content_id: current_user_playlists
                - media_content_type: spotify://playlist
                  media_content_id: spotify:playlist:6KrFnLwZfBHeJc8FAAUFji
    enabled: true
mode: single

Optional code for above automation add start spot cast script to have spot cast start when selecting playlist if spotify is not active. Works but a little slower and changes volume each time you change playlist if you set volume level in script.

alias: "Music Playlist "
description: ""
trigger:
  - platform: state
    entity_id:
      - input_select.playlist
    to: "Rock out "
    id: "Rock "
  - platform: state
    entity_id:
      - input_select.playlist
    to: "Cleaning "
    id: Cleaning
  - platform: state
    entity_id:
      - input_select.playlist
    to: Relax
    id: "Relax "
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "Rock "
        sequence:
          - service: media_player.play_media
            target:
              entity_id: media_player.spotify_smarthome1
            data:
              media_content_id: spotify:playlist:2gvmi685aThtmb1cirWNRS
              media_content_type: spotify://playlist
            metadata:
              title: "rods playlist "
              thumbnail: https://i.scdn.co/image/ab67616d0000b27343de9079ae02773c1fae9a26
              media_class: playlist
              children_media_class: track
              navigateIds:
                - {}
                - media_content_type: spotify://current_user_playlists
                  media_content_id: current_user_playlists
                - media_content_type: spotify://playlist
                  media_content_id: spotify:playlist:2gvmi685aThtmb1cirWNRS
          - service: script.turn_on
            data: {}
            target:
              entity_id:
                - script.spotify_start_playback
      - conditions:
          - condition: trigger
            id: Cleaning
        sequence:
          - service: media_player.play_media
            target:
              entity_id: media_player.spotify_smarthome1
            data:
              media_content_id: spotify:playlist:286CJ3BIOOiiXBWcXdK7gI
              media_content_type: spotify://playlist
            metadata:
              title: "our playlist "
              thumbnail: >-
                https://mosaic.scdn.co/640/ab67616d0000b2731336b31b6a1799f0de5807acab67616d0000b27334559f859e2522719de48ff3ab67616d0000b2734408c01a8909ee878c2a42f2ab67616d0000b273b98aacb957a40d613445f92f
              media_class: playlist
              children_media_class: track
              navigateIds:
                - {}
                - media_content_type: spotify://current_user_playlists
                  media_content_id: current_user_playlists
                - media_content_type: spotify://playlist
                  media_content_id: spotify:playlist:286CJ3BIOOiiXBWcXdK7gI
          - service: script.turn_on
            data: {}
            target:
              entity_id: script.spotify_start_playback
      - conditions:
          - condition: trigger
            id: "Relax "
        sequence:
          - service: media_player.play_media
            target:
              entity_id: media_player.spotify_smarthome1
            data:
              media_content_id: spotify:playlist:6KrFnLwZfBHeJc8FAAUFji
              media_content_type: spotify://playlist
            metadata:
              title: 70's Soft Rock Essentials
              thumbnail: https://i.scdn.co/image/ab67616d0000b273ac2fd4d587d556ae41bd64d7
              media_class: playlist
              children_media_class: track
              navigateIds:
                - {}
                - media_content_type: spotify://current_user_playlists
                  media_content_id: current_user_playlists
                - media_content_type: spotify://playlist
                  media_content_id: spotify:playlist:6KrFnLwZfBHeJc8FAAUFji
          - service: script.turn_on
            data: {}
            target:
              entity_id: script.spotify_start_playback
    enabled: true
mode: restart

Dashbord
I have this in a separate dash board called tablet in a one card Panel view but you can add in anywhere.


card1 screenshot

card2 screenshot

type: horizontal-stack
cards:
  - type: vertical-stack
    cards:
      - square: false
        columns: 1
        type: grid
        cards:
          - type: custom:mini-media-player
            entity: media_player.spotify_smarthome1
            artwork: full-cover
            info: scroll
            source: full
            sound_mode: icon
            replace_mute: stop
            volume_stateless: false
            toggle_power: true
            group: true
  - type: vertical-stack
    cards:
      - type: entities
        entities:
          - entity: input_select.speakers
          - entity: script.spotify_start_playback
          - entity: input_select.playlist
        title: 'pick media '
        state_color: false
      - type: entity-filter
        entities:
          - entity: media_player.nestmini5944
          - entity: media_player.kitchen_nest
          - entity: media_player.nestmini7847
          - entity: media_player.bedroom_tv
          - entity: media_player.livingroomtv
        state_filter:
          - playing
        card:
          type: entities
view_layout:
  position: main

This is the entities filter that only shows active players on the card.
Make sure you add all your Home assistant players used in the Drop down list in the list or they will not show up when active.


strong text

Dont forget to reload scrips in Developer tools before you try this out.

Bonus automation using just Spot cast in HA UI automation.
I wanted to play a joke on Barbara so I did this little automation when she used the bathroom and the lamp triggered it played a random potty time song from Spotify.

description: ""
trigger:
  - platform: state
    entity_id:
      - light.bathroom_lamp
    to: "on"
    from: "off"
condition:
  - condition: state
    entity_id: binary_sensor.work_time_time_block
    state: "on"
    enabled: true
action:
  - service: spotcast.start
    data:
      device_name: Bedroom speaker
      start_volume: 58
      uri: spotify:playlist:56ZJEeDFOZvQ4CABazkAVG?
      random_song: true
      force_playback: true
mode: single

My background. I am NO coder or expert. I write thies long drawn out explanations on how to do things in HA because it take me forever to figure out the GEEK language in most help files.
I have been using sonoff devices and ewelink with IFTTT for about 3 years to automate my home and heating and evaporative cooler. I made the jump to HA and I have been playing with Home assistant since 2018. I’m enjoying playing with WLED
I made a automatic fish feeder, a LED clock and a lamp with ESPHOME also.
Over 30 years I have been installing and servicing Hearth products specializing in Gas and wood pellet burning stoves.
Home automation is a hobby I needed.
my Facebook page diyredneck.thestovedoc.com

18 Likes

Excellent, exactly what I’m looking for
Thank you

1 Like

Hey! I just wanted to add one suggestion. For the music playlist, you can create another drop-down list using Helpers or an input selection in YAML. This list will be used in the music playlist automation. You can set up different playlists and associate them with specific triggers. When a playlist is selected, the automation will use the media_player.play_media service to start playing that playlist on your Spotify HA account media player. Also, have you considered LinkedIn? You can buy the cheapest LinkedIn followers and promote your account or group with guides. I hope you like the idea.

1 Like

Hello,

Thank you for your tuto.

But, I’m dumped from

Music playlist Automation
The first automation below works great. But if Spotify is not already started or active on a device and you select music playlist you need to click the RUN next to Spotify:Start playback after selecting.

You say above that it is not necessary to introduce the uri, because we use the HA media. However, below, you introduced them in the code

 media_content_id: spotify:playlist:2gvmi685aThtmb1cirWNRS
              media_content_type: spotify://playlist

Did I miss something?

Sorry if my English is not terrible, but it is not the native speak

Thank you