Help to build a Spotify Statistics Dashboard in Grafana

Inspired by the Listening report of Last.fm, I want to build my own dashboard of streamed music with the help of Spotify and the last.fm Api, since the last.fm sensor from Home Assistant is not really reliable for me (get a lot of unknown states) and the lastfm Api can grab a lot more information for example the genre of each played song and/or artist. I created two sensors so far that will get me the genre of each Spotify artist that is streamed:

  - platform: template
    sensors:
      get_artist:
        friendly_name: Get Spotify Artist
        value_template: >
           {% if states('media_player.spotify') != 'playing' %}
              Pause
           {% else %}
              {{ states.media_player.spotify.attributes.media_artist |urlencode }}
           {% endif %}

And then another sensor that grabs the genre from the last fm API, triggered by an automation when the artist changes on the spotify media player:

- alias: Update Track Genre
  trigger:
    platform: state
    entity_id: sensor.get_artist
  action:
  - service: homeassistant.update_entity
    entity_id: sensor.get_the_genre
  - platform: rest
    resource_template: http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist={{states.sensor.get_artist.state}}&autocorrect=1&api_key=MYLASTFMAPI&format=json
    name: Get The Genre
    method: GET
    timeout: 60
    scan_interval: 86400
    value_template: >
       {% if states('media_player.spotify') != 'playing' %}
          Pause
       {% else %}
          {{ value_json.artist.tags.tag[0].name | title}}
       {% endif %}

All that works fine so far, and I already displayed it in grafana with the really nice discrete plugin:

SCREENSHOT-2020-12-31 at 13-41-57

SCREENSHOT-2020-12-31 at 13-39-35

As far as I know, that’s the only way to display different string states of one sensor in grafana, but now I’m looking for a way to not show this states in a timeline, but in a graph or pie chart like this here:

SCREENSHOT-2020-12-31 at 13-26-40

Does anybody know if that is possible? One way via Home Assistant would be to create a history stats sensor for each genre

  - platform: history_stats
    name: Genre Heavy Metal
    entity_id: sensor.get_the_genre
    state: 'Heavy Metal'
    type: time
    start: '{{ now().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'

but of course, that would be a lot of work since there are thousands of genres listed…

Hope somebody can help! Thanks

I’d also be interested in this. Did you find a solution?

yes, instead of influxdb Im using timescaledb addon for home assistant - you can do much more things in grafana, like described here:

2 Likes