Creating home assistant interface to alexa playlists

Goal:

Use Home Assistant to use Amazon Echo to play Amazon Music playlists

Resources used:

Home Assistant Community Store – Alexa Media Player

Mini Media Player card – https://github.com/kalkih/mini-media-player

Steps:

  • Install Alexa Media Player – https://github.com/custom-components/alexa_media_player/wiki

    This was a bit of a pain in the ass, getting the two factor authorization set up but I finally got it working. You need to follow the instructions carefully.
  • Install the Mini Media Player – https://awesomeopensource.com/project/kalkih/mini-media-player#shortcut-item-object
  • I used the example card from the documentation in step 2 with gave me a text input field to send a message for the echo to play. While this was interesting, it is not very useful by itself. But I have noticed how often the Echo device will trigger on normal conversation when it hears it’s own keyword “alexa” spoken on the TV for example. So I typed into the text input field “Alexa play my country playlist on echo living room”, and hit the send button. A moment later, the echo played the message, recognized it as an echo command, and played the playlist.

    Now I have multiple echo devices, and the “living room” echo played the command, and a second echo device “heard” it and executed the command. But I suspect that this might not work with a single echo.
  • So now I had to by-pass the text input field. I didn’t want to have to type a command each time I wanted to play music. With a lot of internet searching I finally pieced all of the parts together. What I wanted on the mini media player card was a list of buttons, each one for a play list:

  • After many false starts I figured out I needed to create a series of scripts, one for each playlist, and then call the scripts from the shortcuts object of the mini media player.

Here is the code (manually entered) that creates the mini media player card shown above.

type: 'custom:mini-media-player'
entity: media_player.echo_living_room
icon: 'mdi:amazon'
artwork: cover
shortcuts:
  columns: 1
  buttons:
    - icon: 'mdi:cat'
      name: Country playlist
      type: script
      id: message_alexa_country
    - icon: 'mdi:cat'
      name: French playlist
      type: script
      id: message_alexa_french
tts:
  platform: alexa
  enity_id: media_player.echo_living_room

Note that I kept the tts: entry which shows the text input box. That isn’t needed but I figured why not keep it for fun. The key parts are the buttons with and type: script. This code is entered in to the input box you get when creating a manual card. Briefly, when you are editing the interface and hit the “add card” button, scroll down to the bottom of the list of pre-defined cards and click the “Manual” link.

link to manually create a Lovelace card
Text box for defining the card code

Scripts

The next part was coding the scripts. This code is in the scripts.yaml file. I use the visual studio code editor for Home Assistant: https://community.home-assistant.io/t/home-assistant-community-add-on-visual-studio-code/107863

The script code looks like:

message_alexa_country:
  sequence:
    - service: notify.alexa_media_echo_living_room
      data:
        target:
          - alexa_media_echo_living_room
        data:
          type: tts
        message: >
          {{- "Alexa play country playlist on echo living room" -}}
message_alexa_french:
  sequence:
    - service: notify.alexa_media_echo_living_room
      data:
        target:
          - alexa_media_echo_living_room
        data:
          type: tts
        message: >
          {{- "Alexa play french playlist on echo living room" -}}

Issues and next steps

This probably requires that you have two echo devices. I happen to have five in my house so when one speaks the text to speech command, another one hears it and executes the command. I need to test to see if this works with only one echo device.

The echo device miss-hears the commands. This seems to be a problem with the echo.

If music is already streaming the echo asks if you want to stop the streaming.

I would like to call the scripts with parameters so I don’t have to create a new script for each new playlist.

Leave a Reply

Your email address will not be published. Required fields are marked *