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}]