Music Assistant playlist random access in automation

Hello!

This is my very first post in this Community forum!

I’ve been using Home Assistant for a while and I recently discovered the Music Assistant server and add-on. I’ve used the MA APIs to automate playing a selected playlist on a selected speaker. When I do this with the play_media action ( Media player - Home Assistant ) it starts playing from the first track.

Question: Is it possible to provide a queue index input parameter in this API (as an extra perhaps) so that I can start playback from any index from the playlist (like the “Play Playlist from here” option in the MA Frontend)? If this API doesn’t support it, is there any alternative way other than calling play_next_track repeatedly (very slow) till the desired queue_index is reached?

Thanking you in advance!
Ajith

Thank you for the reply!

I have used Node-RED and replicated what you suggested with the Custom Script option, delay and all. It works. But each time I call play_next_track I need to check if the queue_index (as output from speaker state attribute) has reached the desired index. I cannot repeatedly call play_next_track quickly as it overwhelms the system (as you rightly mentioned) and it misses actually skipping tracks randomly. So checking the current queue_index seems to be the only way. And that takes time (nearly a couple seconds). So If I have to skip 10 tracks, it takes nearly 20 seconds while listening to half-a second of each song being skipped!

Seeing that the front-end has this “Play Playlist from here” option, I tried to see what API it might be calling in Chrome dev tools. I didn’t find anything to my surprise. Then realized it is a websocket call. And surprisingly, Chrome doesn’t show up the MA frontend WS calls. I tried to use the WS node in Node-RED to send a request to MA (after enabling the MA webserver port), but I am missing something there as the WS connection gets disconnected as soon as I send a JSON payload. Maybe I am not getting the API signature right (or authentication?). Any help in calling the MA WS API from Node-RED is appreciated!

If all else fails, I will raise that Feature Request :slight_smile:

I figured it out, finally! Here’s a brief (EDIT: maybe not-so-brief!) description, in the hope that it helps someone.

I noticed that in the MA web interface if I open the Chrome dev tools’ console tab (the network tab isn’t showing anything useful when I am using the interface), I can see the message “Connecting to Music Assistant API ws://172.20.1.100:8123/api/…/ws” and also the JS source file which printed this message. Clicking the filename opens the source itself, allowing me to do a Ctrl+F to find “mediaPlay”, which I did. I put a break-point at that line and clicked the “Play Playlist from here” menu item in the MA interface, and execution stopped at this line, as expected. From there, I could trace the execution to the most important line of code I was looking for:

this.ws.send(JSON.stringify(o))

Inspecting the value of “o” got me what I wanted. It looks something like this:

{
  "command": "player_queues/play_media",
  "message_id": 30,
  "args": {
    "queue_id": "767077c6-8498-5920-c0ae-04029797bdb2",
    "media": "library://playlist/109",
    "radio_mode": false,
    "start_item": "Hg8Fa_EUQqY"
  }
}

I took this data and used it in the websocket node of Node-RED and voila! My speaker started playing from the selected track! I was relieved that there wan’t any complicated authentication :roll_eyes: needed!

It goes without saying that every operation that one can do from the MA web interface can be automated this way! For example, I found the JSON payload for the WS call that adds and removes a track from Favorites.

If only the MA websocket API was documented somewhere (didn’t find any after some search. I might have missed it too), it would have saved me a few days.

So if you’re wondering what this project of mine is all about, it is this -

I wanted to be able to play and navigate music from a few playlists I created in MA, on my Google Nest Audio speaker, with minimal effort - just by picking up an IR remote and pressing a key (key-presses received by ESPHome-based IR Receiver and relayed to Home-Assistant/Node-RED). For feedback on what’s going on, like which playlist, what track, and the media transport operations, I wanted it all displayed at appropriate times or when needed, on a ESPHome-based MAX7219 Dot Matrix display (that also doubles as a digital clock).

Other features include being able to do all of the following using just the remote -

  • start music with a single button press - start at the last played playlist and track, no matter the state (off, idle, empty queue etc.,) of the speaker.
  • cycle through playlists
  • skip to next/go to previous track
  • seek within playing track
  • display on demand (using remote), as well as on track change, what’s currently playing

Some more features -

  • display a progress-bar of the current track
  • automatic saving of the current queue_index of each playlist when played, so when I come back to a previously played playlist, I can continue from where I left off (I love this feature :slight_smile: )

So this project is finished now, with all of the above features working. It is all mostly done with Node-RED flows in the Node-RED add-on of Home Assistant. I find wiring up Node-RED flows for complex tasks a lot faster than, say scripting or automation using YAML.

And finally, the WebSocket API of Music Assistant is a major part of my project now! A lot of its features wouldn’t have been possible without it!

With more usage, I will surely add more features. If readers have suggestions to improve the user-experience, you are very welcome to suggest!