Switch between media sources and initial TTS announcement

I am all new to Node-RED and have been struggling quite a bit to figure this out.

I have created a flow for my Xiaomi cube to control media player audio on one of my SONOS speakers. All basic functions are working great, but I have a wish to expand with the following functionalities…:

  1. Make of one the cube actions switch between my SONOS favorites (either automatically by pulling all favorites from the media player entity in HA or just by switching between a list of favorites defined by myself… whatever works.

The current flow already support activating a predefined favorite using a call service node (see screenshot), but I cannot figure out how to make node-red switch between a list of favorites activated by the same cube action type.

  1. Ultimately I would like a TTS announcement just prior to start playing the media in the speakers (this is probably the most tricky part).

Anybody able to push me in the right direction? :slight_smile:

Thanks!

1 Like

If you use the check state node with the media player does it display the favorites as an array or similar in the attributes ?

Just tried and yes, the check state node actually reveals the full list of available favorites (or the “source-list”) in the attributes… see screenshot.

So the attribute “source-list” holds my current 4 favorites… Any idea how to use this? :slight_smile:

That’s good but I did not see in the attributes the current playing playlist. If you select one and play one does it come as an attribute the current playlist ?

A new attribute called “source” appears showing the playlist, but only for radio channels… :frowning:

For album stored as favorites, this attribute does not appear when being played.

You can try this one,

[{"id":"ed2a4c34.bdc8a","type":"api-call-service","z":"4bad5756.743d58","name":"","server":"e0d90e7a.95517","service_domain":"media_player","service":"select_source","data":"{\"source\":\"{{flow.current_playlist}}\"}","render_data":true,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":1010,"y":620,"wires":[[]]},{"id":"7fff9bb6.905904","type":"function","z":"4bad5756.743d58","name":"","func":"current_playlist = flow.get(\"current_playlist\")\nplaylist_length = msg.data.attributes.source_list.length\nplaylist_array = msg.data.attributes.source_list\nif (typeof(current_playlist) === 'undefined') {\n    flow.set(\"current_playlist\",msg.data.attributes.source_list[0])\n}\n\ni = playlist_array.indexOf(current_playlist);\ni++\n\nif (i < (playlist_length)) {\n    flow.set(\"current_playlist\", playlist_array[i])\n} else if (i>=playlist_length) {\n    flow.set(\"current_playlist\", playlist_array[0])\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":620,"wires":[["ed2a4c34.bdc8a"]]},{"id":"e0d90e7a.95517","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open"}]

see if it works

But the flow is not fool proof, as you need to add a bunch of other checks, what would happen if you remove the current playing playlist, or one gets added, you need to keep checking. Don’t forget to fill the entity id.

1 Like

I don’t know what integration you’re using (I use zigbee2mqtt) but when I do a flip90 or flip180 it tells me what side it landed on.

I’m not familiar with Sonos but you could just assign each side to a playlist, so flipping the cube around switches between them.

I use rotate_left and rotate_right for volume up/down (makes more sense, like a volume knob). Slide or move to skip current track, flip90 for play/pause.

Wow, that worked perfectly… thanks man!

Regarding the checks you are mentioning. Why wouldn’t it matter if a new playlist is added? Will the code not just always flow between the playlist available to HA?

Thank you for your inputs. However this is not the solution I am searching for. I already have a static playlist assigned to one of the cube functions, but what I really like is for the automation to automatically switch between all playlists available (basically making it more dynamic and less complex to use).

The code made by subzero79 does the trick in a very elegant way.