Get playing song name

I have a media_player card with Artist an Song info.
I need that info in announce automation like when I ask assist what song is playing.

Is it possible to get playing song name and Artist?

I am thinking of using 2 variable blocks - one for song-name and another for artist. And in announce block combine two variables.

Probably, but it can depend on whether or not the integration provides that data in a way you can access it.

This is a simple version that I use with Voice PE and Music Assistant:

alias: Sentence Query Track and Artist
description: ""
triggers:
  - trigger: conversation
    command:
      - "[who| (what|which) [track|song|artist]] is this"
      - "(name|identify) this [track|song|artist]"
conditions: []
actions:
  - set_conversation_response: >-
      {% set media_players = area_entities(area_id(trigger.device_id))
      | select('match', 'media_player.') | select('is_state', 'playing') 
      | expand | selectattr('attributes.media_artist', 'defined') | first %}
      This is {{media_players.attributes.media_title}} by {{media_players.attributes.media_artist}}
mode: single
More robust version

This version gets all the media player speakers that are playing. If there are multiple, it then tries to get the closest one by comparing the device entities, then area entities, then floor entities. It also includes some randomization of the returned phrase, for fun.

alias: Sentence Query Track and Artist (robust)
description: ""
triggers:
  - trigger: conversation
    command:
      - "[who| (what|which) [track|song|artist]] is this"
      - (name|identify) this [track|song|artist]
actions:
  - set_conversation_response: "{{message}}"
variables:
  are_playing: >-
    {{ states.media_player | selectattr('state','eq','playing') | selectattr('attributes.media_artist', 'defined')
    | selectattr('attributes.device_class', 'eq', 'speaker') | map(attribute='entity_id') | list }}
  active: "{{ are_playing | count }}"
  message: |-
    {%- if not active|bool %}
      There are no near by active speakers.
    {%- else %}
      {%- if active == 1 %}
        {%- set player = are_playing[0] %}
      {%- else %}
        {%- set player = (intersect(are_playing,device_entities(trigger.device_id)) 
        or intersect(are_playing,area_entities(area_id(trigger.device_id))) 
        or intersect(are_playing,floor_entities(floor_id(trigger.device_id)))) %}
        {% set player = player|first %}
      {%- endif %}
      {%- set preface_opt = ['This ' ~ ['','track', 'song']|random,'The current '~['track', 'song']|random,
      'Whats ' ~['','currently ']|random ~'playing']|random %}
      {{-preface_opt ~ ' is '}}{{state_attr(player, 'media_title')}} by {{state_attr(player, 'media_artist')}}
    {%- endif %}
mode: single

IIRC, this specific example using the VPE requires you to have Music Assistant create a media_player entity, because the media_player entity that ESPHome creates for the device doesn’t have the data.

2 Likes

If the media player ent displays it… This works I use a riff on this + TheFes’ music scripts for my LLM work. You can also just have a bare script or expose the media player and put a descriptive alias if you’re not trying to make it work for non llm assist.

I use mpd integration and it has media_player entity with song name. I don’t use Music Assistant and LLM.

From the example I need something like this for my media_player.mpd

"{{ state_attr("media_player.mpd", "media_artist") }}"

and

"{{ state_attr("media_player.mpd", "media_title") }}"

Does this look like a correct syntax?


No, I don’t use LLM. I don’t want to expose the media player. I am thinking about automation with 2 variables, I just need a correct syntax for state_attr.

You need to change the quote marks… if you use " around the entire template, use ' around strings inside the template.

"{{ state_attr('media_player.mpd', 'media_artist') }}"
1 Like

Thanks! Now it works! :tada:

Here is my automation:


alias: get the playing song name
description: ""
triggers:
  - trigger: conversation
    command:
      - what's playing
conditions: []
actions:
  - variables:
      artist: "{{ state_attr('media_player.mpd', 'media_artist') }}"
      song_name: "{{ state_attr('media_player.mpd', 'media_title') }}"
  - action: assist_satellite.announce
    metadata: {}
    target:
      entity_id: assist_satellite.[]
    data:
      preannounce: false
      message: playing {{ song_name }} by {{ artist }}
mode: single

1 Like