Start music on Alexa media player (or movie on Plex) when a tag is scanned

Hi,

Here’s my blueprint to start a music on any Alexa media player when a given tag is scanned.

For the blueprint to work, you need to have installed and configured https://github.com/custom-components/alexa_media_player first.

Cheers,

blueprint:
  name: Start music on Alexa when tag is scanned
  description: When a tag is scanned, start the associated music on the selected Alexa media player
  domain: automation
  input:
    tag_id:
      name: Tag ID
      description: The tag ID for the selected song
    song_name:
      name: Song name
      description: The name of the song to play
    alexa_entity_id:
      name: Alexa player
      description: The entity ID of the alexa player to use
      selector:
        entity:
          domain: media_player

trigger:
  - platform: tag
    tag_id: !input tag_id

action:
  - service: media_player.play_media
    data_template:
      entity_id: !input alexa_entity_id
      media_content_id: !input song_name
      media_content_type: "AMAZON_MUSIC"

6 Likes

I spent about an hour trying to figure out how to select a tag_id, but gave up before I got to just typing it in.

Good work.

2 Likes

hi nice work how do i start a playlist and not a single song only?

I believe it sends “Play !song_name” command, so if you replace the song name by a playlist name it should work.

jea sorry it workt forgot to tell it

great work something like this to scan a tag to play a movie from plex on a tv would be Amazing

That should be possible given https://www.home-assistant.io/integrations/plex/#service-media_playerplay_media

Nice what if i want to use spotify or tunein radio

would you be able to maybe create this, i have been playing about with it for days but im just lost

Just replace the action part of the template by he following:

action:
  - service: media_player.play_media
    data_template:
      entity_id: !input alexa_entity_id
      media_content_id: '{ "library_name": "Music", "artist_name": "!input artist_name", "album_name": "!input album_name", "track_name": "!input song_name" }'
      media_content_type: MUSIC

And add the following to the input part:

artist_name:
      name: Artist name
      description: The name of the artist
album_name:
      name: Album name
      description: The name of the album of this artist the song belongs to

I haven’t tried it yet but should work. If not, maybe remove the double quotes around !input tags.

Cheers was after it to start a Movie, but i will use this and see if i can get it running with a movie

Then replace MUSIC with MOVIE and set in media_content_id the following: '{ "library_name": "Movies", "title": "!input movie_name" }'

hi mate,

Have got this far but not getting the movie playing, sorry i have probably butchered your Blueprint.

blueprint:
  name: Start Movie on Plex when tag is scanned
  description: When a tag is scanned, start the associated music on the selected Alexa
    media player
  domain: automation
  input:
    tag_id:
      name: Tag ID
      description: The tag ID for the selected song
    Movie_name:
      name: Movie Name
      description: The name of the movie to play
    alexa_entity_id:
      name: Alexa player
      description: The entity ID of the alexa player to use
      selector:
        entity:
          domain: media_player
  source_url: https://community.home-assistant.io/t/start-music-on-alexa-media-player-when-a-tag-is-scanned/256630
trigger:
- platform: tag
  tag_id: !input 'tag_id'
action:
- service: media_player.play_media
  data_template:
    entity_id: !input 'alexa_entity_id'
    media_content_id: '{ "library_name": "movies", "title": "!input movie_name" }'
    media_content_type: MOVIE

Indeed it was not as simple as planned.
After a few dozens unsuccessful attempts, here one that works :smiley:

blueprint:
  name: Start movie on Plex when tag is scanned
  description: When a tag is scanned, start the associated movie on the selected Plex media player
  domain: automation
  input:
    tag_id:
      name: Tag ID
      description: The tag ID for the selected movie
    movie_name:
      name: Movie name
      description: The name of the movie to play
    movie_library:
      name: Movie library
      description: The name of the movie library in plex, usually "Movies"
    plex_player_id:
      name: Plex player
      description: The entity ID of the Plex player to use
      selector:
        entity:
          domain: media_player

variables: 
  name: !input movie_name
  library: !input movie_library

trigger:
  - platform: tag
    tag_id: !input tag_id

action:
  - service: media_player.play_media
    data_template:
      entity_id: !input plex_player_id
      media_content_type: movie
      media_content_id: '{ "library_name": "{{ library }}", "title": "{{ name }}" }'

Thanks for creating this Blueprint. Couple questions if you dont mind…
Im totally lacking the knowledge needed in yaml as of now, but Im learning. I have a playlist in spotify I would like to play through my echo device. What part of the code and with what would I change it to.

Thanks,
Jace

According to the doc (https://www.home-assistant.io/integrations/spotify/), replace AMAZON_MUSIC by playlist and in the song_name input use the spotify playlist URI (see doc above for how to get it).

I gave it a try. I added the url from the share spotify playlist link into the song id and set the media content type to playlist. Nothing played. Unfortunately…
Thank you

Sorry but I don’t have a Spotify account so I can’t try.

I changed your blueprint so its more universal and modified some descriptions. It needs some work on the descriptions and though cause I only tested Universal, amazon and spotify (i dont have the rest), but it seems to work fine. Its a good working prototype

Maybe you can check this out and look if you wanna adept this to your blueprint. Its solves the issues with spotify for the most users and as a last chance you can use the Universal source, where you can type in what you would say to your alexa device and it will work.

blueprint:
  name: Start music on Alexa when a tag is scanned
  description:
    When a tag is scanned, start the associated music on the selected Alexa media player.
    You can also run commands with the "universal function"
  domain: automation
  input:
    tag_id:
      name: Tag ID
      description:
        The tag ID of your NFC trigger. Can be found under Settings --> NFC Tags
        and then click on the NFC Tag you want to choose.
    alexa_entity_id:
      name: Alexa player
      description: The entity ID of the alexa player to use
      selector:
        entity:
          domain: media_player
    song_name:
      name: Song name.
      description: The name of the song to play. The source is specified below.
        Make sure to select only one source. If its known that a source must be formatted right
        then its specified in the source you can select.
    source_amazon:
      name: Amazon Music
      description: Music is from Amazon Music. The Song name can be the songname
      default: false
      selector:
        boolean:
    source_spotify:
      name: Spotify
      description:
        Music is from Spotify. Song name must be songname (e.g. Still Alive),
        songname and interpret (e.g. Still alive from Red ),
        "playlist" and playlist name (e.g. Playlist Oldies)
        or the name of a featured playlist from Spotify (e.g. Missed Hits).
      selector:
        boolean:
    source_apple:
      name: Apple Music
      description: Music is from Apple Music.
      default: false
      selector:
        boolean:
    source_deezer:
      name: Deezer
      description: Music is from Deezer.
      default: false
      selector:
        boolean:
    source_tunein:
      name: TuneIn
      description: Music is from TuneIn.
      default: false
      selector:
        boolean:
    source_custom:
      name: Universal
      description:
        You can put a command in the songname which you would say to your Alexa device e.g.
        "Play my playlist Oldies on Spotify" (leave out your trigger word from your device).
        Requires Alexa Media Player version 3.4.0 or newer.
      default: false
      selector:
        boolean:

variables:
  is_spotify: !input "source_spotify"
  is_amazon: !input "source_amazon"
  is_apple: !input "source_apple"
  is_deezer: !input "source_deezer"
  is_tunein: !input "source_tunein"
  is_costum: !input "source_custom"

trigger:
  - platform: tag
    tag_id: !input tag_id
action:
  - service: media_player.play_media
    data_template:
      entity_id: !input alexa_entity_id
      media_content_id: !input song_name
      media_content_type: >
        {% if is_amazon %}
          AMAZON_MUSIC
        {% elif is_spotify %}
          SPOTIFY
        {% elif is_apple %}
          APPLE_MUSIC
        {% elif is_deezer %}
          DEEZER
        {% elif is_tunein %}
          TUNEIN
        {% elif is_costum%}
          custom
        {% endif%}

1 Like

any idea how (if it’s possible) to extend this to allow the Alexa device be selected via an Input Select dropdown similar to how you’ve done it in your TTS Blueprint. Ideally using the Friendly Name of the media_player entity rather than it’s entity_id

Also, as an aside “borrowing” the Input Text structure from your TTS Blueprint (same link as above) allows you to input a song/playlist as text and have this play when the automation executes (I’m using a button in the UI to call the automation)