Music Assistant 2.0 -- Node Red to Shuffle Playlists

So I am trying to create a ‘Node’ to search playlists in Music Assistant. I believe the call service to search for the playlist is correct as I see the results. However, I can’t seem to write the function to pick one playlist at random and then feed that playlist into the play_media call.

Any thoughts?

[{"id":"ce700870a414c8af","type":"api-call-service","z":"3dec4f5f10a1bb49","name":"Spa Music","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"domain":"mass","service":"play_media","areaId":[],"deviceId":[],"entityId":["media_player.office_group"],"data":"{\t  \"media_id\": \"{{media_id}}\"\t}\t","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":710,"y":220,"wires":[["483f4469cf0b1073"]]},{"id":"15677c0b4788198b","type":"inject","z":"3dec4f5f10a1bb49","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":300,"wires":[["4daaef19cfe2bdd0"]]},{"id":"eb90fbcc7a48fbf1","type":"api-call-service","z":"3dec4f5f10a1bb49","name":"","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"domain":"mass","service":"search","areaId":[],"deviceId":[],"entityId":[],"data":"{\t   \"name\":\"relaxing\",\t   \"media_type\":\"playlist\",\t   \"limit\":10\t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"queue":"none","x":250,"y":120,"wires":[["03dc68887efa6251"]]},{"id":"4daaef19cfe2bdd0","type":"api-call-service","z":"3dec4f5f10a1bb49","name":"Shuffle Set","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"domain":"media_player","service":"shuffle_set","areaId":[],"deviceId":[],"entityId":["media_player.office_group"],"data":"{\"shuffle\":\"true\"}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":270,"y":200,"wires":[["eb90fbcc7a48fbf1"]]},{"id":"483f4469cf0b1073","type":"debug","z":"3dec4f5f10a1bb49","name":"debug 102","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":910,"y":220,"wires":[]},{"id":"22c48ed527b44d04","type":"debug","z":"3dec4f5f10a1bb49","name":"debug 103","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":750,"y":120,"wires":[]},{"id":"03dc68887efa6251","type":"function","z":"3dec4f5f10a1bb49","name":"function 4","func":"var playlists = msg.payload.playlists;\nif (playlists && playlists.length > 0) {\n    var randomIndex = Math.floor(Math.random() * playlists.length);\n    msg.payload = { media_id: playlists[randomIndex].uri };\n} else {\n    node.error(\"Playlists not found or empty\", msg);\n}\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":180,"wires":[["ce700870a414c8af","22c48ed527b44d04"]]},{"id":"6e751b1b.8f17c4","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

media_id in your payload message should be in quotes "media_id"

Thank you for the tip, but that did not fix the issue. When I run the flow it plays the same song each time, yet the media_id changes:

First time run:

apple_music://playlist/pl.725376c1e6b14915b7e27a919db39c52

Second time run:

apple_music://playlist/pl.22b3308452924e7aa49f3e9e9c2b732e
```

I ran each playlist through a call service action in HA and confirmed they generate entirely different queues.

I do not think its the function, but I am stumped. I changed the data in the mass search call to “rock”

In the debug I see ten (10) playlists containing the word “rock”

Yet this song always shows up! This song is not rock and not on the playlist.

image

The action node that you are sending the playlist to, can you show me how you have that configured?

for data use

{ "media_id":  payload.media_id }	

You are returning the media id inside the payload object, so it needs to be included with the path.

Mustache templates {{ }} only work when data is set to json. In Jexpression/jsonata you use the path without msg. like you would in a function node.

1 Like

Worked. Thank you!