I want to send a command to sonos to play my favorite

951 views
Skip to first unread message

pol...@hotmail.com

unread,
Nov 4, 2015, 9:08:57 AM11/4/15
to SoCo - Sonos Controller
I'm setting out to make using my Sonos a little simpler.

We have a zone that we share with the kids and they always have "their music" on it, whether it be a playlist or music service.  Whenever I want to play my Rhapsody Station 80's Rock I have to find a controller, or fire up an ipad, and take no less then 5 menus to get it playing.  This weekend I realized I could make that a "favorite", but it still requires me to find a controller, turn it on, enter the password, start the app, go to favorites and select 80's rock.

I want to use a Button.  Whether it be a wifi or Zeewave button.  Press it and it sends a command to sonos to play 80's rock at 75% volume.

I can probably get the button to trigger a windows command to fire off whatever I need, but first I need get a platform that can send a command and then I'll work on the triggering.

Can someone point me in the right direction.

I'd prefer to do it with windows, but I've got RPi's lying around and could do it with linux but I'm not a linus guru althrough I do have one running linux connecting my Amazon Alexa to my Vera Zeewave to automate (via voice) turning on and off lights).

So I've seen a few ideas about sending xml commands to sonos via web browser?

Please keep the acronims down, I've been on a few other sites that seemed to be very close to what I wanted but they were so filled with acronims that I didn't understand that I thought that I'd never be able to make it work.  I'm pretty good at figuring things out, but when I'm stuck in hard core land It's next to impossible.

Thanks.

Roveer

DPH

unread,
Nov 5, 2015, 3:12:41 AM11/5/15
to SoCo - Sonos Controller
Take a look at SOCO on github  https://github.com/SoCo/SoCo with documentation at http://docs.python-soco.com/en/latest/

This is a well developed python wrapper that runs on raspberry Pi or windows and allow you to programatically control your sonos system. 

I use it for a door bell (although building work has suspended use for a short while) - you can certainly do what you want - and the group is very helpful.

If you have a programming background , Python is great

Hope this helps - feel free to ask more questions
Good luck
Cheers David

Jim Bretschneider

unread,
Nov 27, 2015, 6:20:34 PM11/27/15
to SoCo - Sonos Controller
You can find a simple example on how to detect a pressed button in Python on a PI here http://razzpisampler.oreilly.com/ch07.html
If you are planning on hardcoding the sonos IP and the playlist the rest is likely just a few lines of code.

pol...@hotmail.com

unread,
Dec 1, 2015, 4:40:35 PM12/1/15
to SoCo - Sonos Controller
Thanks for the url  I'll be sure to look at it.  What I'm really after is that "few lines of code" that will actually get the favorite playing on my play1 speaker.  Yes, the ip would be hardcoded.  I want to play a specific favorite on a specific speaker so the IP will always be the same. It seems like SoCo is pretty powerful but I am unable to find a way to do this.  I have a favorite called 80H.  I think it was called 80's hard rock or something similar.  I simplified it so I could locate it in my sniffer traces etc.  I ran a wire shark sniffer trace on a laptop that had the sonos desktop controller installed and have only had time to take a quick look at it.  So far I can't see any references to the favorite name.  If I remember correctly sonos controllers talk via xml commands?  I do see a lot of text commands, and again, haven't had time to study it further.  

If anyone can help me in crafting this command I'd really appreciate it.  I'm not a coder so I have to learn this as I go.  I like these sorts of challenges.  Oh, I've got RPi's lying around just waiting to be put to good use.  One is currently running some bridge software between my Amazon Echo and my zwave devices.  That was a fun project.

Thanks.

Roveer

DPH

unread,
Dec 2, 2015, 4:10:54 AM12/2/15
to SoCo - Sonos Controller
Hi,
to play a favorite has some complexity as it could be anything - a track on your local server, a radio station, a streaming service etc. 

If all you want to do is play one stream then it is much easier.
This python code would do it (on PC or Raspberry Pi) - and it works in the UK on Napster which is a sub of Rhapsidy.

import soco
zp = soco.SoCo('192.168.1.76')  # your ip address of the Sonos unit

media_uri = 'rdradio:station:ps.54600900?sn=2'
media_metadata = '<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/" x...........removed middle bits...................om</desc></item></DIDL-Lite>'
zp.volume = 75
zp.play_uri(media_uri, media_metadata, start=True)


Your only challenge is to find the values for media_uri and media_metadata - these will be specific to your region.
To do this set the Sonos unit playing the station you want (it must not be in a group) and then run the code below once  - this will list the values you need:

from soco import SoCo
from soco.snapshot import Snapshot

zp = SoCo('192.168.1.76')  # your IP address

snap = Snapshot(zp)    #create snapshot class
snap.snapshot()           #take a snapshot of current state

print 'media_uri: ', snap.media_uri
print 'media_metadata: ', snap.media_metadata


This will only work for streams!

Hope this helps - shout if any problems.
Cheers David

pol...@hotmail.com

unread,
Dec 2, 2015, 9:54:50 AM12/2/15
to SoCo - Sonos Controller
Wow!!!  That's exactly what I'm looking for.  I will test and report back.  Thanks so much.

pol...@hotmail.com

unread,
Dec 5, 2015, 5:10:11 PM12/5/15
to SoCo - Sonos Controller
SUCCESS!!!

Now I can move on to getting the button functionality working.  Thanks a million.

Roveer




On Wednesday, December 2, 2015 at 4:10:54 AM UTC-5, DPH wrote:

pol...@hotmail.com

unread,
Dec 10, 2015, 11:41:33 PM12/10/15
to SoCo - Sonos Controller
I got it all to work!

I have a Vera Zwave system in my house so a logical choice for a "button" so I got a Aeon Minimote which is a 4 button zwave device.  So here's how it works.

You gave me the python soco's code to get the speaker playing my 80's Hits radio station.  to trigger that script I use a program on windows called eventghost.  It fronts a small webserver on the windows PC that accepts commands and then triggers events (in my case the python script to start the sonos speaker).  In order to trigger that script I use luup code on my Vera which takes the button push and sends a message to the webserver.  Now that was all backwards, so let me run it front to back.

Press the Zwave button on the wireless remote.  That tells the Vera zwave hub to execute a piece of luup code which sends a TCP request to a url (my kitchen PC, which runs sql and bunch of other things in the background, always on).  Evengthost watches that port (85) for messages and when it receives them it triggers events on the PC, which in my case is the python script to trigger the sonos speaker on the radio station.  Works perfectly, and fast too.

Quite a complicated set of linkages but once its all in place it works like a dream AND I've 3 other buttons to put cool stuff on.  I'm thinking 3 other radio stations.

Thanks for the help with the soco script.  That's what pulled it all together.

Roveer

Reply all
Reply to author
Forward
0 new messages