Setting Up Lovelace Accessible Alexa Multi Room Audio

HA Accessible Alexa Multi Room Audio Setup

This is just a quick project to setup a way to send and monitor audio with Alexa Multi-Room Music Groups via Lovelace in Home Assistant. There is not a streamlined way to do this, and I hate having to do it through the Alexa app. This helps for times that I don’t want to yell long commands, or even if I want to start music to groups from Home Assistant for when I am arriving home or some other automation purpose.

You must create the Alexa Multi-Room Music Groups in the Alexa app.

Alexa Media Player Setup

This project uses the Alexa Media Player custom component to interact with my Alexa Devices. I won’t go into detail on setting this up, because I haven’t deviated any from the standard setup for this custom component.

There is currently a limitation to Alexa Media Player which can be alleviated using this setup. You can start and stop music to individual echo devices, but you can’t send music directly to an Alexa Multi-Room Music Group. The workaround that I’ve found to be most consistent is sending a custom simulated voice command to Alexa through the component, like “Play Jazz on the Everywhere Music Group.”

Home Assistant Helpers

I used HA’s helpers to set up an input_select entity and an input_text entity.

I created the input_select entity in the UI by going to Configuration/Helpers and clicking the add button, then “Dropdown.” I then added a name, in my case “Alexa Music Groups”. This entity will be used to let me choose which Alexa Music Group I want to use to play whatever music or audio I choose.

Mine are:

Everywhere
Downstairs
Upstairs
Front of House
Master Bedroom

I also changed the icon to “mdi:amazon”

I then created the input_text entity in the UI by going to Configuration/Helpers and clicking the add button, then “Text.” I then added a name, in my case “Alexa Text Input for Groups”. This entity will be used to type text for whatever music or audio I want sent to the Alexa Music Groups. I changed the icon to “mdi:music”

Scripts

I only created two scripts for use in this project. The first one is what I use to start the audio on the group that I have selected. (This is “Play Music on Alexa Groups.”) The other is an (overkill) fail-safe to make sure all music and audio stops if I want it to stop. I made these as scripts so I could control them both through automations and manual input buttons in the UI.

##############################################################################
###Script to send data from helper entities to Alexa via Alexa Media Player###
##############################################################################
alexa_input_music:
  alias: Play Music on Alexa Groups
  sequence:
    - service: media_player.play_media
      data_template:
        entity_id: media_player.echo_link
        media_content_id: "play {{ states('input_text.alexa_text_input_for_groups') }} on the {{ states('input_select.alexa_music_groups') }} group"
        media_content_type: custom

If the text input is set to “Classical Music” and the input select is set to “Everywhere,” this script will send the simulated voice command, “play Classical Music on the Everywhere group” to my Echo Link when it is run. Note the need to use “data_template” for this script to work.

##############################################################################        
#################Script to stop all audio on all groups#######################
##############################################################################
stop_alexa_music:
  alias: Stop Music on Alexa Devices
  sequence:
    - service: media_player.play_media
      data:
        entity_id: media_player.echo_link
        media_content_id: stop music on everywhere group
        media_content_type: custom
    - service: media_player.play_media
      data:
        entity_id: media_player.echo_link
        media_content_id: stop music on front of house group
        media_content_type: custom
    - service: media_player.play_media
      data:
        entity_id: media_player.echo_link
        media_content_id: stop music on downstairs group
        media_content_type: custom
    - service: media_player.play_media
      data:
        entity_id: media_player.echo_link
        media_content_id: stop music on upstairs group
        media_content_type: custom
    - service: media_player.play_media
      data:
        entity_id: media_player.echo_link
        media_content_id: stop music on master bedroom group
        media_content_type: custom
    - service: media_player.play_media
      data:
        entity_id: media_player.echo_link
        media_content_id: stop music
        media_content_type: custom

This one is just sending “stop music” commands to all possible groups.

Card for Lovelace UI

Next, I combined everything into a simple Lovelace card:

Custom Card for Lovelace UI

type: vertical-stack
cards:
  - type: entity
    entity: input_text.alexa_text_input_for_groups
  - type: horizontal-stack
    cards:
      - type: entity
        entity: input_select.alexa_music_groups
      - type: horizontal-stack
        cards:
          - type: button
            tap_action:
              action: toggle
            entity: script.alexa_input_music
            icon: 'mdi:play'
            icon_height: 50px
            name: Play
          - type: button
            tap_action:
              action: toggle
            entity: script.stop_alexa_music
            icon: 'mdi:stop'
            icon_height: 50px
            name: Stop All

If you click on the Text Input in Lovelace, you can type in the text representing the music or audio you want to send to Alexa:

You can click on the Dropdown entity in Lovelace to choose which Multi-Room Music Group you want to send your request to:

Now, all you have to do is click “Play” to launch the script and the music you requested will play on the group you chose! To organize everything, I placed the card in a Lovelace view that contained all of my Alexa Media devices as media cards. This allows me to see everything playing in one place, and gives me controls to pause, play, skip, etc.

Custom Card for Lovelace UI

7 Likes

@davearneson I currently use the same method as you to play music on Alexa speaker groups but was wondering if you ever found another way to do this in which the entity name of the speaker group is used.?

Hey sorry for the delayed response. I’m not actually sure I know what you mean.

As far as I know, nothing has changed in the Alexa Media Player component. The documentation indicates that you still have to treat it like a voice command as a work around to get media to play on Alexa groups.

I don’t know if you use Spotify, but I switched to Spotify after posting this originally, and it plays much better with Home Assistant and the Alexa groups. I usually just play from the Spotify app and all of my Alexa groups are available as output speakers there.

I do still use this work around for playing music from Spotify in automations or from actionable notifications.

do you use the alexa’s with spotify now?

@davearneson sorry for my delayed response. I think mine is a lot more delayed than yours. Lol.

So you definitely understood everything. I do exactly as you used to do and I treat it like a voice command. I was just hoping that maybe you knew something that I didn’t and that maybe now the speaker group entity could be used instead of using the voice command. Oh well.

I know you were previously using Apple Music and I was actually using Amazon Music Unlimited but I did so only because it came free with my cell service. When I recently switched cell service I lost the Amazon Music Unlimited so I’m giving Spotify a try. So far, I’ve been impressed with Spotify but not enough to make the complete switch. I still utilize both, it just depends on what I’m doing or if I was too lazy to switch something over. I have a lot of single-button presets that when pressed play a particular artist or playlist so some I left alone and others I changed. I have noticed that by not having the Unlimited service I no longer have access to the same amount of music content so I’ll have to make a choice soon.

I do have two questions for you in regards to Spotify. I don’t switch speakers very often but when I do I have occasionally run into the issue where one of my Alexa speakers or Alexa speaker groups is missing from the available Spotify speaker options. Have you ever had that happen and if so do you know why?

My other question is, do you know of a way to create a button that could bring up the Spotify Media Libray with a single press? I tried using the button-card and browser-mod popup but I haven’t had any luck. To currently access it I have to click on the media player or a Spotify entity card so that the Spotify details box pops up. On the details box under the media controls, there is an option that says Browse Media. When I click on that the Spotify Media Library opens. From there I can easily navigate through Spotify playlists, albums, artists, and more. I’m trying to find a way to circumvent everything and have the media library pop up with a single button but I can’t find a command or entity that applies to it. Any suggestions.?

Hi, A really usefull feature which I would want to use. As I am new to HA I have hit a snag in as much when I try and save the first script I get a failure "message malformed, unable to deterime action @data [“sequence”][0]
Script looks as follows:

 alexa_input_music:
  alias: Bayern 1 auf Heim Gruppe
  sequence:
    - service: media_player.play_media
      data_template:
        entity_id: media_player.echo_show_8
        media_content_id: >-
          play {{ states('input_text.alexa_text_input_for_groups') }} on the {{
          states('input_select.alexa_music_groups') }} group
        media_content_type: custom

Can you give me a hint as to where I have gone wrong. Many thanks
Andy

Your script should work. I plugged it into mine and only changed the entity_id to match one of my echo devices and got no errors.

Did you set up Alexa Media Player and the two helpers as described in the guide?

Yes, so sorry about another delayed response. I am in law school (and taking summer classes AND working full time) and my wife and I are expecting our first baby on Tuesday - so I’ve been crazy busy. Hopefully, you’ve already figured out the answers to your questions, but I’ll try to help anyway just in case.

Yes, speakers that haven’t been used recently drop from the Spotify list (in Spotify app itself) for reasons within Spotify’s architecture that I haven’t quite worked out. You can always call them from the voice-simulating script or by voice and it will usually bring them back into the Spotify list. Not sure how Spotify is determining which available echos to drop. Hasn’t really been a big deal for me, but it is a minor annoyance for sure.

To create a button in lovelace to browse your Spotify library, you could do this:

show_name: true
show_icon: true
type: button
tap_action:
  action: navigate
  navigation_path: /media-browser/media_player.spotify_davearneson
icon: mdi:spotify
name: Spotify Media Browser
hold_action:
  action: none

(Replacing media_player.spotify_davearneson with whatever your spotify entity is called).

You can just do it in the UI, it will look like this:

That button will take you right to your media browser with one click.

Hope that helps!

1 Like

Hi Thanks for the reply. Yes the two helpers are set up. Will spend some time looking further at least there are nor glaring errors. once again thanks.

Do you have the Alexa Media Player custom component working correctly? Maybe there is an issue there regarding the availability of the “media_player.play_media” service?

will re install the Alexa Media Player. I had teh player already installed and have been useing it together with node red to start different music streams. All work OK but it is a clumsy way. I found your method far more elegant.

@davearneson yes that definitely did it. Now that I see it I can’t believe that I wasn’t able figure it out. I was using /media-browser instead of /media-browser/media_player.spotify which is what I call mine. So close yet so far away. Lol. I created a popup instead of a navigation so I could close it out and be right back at my dashboard.

type: custom:button-card
show_name: true
name: Spotify Media Browser
icon: mdi:spotify
tap_action:
  haptic: heavy
  action: fire-dom-event
  browser_mod:
    command: popup
    title: Spotify Media Browser
    hide_header: false
    large: false
    card:
      type: iframe
      url: /media-browser/media_player.spotify
      aspect_ratio: 200%

I might as well as you one random question that I have regarding syntax that I don’t quite understand. I use this myself for a custom_field in order to get the brightness of a light on the top right corner of a custom:button-card. It’s one of the rare times that I see it being used. I have a feeling there’s a simple answer and my coding knowledge is limited so I’m learning YAML, Jinga, Python, and Javascript as I go. Here’s the full code being used followed by the line that I do not understand…

brightness: >
  [[[ if (entity && entity.attributes && entity.attributes.brightness) {
  return Math.round(entity.attributes.brightness * (100 / 255)) + '%' }
  return '' ]]]
if (entity && entity.attributes && entity.attributes.brightness)

I understand the math and how it’s rounding the brightness attribute but why the need to use all three.? I haven’t tried using just the brightness attribute but I’m assuming that it would work on its own unless I shouldn’t be assuming. Thanks for the help.

Hi all,
is this method still working?
I ve got a problem when i try to pass the states of input_select.
For example if i have “Bathroom” in input_select and i send this:
media_content_id: "play music on {{ states(‘input_select.audio_output’) }}
she don’t understand bathroom, but literally what i wrote ( {{ states… ecc)

any suggestion?

Ty

You need to police your quote marks… unpaired quote marks can cause weird issues and errors because the yaml interpreter starts treating things that are not literal strings as literal strings.

media_content_id: play music on {{ states('input_select.audio_output') }}

Could someone tell me how to tweak the code if I only have 1 Alexa, but wanted to use an input selector dropdown to choose music platform. Amazon, Apple, Pandora, etc?