Need help with luup for SONOS-morning rutine.

Hi there.

In an atempt to do all me homeautomation in Vera instead of doing it in alot of diferent apps. I have come to my Sonos morning-rutine.

The rutine i a bid more complex than just turning lights on and of on a timer, so i think I nedd to use some luup-code. Unfortunately I do not now how to code in Luup!

I hope that some of you micht be able and willing to help me out.

So here we go - this is my sceduled Sonos morning rutine:

Schedule - Monday thru Friday
Modes - HOME + Night
Sonos-zones - Bathroom - Kitchen
Music source - TuneIn Radio (Chanel “P1” and “P7”)
using 24h - not am/pm

At 0630 turn on “bathroom” level x, source “TuneInradio”, chanel “P1”
At 0640 group “bathroom” and “kitchen”, set volume to Y in “kithcen”
At 0710 ungroup “bathroom” and Kitchen"
At 0710 turnoff “bathroom”
At 0710 set “kitchen” to play “TuneIn radio” chanel “P7”,
At 0740 turnoff “Kitchen”

Hope it is relatively clear where i am going.

Regards Hoekeren

The easiest ist a normal scene I think , you have to use the advanced setup for scene. if you select in the advanced tab sonos , you can select there the more advanced options of sonos. I noticed there need some time between actions.

Also PLEG is a good option to do this, I use both for a similar setup as you and works fine.

Cor

@Cor, is it possible to share your PLEG example with a copy of your report? It helps those of us that are still learning the ropes ;D Mike

In addition to seeing examples, I have a PLEG device devoted to playing - getting device information, playing with triggers, conditions, logic, etc. If I am stuck, I disable it until I had time to mess with it some more.

@Don Phillips, is possible to share your PLEG example by attaching a copy of your report? I’m finding with the combination of PLEG and LUA snippets that there are some sophisticated solutions that folks are utilizing e.g. PLEG, SONO, GCalII ect… Mike

Here is my main, most complicated PLEG. Since changing the USNAP module on my CT30, I cannot get accurate fan status, otherwise, it is working well. Two others are very simple, single purpose setups not worth sharing, and the 4th is an empty test PLEG.

@Don Phillips, many thanxs for sharing… I will have to study in more detail, I found it interesting that you had no triggers and primarily used the schedule and properties for you conditions. Mike

It’s not very elegant, and I am sure it could be better written, but my little pea brain understands it.

Elegance is in the eye of the beholder… I agree I’m finding keep it simple seems to work more than not. Mike

@Hoekeren, is seems that we hijacked your original request were able to solve your problem? If not does someone have a PLEG example? Mike

Nope - I am stil on bare ground!

A snip of LUA would be great. I am confident the there is a helpfull soul out there. ;D

@hoekeren, I have a LUA snip-it you might be able to modify for your purposes.

[code]local AV_DEV = 15 – Sonos Device ID within Vera
local LS_SID = “urn:micasaverde-com:serviceId:Sonos1”
local LS_ZONES =“master bedroom” – Valid GroupZones = “All”, “Kitchen”, “Patio”, “Living Room”, “Master Bedroom”
local WEATHER_ID = 5
local WEATHER_SID = “urn:upnp-micasaverde-com:serviceId:Weather1”

local time = os.date(‘It is %H:%M.’)
local weatherCurrCondition = luup.variable_get(WEATHER_SID, “Condition”, WEATHER_ID)
local weatherForecast = luup.variable_get(WEATHER_SID, “Forecast.0.Condition”, WEATHER_ID)
local weatherLow = luup.variable_get(WEATHER_SID, “Forecast.0.LowTemperature”, WEATHER_ID)
local weatherHigh = luup.variable_get(WEATHER_SID, “Forecast.0.HighTemperature”, WEATHER_ID)
local weather = “The current weather is " … weatherCurrCondition … “. The forecast for today is " … weatherForecast … " with a minimum of " … weatherLow … " and a maximum of " … weatherHigh
local hour=tonumber(os.date(‘%H’))
local greeting=””

if hour < 12 then
greeting = “Good morning”
elseif hour < 17 then
greeting = “Good afternoon”
elseif hour < 21 then
greeting = “Good evening”
else
greeting = “Good night”
end

luup.call_action(LS_SID, “Say”, {Text= greeting … " Hello, excuse me, " … greeting … " Mike. " … time … weather … “. Goodbye.”, Language=“en”, GroupZones=LS_ZONES, Volume=20, SameVolumeForAll=“true”}, AV_DEV)

– function pandoraOn()
luup.call_action(SONOS_SID, “PlayURI”, {URIToPlay=“SF:Enya Radio”, GroupZones=“ALL”, SameVolumeForAll=“true”, Volume=20}, 127)
end
luup.call_delay(“pandoraOn”,60)

return true[/code]

[quote=“MNB, post:12, topic:196015”]@hoekeren, I have a LUA snip-it you might be able to modify for your purposes.

[code]local AV_DEV = 15 – Sonos Device ID within Vera
local LS_SID = “urn:micasaverde-com:serviceId:Sonos1”
local LS_ZONES =“master bedroom” – Valid GroupZones = “All”, “Kitchen”, “Patio”, “Living Room”, “Master Bedroom”
local WEATHER_ID = 5
local WEATHER_SID = “urn:upnp-micasaverde-com:serviceId:Weather1”

local time = os.date(‘It is %H:%M.’)
local weatherCurrCondition = luup.variable_get(WEATHER_SID, “Condition”, WEATHER_ID)
local weatherForecast = luup.variable_get(WEATHER_SID, “Forecast.0.Condition”, WEATHER_ID)
local weatherLow = luup.variable_get(WEATHER_SID, “Forecast.0.LowTemperature”, WEATHER_ID)
local weatherHigh = luup.variable_get(WEATHER_SID, “Forecast.0.HighTemperature”, WEATHER_ID)
local weather = “The current weather is " … weatherCurrCondition … “. The forecast for today is " … weatherForecast … " with a minimum of " … weatherLow … " and a maximum of " … weatherHigh
local hour=tonumber(os.date(‘%H’))
local greeting=””

if hour < 12 then
greeting = “Good morning”
elseif hour < 17 then
greeting = “Good afternoon”
elseif hour < 21 then
greeting = “Good evening”
else
greeting = “Good night”
end

luup.call_action(LS_SID, “Say”, {Text= greeting … " Hello, excuse me, " … greeting … " Mike. " … time … weather … “. Goodbye.”, Language=“en”, GroupZones=LS_ZONES, Volume=20, SameVolumeForAll=“true”}, AV_DEV)

– function pandoraOn()
luup.call_action(SONOS_SID, “PlayURI”, {URIToPlay=“SF:Enya Radio”, GroupZones=“ALL”, SameVolumeForAll=“true”, Volume=20}, 127)
end
luup.call_delay(“pandoraOn”,60)

return true[/code][/quote]

GREATE - Ill give it at go at get back to you all.

:slight_smile:

@ MNB: The sonos , I do with scenes. very straight forward . But you can also do them in PLEG , I understood they would have no issues with delays during a Luup restart.

Cor

@Cor, as you stated the mechanism to execute the LUA snipit can be either via scenes or PLEG. I personally have been gravitating to PLEG of late (It’s hard to teach an Old dog new tricks! but… ::slight_smile: ) I find that it is relatively easy to set-up conditions/logic that scenes do not always facilitate (kudos to RTS) . As to your question I see no performance delay using either scene or PLEG. Do I presume that your translating this? Mike

Like I said , I use scenes as well for my sonos “morning routine”. But another option is using PLEG. I understand that when there is a LUUP restart in the middle of a scene the delayed action will not be executed. ( For example , time 0: Sonos on, TIme +10 seconds: this channel, Time + 10 minutes: change channel. If in those 10 minutes there is a Luup restart , the change channel will not happen.) PLEG doesn’t have this “issue”.

Cor

Hi.
Firstly a thanks to MNB for sharing some of his code.

I just want to share what I have created for a morning routine. With language localication because my son at 7 don’t understand what the lady in the bathroom says :slight_smile:

There might be better ways to do all the if statements but it works for me

local AV_DEV =49-- Sonos Device ID within Vera
local LS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local LS_ZONES ="All" -- Valid GroupZones = "All", "Kitchen", "Patio", "Living Room", "Master Bedroom"
local WEATHER_ID = 34
local WEATHER_SID = "urn:upnp-micasaverde-com:serviceId:Weather1"
local TEMP_ID = 35
local TEMP_SID = "urn:upnp-org:serviceId:TemperatureSensor1"

local time = os.date('klokken er %H:%M.')
local weatherCurrCondition = luup.variable_get(WEATHER_SID, "Condition", WEATHER_ID)
local weatherForecast           = luup.variable_get(WEATHER_SID, "Forecast.0.Condition", WEATHER_ID)
local weatherLow                  = luup.variable_get(WEATHER_SID, "Forecast.0.LowTemperature", WEATHER_ID)
local weatherHigh                 = luup.variable_get(WEATHER_SID, "Forecast.0.HighTemperature", WEATHER_ID)
local outsideTemp = luup.variable_get(TEMP_SID, "CurrentTemperature", TEMP_ID)
outsideTemp=tostring(outsideTemp)


local hour=tonumber(os.date('%H'))
local greeting=""
if  hour < 12  then
    greeting = "God morgen"
elseif hour < 17 then
    greeting = "God dag"
elseif hour < 21 then
    greeting = "God aften"
else
    greeting = "Goo nat"
end

if (weatherCurrCondition=="Partly Cloudy") then
	weatherCurrCondition="Delvist overskyet"
elseif (weatherCurrCondition=="Clear") then
	weatherCurrCondition="klart vejr"
elseif  (weatherCurrCondition=="Partly Sunny")  then
	weatherCurrCondition="delvist solrigt"
elseif  (weatherCurrCondition=="Rain") then
	weatherCurrCondition="regnvejr"
elseif  (weatherCurrCondition=="Cloudy") then
	weatherCurrCondition="overskyet"	
elseif  (weatherCurrCondition=="Flurries") then
	weatherCurrCondition="Snevejr"		
elseif  (weatherCurrCondition=="Fog") then
	weatherCurrCondition="t?get"
elseif  (weatherCurrCondition=="Fog") then
	weatherCurrCondition="t?get"
elseif  (weatherCurrCondition=="Sunny") then
	weatherCurrCondition="Solskind"
elseif  (weatherCurrCondition=="Mostly Cloudy") then
	weatherCurrCondition="Mest overskyet"
end	

if (weatherForecast=="Partly Cloudy") then
	weatherForecast="Delvist overskyet"
elseif (weatherForecast=="Clear") then
	weatherForecast="klart vejr"
elseif  (weatherForecast=="Partly Sunny") then
	weatherForecast="delvist solrigt"
elseif  (weatherForecast=="Rain") then
	weatherForecast="regnvejr"
elseif  (weatherForecast=="Cloudy") then
	weatherForecast="overskyet"	
elseif  (weatherForecast=="Flurries") then
	weatherForecast="Snevejr"		
elseif  (weatherForecast=="Fog") then
	weatherForecast="t?get"
elseif  (weatherForecast=="Fog") then
	weatherForecast="t?get"
elseif  (weatherForecast=="Sunny") then
	weatherForecast="Solskind"
elseif  (weatherForecast=="Mostly Cloudy") then
	weatherForecast="Mest overskyet"
end	
	

local weather1 = "Det aktuelle vejr er " .. weatherCurrCondition .. ". Temperaturen er " .. outsideTemp .. "grader"
local weather2 = "Vejret i dag bliver" ..  weatherForecast .. ".Temperaturen bliver mellem " .. weatherLow  .. " og " .. weatherHigh .."grader"

	
luup.call_action(LS_SID, "Say", {Text= greeting .. ". " .. time .. weather1, Language="da", Volume=30}, AV_DEV)
luup.call_action(LS_SID, "Say", {Text= weather2, Language="da", Volume=30}, AV_DEV)

It says something like.
Good morning, the time is 7:09 the actual weather is “mostly cloudy”. The temperature is 6.8 degrees.
The weather today will be “Clear” The temperature will be between 7 to 9 degrees.

I have split up the text in two because it made a strange pause if the text gets to long.

I havent used all the possible conditions but want to add them when they happen.

You can find a full list of possible conditions here
[url=https://www.wunderground.com/weather/api/d/docs?d=resources/phrase-glossary&MR=1&_ga=1.42524774.278975447.1492550212]Loading | Weather Underground

Hope that someone want to “localice” it as well.

Best regards,
Jens

@jensk, many thanxs for sharing… I wish more folks out there would also start sharing there solution(s) and modifications as it makes the job for nubies a little easier to incorporate into their home automation. I like your idea of splitting the long text into two statements, I have an hourly gong with time announcement that I like the way it announces which is a little better than the current morning announcement, just didn’t have enough spare time to splice it in :o, Mike

@jensk after review of your LUA snipit, I’ve noticed that you have the weather forecast translation (English to Danish) lines 30-50 of the “If” loop has been duplicated (see lines 52-72) is there a reason? Also I noticed that you have listed “Fog” twice and the translation has “t?get” I assume that it is should be “Mist” not that I speak Danish ???. Mike

Hi.
Yes, there is a reason. Its two different variables. One is for the current weather and the other one is for the forecast.
I have noticed the “Fog” thing and will remove it.
Its not Dutch its Danish and the ? in the t?get is instead of a Danish char.

Thanks for the review :slight_smile:

Best regards,
Jens