Sonos playlists etc?

yessir

Here are some screenshots. I have it working with Radio, Apple Music and Spotify.

The process I followed was to start a stream to the speaker of choice. Then when I created the preset I flipped the "Recently Played URI's" and copy and pasted that into the URI: of the preset.

1 Like

Sorry @Dunginhawk, been a really crazy day at work. Did you get this working or do you still need help?

Ok, so I dont see preset control or child... are those drivers or apps code? I only see sonos.

In case you do, here's a quick overview.

  1. Select your sonos speaker
  2. Configure Presets
    This is the screen you are presented with

Number of presets:
This will dynamically create the input fields used to create each preset for this particular speaker. I limited it 6 because anything more than that seemed to cause the hub to crawl for a bit. This was almost a year ago so I may be able to increase this number. I haven't ever needed more than 5 presets for a speaker so I left it at 6.

Preset Name:
This is the name of the station/preset. It will be the name of the Virtual Switch created for this preset so name it accordingly...especially if you plan to use this with a voice assistant like Alexa.

URI:
This is the URI of the preset/station/playlist you want to start when the preset switch is turned on. If you already have one saved you can just paste it here and off you go. In most cases you would need to get the URI. This is where ST and HE strayed in their Sonos implementations. In ST This was easily extracted and fairly consistent. With HE different uri types were needed based on what was being played.eg certain radio stations needed, a certain uri (or portion of a uri to work correctly. It got confusing fast. In any case I created an option that would do it's best to get you the correct uri and also give you other probable options.

Show Recently Played URI's:
Enable this to display the URI of the most recently played audio on the speaker chosen. If this is blank, then start playback of one of your favorite stations and hit the Refresh Recent List button. You should end up with a list like this:

You can then copy and paste any uri of your choosing into the Test URI input and click the Test button. If it plays the station as expected, then this is the uri you need. Just copy it into the URI field of the preset. Rinse and repeat for as many presets as needed (up to 6 max) and save.
For some stations/playlists you might get more than one option. If the first uri you test does not work try the next in the list. I tried to gather the best options for the different playlists and present them in the Recents List.

When you save give it time to complete. It is actually generating a Virtual container device and then creating up to 6 virtual switches within that container. It might takes a few seconds to complete.

When all is said and done you should see a container device in your device list, with all your presets list under it. You can add these switches to a dashboard or assign to a button device or turn on via voice assistant. The switch has an autoOff so it is essentially a momentary button. There is no off option just different button to load different stations..kinds like presets on your car radio.

3 Likes

I still use @stephack's app, but if all you want is to play a track from a tile it's easy to set up in RM 4. Get the Sonos URI from the Sonos device (in the same manner as for @stephack's app). Set up and use a virtual button as the RM trigger and then use a custom action to play the track. Below is an example using a virtual button:

Did you install the code that I linked from Github. You need to install 2 apps (one parent one child) and one driver (virtual container).

After installing the code you would need to go to Apps and click the Add User App button. You should see Sonos Preset Controller in the list.

Yeah i did all that, just forgot to add the user app after... Duh :slight_smile:
So I have one of the URIs testing and seems to be working ok. Then i create a virtual device as a virtual container. Then just create a dashboard item with that virtual container device. Clicking it does nothing though.. Something is amiss, I need to investigate.

@bill.d really all i need/want is to play playlists from spotify through sonos with a button push... not an artist per sae, but mostly playlists. i assume playTrack can be changed to playPlaylist with a sonos URI?

The container and virtual switch are created automatically after you save the app. You don't have to manually create them. If you have manually created the container, delete them and look for the container that was automatically created by the app. Those switches should work.

ah thats where I went wrong..
So I am noticing that some of the Playlists etc work others dont seem to.
For instance a Pandora Station for Flyleaf works, but a Spotify for Eric Church does not.
Your integration is great. Just click and go, and if you want to change volume just pop open the player in a dashboard or something.
thank you for doing it

1 Like

Yeah...that's finicky part I mentioned before. Different audio options need different uri's. It made porting my original ST app impossible. I don't have Pandora so I can't really test it to see what uri it needs. You may have more than one uri in the recent list you could try.

The action is still playTrack, but you have to find the right URI. Start the playlist in Spotify, playing on the Sonos. Go to the Sonos device in Hubitat and look for URIs. The Spotify URI will start with x-rincon-queue. But for whatever reason it doesn't always work. You may need to start, stop, change the track, repeat, in some kabuki fashion to get it to work.

@Dunginhawk, I attempted to setup Spotify on my Sonos today but unfortunately I need a premium account just to add it. If you are still interested in using my app let me know and I could work with you to get the correct uri to display in the recent list.

Hopefully you got one of the suggested solutions to work for you by now. If not, shoot me a PM and I'll get you sorted.

@stephack If you need any info, let me know. My post above shows a Spotify playlist that I pulled from the Sonos speaker.

Does that uri also show in the Recents list of the app or did you extract it manually from the Sonos speaker attribute?

If manual, where did it show in the speaker device...transporturi, enqueueuri,etc

Upon an hour or so of trying:

  1. URIs are different based on the source. Playing from the Sonos app, casting from the Spotify app, or using the node.js app (I don't have that app, but based on @homeauto2112 screen shot) all give different URI values.
  2. Neither the Sonos nor Spotify apps pass usable playlist URIs to HE. URIs are valid, but at best only activate the previous playing track. And they seem to be temporary.
  3. It appears that, based on @homeauto2112 post above, the only way to get a usable URI for a Spotify radio station is to use the node.js app. Where the URI would start with x-sonos-api.

With a bit of logging info I "may" be able to extract a usable uri from the Sonos speaker logs. It's what I had to do to get usable uri's for certain Radio Streams. It's a real PITA and the main reason I have not released this app. The moment I post this in the forums I will be swamped with specific playlist type issues. For instance I may get this working for Spotify "playlists" but it might require a different setup for Spotify "stations". It is especially troublesome with HE because of how it logs the different uri's.

If Spotify didn't require a premium account just to add it to Sonos, I could play around and potentially provide a best guess in the recent list. As it is, it would require trial and error and someone willing to send me detailed logging from their speakers.

@homeauto2112, what type of music were you playing to get the uri and where exactly did you get extract it from.

@stephack that was my Miles (as in Miles Davis) playlist.

@bill.d I got it from the state variables of my Kitchen Sonos in HE. I loaded the Built In Sonos app and then I used the Sonos app on my Mac to pick what I wanted to listen to. I lifted the URI after the music started playing and I refreshed the webpage on the device.

You can use the API to set presets and then an HTTP Get to access them (http://IP address:5005/preset/nameofpreset)

@homeauto2112 What specific variable did you get it from ....a uri, a subset of the TrackData?