How to enumerate Sonos favourites?

Could anyone share code that I could use in a rule to enumerate all the favourites of a Sonos speaker? I can change them using the :favorite channel, and I can see them listed in Paper UI. The table in the documentation, however, states that this channel is Write-only. I am hoping to read from it. Many thanks.

Here’s how I do mine by day.

var String Sonos_Mon_Favorite = ‘Chill Out Music’
var String Sonos_Tue_Favorite = ‘Chill Tracks’
var String Sonos_Wed_Favorite = ‘Cafe Low-Fi’
var String Sonos_Thur_Favorite = ‘Chill Out Music’
var String Sonos_Fri_Favorite = ‘Chill Instrumental Beats’
var String Sonos_Sat_Favorite = ‘Chill Tracks’
var String Sonos_Sun_Favorite = ‘Chill Out Music’
var String Sonos_Today_Favorite = null

switch (currDayofWeek) {

	       case 1: 	{	
						Sonos_Today_Favorite = Sonos_Mon_Favorite
						Echo_Today_Favorite = Echo_Mon_Favorite
					}		
	       case 2: 	{	
						Sonos_Today_Favorite = Sonos_Tue_Favorite
						Echo_Today_Favorite = Echo_Tue_Favorite
					}
	       case 3: 	{	
						Sonos_Today_Favorite = Sonos_Wed_Favorite
						Echo_Today_Favorite = Echo_Wed_Favorite
					}
	       case 4: 	{	
						Sonos_Today_Favorite = Sonos_Thur_Favorite	
						Echo_Today_Favorite = Echo_Thur_Favorite	
					}
	       case 5: 	{	
						Sonos_Today_Favorite = Sonos_Fri_Favorite
						Echo_Today_Favorite = Echo_Fri_Favorite
					}
	       case 6: 	{	
						Sonos_Today_Favorite = Sonos_Sat_Favorite
						Echo_Today_Favorite = Echo_Fri_Favorite
					}
	       case 7: 	{	
						Sonos_Today_Favorite = Sonos_Sun_Favorite
						Echo_Today_Favorite = Echo_Sun_Favorite
					}
		default:	{	
						Sonos_Today_Favorite = Sonos_Mon_Favorite
						Echo_Today_Favorite = Echo_Mon_Favorite
					}


		    Sonos_LivingRoom_Favorite.sendCommand(Sonos_Today_Favorite)
		        Thread::sleep(1500)

		    Sonos_LivingRoom_PlayQueue.sendCommand(ON)
		        Thread::sleep(1500)

Best, Jay

Thank you, @jwiseman, but this is not what I was looking for…I don’t think I have expressed my need clearly, apologies for that.

What I am looking for is a programmatic way to retrieve all the already defined favourites from the speaker. In your case you are manually listing them out, and that is what I am trying to avoid.

The reason for that is that I am about to write a rule that randomly switches to a station within the currently playing genre whilst avoiding any stations recently played. I do not want to maintain several hand-written listings of my favourites and I would prefer to just retrieve them from the speaker.

I am looking for code that retrieves (ie. enumerates) the stations from the favorite Sonos binding channel.

I too am looking for this info. It’s possible, as the display in ‘PaperUi -> Controls’ show a drop down list of the channels. I’d assumed that the ‘favorites’ item would have been an array of strings, but it only seems to display the first item.

Favorites

Any help gratefully received.

Any news with openHAB 3 how to get the defined favorites out of the speaker?

Hi,
in another forum entry I found a hint to access the favorite command entrys.
I use it to generate a dynamically drop down list.
Perhaps you can use the statement in your rule, etc.

<h4>Sonos Favoriten</h4>

<div class="btn-group" uib-dropdown dropdown-append-to-body="true"
     ng-init="sonos_favorites = getItem('str_sonos_kitchen_favorite').commandDescription.commandOptions">
  <button id="single-button" type="button" class="btn btn-primary" uib-dropdown-toggle>
    {{itemValue('str_sonos_kitchen_favorite')}} <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" uib-dropdown-menu role="menu" aria-labelledby="single-button">
    <li role="menuitem" ng-repeat="favorite in sonos_favorites">
      <a ng-if="$index != 0" ng-click="sendCmd('str_sonos_kitchen_favorite', favorite.command)">{{favorite.command}}</a>
    </li>
  </ul>
</div>

In HabPanel it looks like:

Bildschirmfoto 2021-01-14 um 15.49.56

1 Like

Hi @GuidoS - Is this a custom habpanel widget?

Hi Mike,
I do not use custom widgets.
I’m using the template object and there I insert the code.

Fantastic, thanks for the quick response @GuidoS
I’ve got it working now too!

You mentioned the other forum post that describes how to go about enumerating the favourites list.
looks like it’s this line that makes that work.

"sonos_favorites = getItem('str_sonos_kitchen_favorite').commandDescription.commandOptions"

I’d like to learn more about this and I’m not seeing anything in the docs about this - do you recall the other forum post that describes this?

Thanks

@iLion @evansnp @Rafal @RedRocket If you’re still looking - this method works!

1 Like

I’ve made an update to allow configuring the title and the sonos item using the configuration.

Just take this code, save it out to a json file, and import it as a custom widget

{
    "template": "<h4>{{config.title}}</h4>\n\n<div class=\"btn-group\" uib-dropdown dropdown-append-to-body=\"true\"\n     ng-init=\"sonos_favorites = getItem(config.favorite_item).commandDescription.commandOptions\">\n  <button id=\"single-button\" type=\"button\" class=\"btn btn-primary\" uib-dropdown-toggle>\n    {{itemValue(config.favorite_item)}} <span class=\"caret\"></span>\n  </button>\n  <ul class=\"dropdown-menu\" uib-dropdown-menu role=\"menu\" aria-labelledby=\"single-button\">\n    <li role=\"menuitem\" ng-repeat=\"favorite in sonos_favorites\">\n      <a ng-if=\"$index != 0\" ng-click=\"sendCmd(config.favorite_item, favorite.command)\">{{favorite.command}}</a>\n    </li>\n  </ul>\n</div>",
    "name": "Sonos Favorites",
    "author": "Guido Strotmann / Mike Kolcun",
    "settings": [
        {
            "type": "string",
            "id": "title",
            "label": "Title",
            "default": "Sonos Favorites",
            "description": "The title that will be shown"
        },
        {
            "type": "item",
            "id": "favorite_item",
            "label": "Favorite Item",
            "description": "Select the favorite item for your sonos device"
        }
    ],
    "description": "Select and play a favorite item on a particular Sonos device"
}

Hi Mike, hanks for the info … very useful :slight_smile:

Hi @kolcun , you found the thread already :wink:

Unfortunately there is no explanation of the command…