HOW-TO: When you get home start play random song on your Sonos from a playlist

I’ve been trying to achieve a simple thing: when I get home, I want my entrance door sensor to run a scene that among other things, starts playing music on my Sonos speaker. This seems to be easy but I found some problems:

1st - I have my Sonos device turned its power off when I’m away. Coming home and turning the power on means there is a roughly 45 second delay so that it boots and only after that I can start playing something
2nd - Vera keeps seing my Sonos offline after I restore its power, so I had to for a status update
3rd - I don’t want to get home and always get the same music, and playing a random music was a lot harder than I expected

Below you will find code to do all of this. I’m still a newbie with Vera, Sonos, and LUA programming. This forum is a terrific source of information, and I’m learning a lot every day, so I thought it was my turn to return something to the community.

Enjoy the code and please feel free to give your sugestions for improvements!

[code]-- Luup code to start playing a Sonos playlist when my entrance sensor is armed and detects movement
– This should be enhanced with time of day, day of week validations, etc

function Musica()
– Constants to be used, maybe you can also add play volume. Remember to change device number, playlist name and IP address of your Sonos speaker
local SONOS_DEV = 65
local SONOS_SID = “urn:micasaverde-com:serviceId:Sonos1”
local SONOS_AVT = “urn:upnp-org:serviceId:AVTransport”
local PLAYLIST_URI = “SQ:SlowsMetal”
local SONOS_URL = “http://192.168.1.10:1400/xml/device_description.xml

-- Forces the Sonos status to update to become online after power up
luup.call_action(SONOS_SID,"SelectSonosDevice", {URL=SONOS_URL},SONOS_DEV)
-- Gives 3 seconds for the update to happen, maybe this can be shorter
luup.sleep(3000)
-- Defines the playlist to be played
luup.call_action(SONOS_SID, "SetURIToPlay",  {URIToPlay=PLAYLIST_URI}, SONOS_DEV)
-- Sets playmode to random and repeat
luup.call_action(SONOS_AVT, "SetPlayMode", {InstanceID="0", NewPlayMode="SHUFFLE"}, SONOS_DEV)
-- Moves to next song so that the first played in not always the same song
luup.call_action(SONOS_AVT, "Next", {InstanceID="0"}, SONOS_DEV)
-- Finally starts playing
luup.call_action(SONOS_AVT, "Play", {InstanceID="0"}, SONOS_DEV)

end

– This is the device Id of my sonos power switch
local SONOS_SWITCH = 28
local SWITCH_SID = “urn:upnp-org:serviceId:SwitchPower1”

– Turn on power to my Sonos speaker
luup.call_action(SWITCH_SID, “SetTarget”, {newTargetValue = “1”}, SONOS_SWITCH)
– Calls Musica() function after a 50 second delay so that the Sonos can boot.
luup.call_delay(“Musica”, 50, “”)
return true
[/code]

Thank you! I have been looking for the shuffle & repeat code for quite a while. Works great, did add volume variable, which is nice so can adjust from tv room to baby room and just adjust in the variables rather than in the function’s code (hence the point of a function at all, right? :wink: )

Thanks!

Thanks @fleetmack! I was wondering if this was not useful at all and it was just me who wanted it :slight_smile: Glad to know you are using that code.

Thanks so much for this. Works great,

I am running on VeraPlus on UI7

Only four changes to make to make this run

local SONOS_DEV = 65 <<< In Advance settings Device IP number of your Sonos Plugin

local PLAYLIST_URI = “SQ:Name of Your Playlist in Sonos”

local SONOS_URL = “http://192.168.0.12:1400/xml/device_description.xml” <<< change ip to IP of your Sonos

local SONOS_SWITCH = 28 << change number to Device Number of your on/off

I intend to operate much as you describe, when arrive Home and switch OFF alarm, the player starts. As I am awaiting an ON/OFF socket, I commented out the line regarding SONOS_SWITCH =

Tried manually running the scene, nothing happened. Added in switch a device already in place and run perfectly. Is it a requirement that a scene operates a physical device ?

I hope I have described accurately the changes required.

Again many thanks

johnmcc, if I understood you correctly, you are not using (yet) an on/off switch to power on/off your sonos player, correct? That means the Sonos player is always turned on, right?

If the player is always turn on, my script should run with no problem. If you somehow turn on your Sonos player after coming home, then you must give it sometime to boot before it can be discovered and starts receiving commands.

If you can detailed a bit more of your setup maybe I will be able to help. :slight_smile:

Take care!

Santos,
You are correct don’t have an ON/OFF switch, awaiting arrival. To test I could modify to suit my setup, I commented out the power on function.
So my “Test Scene” did not operate a physical on/off switch. So I set the scene to switch on the entry porch light. The scene then functioned correctly.

Have been reading through your help file, to setup to switch to a “Radio Station”, have only just obtained a Sonos Play 3, so I am learning both Sonos nd your excellent plugin. :slight_smile: :slight_smile:

johnmcc, first let me tell you that the Sonos plugin is not mine. It is a great plugin and the author is lolodomo.

back to my code, if you comment just the line
luup.call_action(SWITCH_SID, “SetTarget”, {newTargetValue = “1”}, SONOS_SWITCH)
it should work without any problems, as long as your Sonos player is always powered on (and has a fixed ip address).

However, be cautious when you turn off/on your player. Your Sonos:3 may take more time to boot than my Sonos:1 used for testing. I call the luup_delay function with a 50 second delay after measuring the time it took to boot. You may need more (or less) time depending on your setup, so please adjust it accordingly.

Again, if you need further help, I’ll be around :slight_smile:

Thanks for the replies, thanks for the update on extending the time. I manually powered up Sonos 3, but I shall give it more time.