Music Assistant - How can I create a template sensor that gives me the current playlist?

Recently, I’ve started to use Music Assistant more and more, and I love it because it’s playing much more reliably with my Chromecast Audios around the house.

But there’s one thing I can’t wrap my head around:
How can I see which playlist is playing?

Looks like I can’t see it in MA directly and there’s also no attribute that exposes it for the media_player.

Any suggestions on how to retrieve it without installing MA in a separate container (to expose the backend API) rather than running it as an HA Add-on?

I don’t think you currently can… MA doesn’t seem to see it as the player playing the playlist, instead the player is just playing the songs that have been passed into its queue.

This is where I am right now, the trick is to find the token, player_id and queue_id, the latter two are fixed in my case as I only play to one device. If you have multiple then you can use the API to get these too in other template sensors and then re-use in the command_line below.

The token I found browsing through the api-docs which are inside the MA container (example in my case: http://192.168.1.130:8095/api-docs/commands#remote-access

The first sensor is REST, this is where I started with but for the queue I needed jq to simplify the outpout for use in HA

sensor:
  - platform: rest
    name: ma queue current item
    unique_id: ma_queue_current_item
    resource: "http://192.168.1.130:8095/api"
    method: POST
    scan_interval: 20
    headers:
      Authorization: "Bearer YOURTOKEN"
      content-type: "application/json; charset=utf-8"
    payload: >
        {
          "command": "player_queues/get_active_queue",
          "args": {
            "player_id": "00:22:6c:2c:04:69"
          }
        }      
    value_template: "{{ now().strftime('%H:%M:%m:%s') }}"
    json_attributes:
      - current_item

  - platform: folder
    folder: "/media/music/list1" 
    

      
command_line:
  - sensor:
        name: MA Queue Items
        command: >
            curl -X POST --silent --show-error http://192.168.1.130:8095/api -H "Content-Type: application/json" -H "Authorization: Bearer YOURTOKEN" -d '{"command": "player_queues/items", "args": {"queue_id": "00:22:6c:2c:04:69"}}' | jq '{items: map({name: .name,duration: .duration, sort_index: .sort_index})}'
        value_template: "{{ value_json['items'] | length  }}"
        json_attributes:
            - items
        scan_interval: 60 
  - sensor:
        name: MA Queue Remaining Items
        command: >
            curl -X POST --silent --show-error http://192.168.1.130:8095/api -H "Content-Type: application/json" -H "Authorization: Bearer YOURTOKEN" -d '{"command": "player_queues/items", "args": {"queue_id": "00:22:6c:2c:04:69"}}' | jq '{items: map(select(.sort_index >= {{ state_attr('sensor.ma_queue_current_item','current_item').sort_index if state_attr('sensor.ma_queue_current_item', 'current_item') is not none else 0 }} )| {name: .name,duration: .duration, sort_index: .sort_index})}'
        value_template: "{{ value_json['items'] | length  }}"
        json_attributes:
            - items
        scan_interval: 10  

And in HA I use flex-table card

type: custom:flex-table-card
entities:
  include:
    - sensor.music_queue_remaining_items
clickable: true
title: Remaining Queue
display_footer: true
columns:
  - data: items.sort_index
    name: Index
    footer_type: text
    footer_text: remaining
  - data: items.name
    name: Name
    footer_type: count
  - data: items.duration
    name: Duration
    align: right
    footer_type: sum
    modify: >
      if (x != null ) {Math.floor(x/60) +':' + String(parseInt(x
      %60)).padStart(2, '0')} else {0};
    footer_modify: >
      if (x != 0 ) {Math.floor(x/60) +':' + String(parseInt(x %60)).padStart(2,
      '0')} else {0};
    sort_unmodified: true

Hello !

Perhaps i don’t understand but in my cas i can have the list.

I used Mass queue integration

Yeah, true :rofl: and a demo on how one can make ones life miserable by not using the MASS integration.

@chairstacker I missed the part that you donot want MA in a separate container, which is my case. I have no clue if the same approach would work if run as add-on as I donot run HAOS setup

Sorry, seems like my wording wasn’t clear enough:
I can see the songs that are in the queue which is based on the playlist I selected, but I’m looking for the name of said playlist.

Correct!
I run MA as an HA add-on which doesn’t expose the backend API you’re referring to in your first response (by URL).

OK, still not sure but one can activate the webserver 8095 for MA and then try (???) to get to the API via http://homeassistant.local:8095/api-docs, I also remember somewhere that one could fix the IP of HAOS but not sure if that is the case or even not recommended. With changing ip, I am not sure of the underlying yaml recognise mDNS entries in HAOS.

What you could do is ask in the MA forum on discord? Discord

Unfortunately, if MA is installed as an HA Add-On the link to the APi-docs seems to be the only link that works wrt the backend API.

I’ll check on Discord - thanks for the suggestion.

Yeah…not a surpise but worth a try.
As a side note, regardless of the choice for HAOS and add-ons: having MA as a container has proven quite handy for me, it is easy to create backups, go back and forth in beta-releases and I run it on my NAS containing a 4Tb local set of music files…which seems to have helped in reducing disk noise. On top, MA runs fully independent of HA, not being hindered by HA issues/perf./reboots/ etc.

Now, I am very well aware that all of this may not apply to you but…my 2 cents.

to resolve your specific topic of having the playlist visible, that is not a major FR but one that may not see the light soon (just my guesswork) due to loads of interest in other topics…discord will give more transparency in MA which is (I cannot else but repeat this a lot) a really great (!) application

Not sure if I’m misunderstanding but the MA API can be accessed from HA when running as an App (formerly Add-on) using the RESTful Command integration. This is working well for me…

rest_command:
  ma_get_queue:
    url: "http://localhost:8095/api"
    method: POST
    content_type: "application/json; charset=utf-8"
    headers:
      accept: "application/json, text/html"
      authorization: !secret ma_admin_auth_tolken
    payload: >
      {
        "command": "player_queues/get",
        "args": {
          "queue_id": "{{ queue_id }}"
        }
      }

And then call it with…

action: rest_command.ma_get_queue
data:
  queue_id: yourqueueid
response_variable: queue_info

Again: the API is only available if MA is installed independent of HA, i.e. NOT as an add-on.

I don’t want to change the install method :frowning:

Had a great chat on Discord and was told that it should be straightforward to pull the info from the ‘Recently Played’ area - where it doesn’t just show the latest playlists, but also if you played an album or an artist. But it’s not available in the add-on right now.

Should be in one of the next add-on betas and I can “use a REST sensor in HA to request the info until HA integration also gets support for it later.”

I’ll post an update here when I’ve made some progress.

Until then:
Thanks to everybody for your suggestions!

I’m curious how you converted the response into a template sensor. I’m hoping to create a viewable list on my ESP32 Display, but text helpers aren’t an option due to the character limit.

Not sure what your skill level is and I don’t want to highjack this thread so… in short, entity/sensor attributes can hold a metric shit ton more than 255 characters.

1 Like

I wanted to verify this and it is (!) possible to use the API from HAOS. I installed HAOS via HyperV and then the MA latest beta. Start it, do not (!) open the webui form HAOS but open it via the ip of MA (log). Create user/pwd and then you have access, the IP is fixed in the addon so you can use that for any rest/curl. via the ip…/api-docs you can get to the details and run an example, which will give you the token too.

I cannot say this was easy, I prefer my container setup as more straightforward to get access to the music-folders/mass-db/auth/etc.

Thanks for that - I can log in at ws://192.168.7.11:8095/ws and get responses through the websocket interface fine.

Not sure yet, how that will help me to retrieve the name of the currently active playlist, though.
Seems like I’ll have to wait for it to be made available in one of the next betas.

I do read that it was not made available (yet) … and not even sure if the API is the most friendly / efficient way to retrieve that info if-and-when available. My comment was more to make clear that the API also works in HAOS. Net-net, it works for my requirements but I do need to jump through multiple hoops to get to data that may be a lot easier to find though the MA integration :slight_smile:

1 Like