Media player song history (e.g. for Google Home)

I wanted to have a history of what I am listening to when I say “Hey Google, play music”
It might be trivial but I spent a good amount of time getting this right so I’d figured I’d share for the benefit of the next newbie out there.

sensor:
  - platform: template
    sensors:
      bedroom_speaker_song:
        value_template: >
          {{ states.media_player.bedroom_speaker.attributes.media_title }} by {{ states.media_player.bedroom_speaker.attributes.media_artist }}
        friendly_name: Bedroom Speaker Song
        icon_template: mdi:music

Some of the links that got me here:

7 Likes

Hi, thanks for this.
I would like to use this to keep a record of the tracks we play via our media devices. I have added the sensor and it reflects the track played just fine.

What are you using to display/show the tracks played? Are you showing this in HA?

Thanks again.

Thanks for sharing your result. I’m looking to do similar. Are you using the built in History function in HA to view the history?

This thread was very useful to me, and it led me to create a list of recently played media player songs and when they played

I used this template sensor that I added to configuration.yaml

template:
  - sensor:
      - name: "Spotify Song & Artist"
        state: >
          {%  if state_attr('media_player.spotify_dan', 'media_title') != none -%}
          {{ state_attr('media_player.spotify_dan', 'media_title') }}  |  {{ state_attr('media_player.spotify_dan', 'media_artist') }}
          {%- endif %}
        icon: mdi:format-list-bulleted

To display the information I used the Home Feed Card on Github | Forum Thread and these settings on my dashboard

type: custom:home-feed-card
title: Spotify Recently Played
show_empty: false
scrollbars_enabled: false
more_info_on_tap: true
state_color: true
compact_mode: true
entities:
  - entity: sensor.spotify_song_artist
    include_history: true
    max_history: 25
    format: time
    content_template: '{{state}}'
2 Likes