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]