Ask Alexa what song is playing on media_players

I use Squeeze Server to send music to various media_players in my house. When I want to know the name of the current song, I trigger this Alexa intent. It’ll check my media_players (in order of my preference in case more than one is actually playing) and reply with the name of the song. It’ll also trigger (via script) a persistent_notification so that I can look back later at recently identified songs, as well as a Kodi notification. I have this isolated as a script so that I can trigger it by other methods than just the Alexa skill.

The intent does some basic template manipulations that some might find instructive.

Hope someone finds this helpful.

Alexa intent:

WhatSongIntent:
  action:
    - service: script.whatsong
  speech:
    type: plaintext
    text: >
      {% if states.media_player.squeezeplay.state == 'playing' %}

        {% if states.media_player.squeezeplay.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.squeezeplay.attributes.media_title %}
          {%- if states.media_player.squeezeplay.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.squeezeplay.attributes.media_artist%}{% endif %}
        {% else %}{% set text = "I don't know." %}
        {% endif %}

      {% elif states.media_player.living_room_chromecast.state == 'playing' %}

        {% if states.media_player.living_room_chromecast.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.living_room_chromecast.attributes.media_title %}
          {%- if states.media_player.living_room_chromecast.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.living_room_chromecast.attributes.media_artist%}{% endif %}
        {% else %}{% set text = "I don't know." %}
        {% endif %}

      {% elif states.media_player.living_room_home.state == 'playing' %}

        {% if states.media_player.living_room_home.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.living_room_home.attributes.media_title %}
          {%- if states.media_player.living_room_home.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.living_room_home.attributes.media_artist%}{% endif %}
        {% else %}{% set text = "I don't know." %}
        {% endif %}

      {% else %}{% set text = "No music is playing." %}
      {% endif %}{{ text }}

script:

whatsong:
  alias: 'What Song is Playing'
  sequence:
    - service: persistent_notification.create
      data_template:
        message: >
          {% if states.media_player.squeezeplay.state == 'playing' %}

            {% if states.media_player.squeezeplay.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.squeezeplay.attributes.media_title %}
              {%- if states.media_player.squeezeplay.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.squeezeplay.attributes.media_artist%}{% endif %}
            {% else %}{% set text = "I don't know." %}
            {% endif %}

          {% elif states.media_player.living_room_chromecast.state == 'playing' %}

            {% if states.media_player.living_room_chromecast.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.living_room_chromecast.attributes.media_title %}
              {%- if states.media_player.living_room_chromecast.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.living_room_chromecast.attributes.media_artist%}{% endif %}
            {% else %}{% set text = "I don't know." %}
            {% endif %}

          {% elif states.media_player.living_room_home.state == 'playing' %}

            {% if states.media_player.living_room_home.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.living_room_home.attributes.media_title %}
              {%- if states.media_player.living_room_home.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.living_room_home.attributes.media_artist%}{% endif %}
            {% else %}{% set text = "I don't know." %}
            {% endif %}

          {% else %}{% set text = "No music is playing." %}
          {% endif %}{{ text }}
        title: "What Song?"
          
          
    - service: notify.kodi
      data:
        title: "What's Playing"
      data_template:
        message: >-
          {% if states.media_player.squeezeplay.state == 'playing' %}

            {% if states.media_player.squeezeplay.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.squeezeplay.attributes.media_title %}
              {%- if states.media_player.squeezeplay.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.squeezeplay.attributes.media_artist%}{% endif %}
            {% else %}{% set text = "I don't know." %}
            {% endif %}

          {% elif states.media_player.living_room_chromecast.state == 'playing' %}

            {% if states.media_player.living_room_chromecast.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.living_room_chromecast.attributes.media_title %}
              {%- if states.media_player.living_room_chromecast.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.living_room_chromecast.attributes.media_artist%}{% endif %}
            {% else %}{% set text = "I don't know." %}
            {% endif %}

          {% elif states.media_player.living_room_home.state == 'playing' %}

            {% if states.media_player.living_room_home.attributes.media_title != '' %}{% set text = 'This is ' + states.media_player.living_room_home.attributes.media_title %}
              {%- if states.media_player.living_room_home.attributes.media_artist != '' %}{% set text = text + ' by ' + states.media_player.living_room_home.attributes.media_artist%}{% endif %}
            {% else %}{% set text = "I don't know." %}
            {% endif %}

          {% else %}{% set text = "No music is playing." %}
          {% endif %}{{ text }}
4 Likes

I like this a lot. Im going to have to try this script out later. Although I still havent set up alexa intents yet so i should probably do that first lol.

Thank man

This does look very interesting. Can you add the additional information of how to set up Alexa Intent?