We have moved at community.getvera.com

Author Topic: Need help with luup for SONOS-morning rutine.  (Read 2968 times)

Offline Cor

  • Hero Member
  • *****
  • Posts: 1249
  • Karma: +8/-4
Re: Need help with luup for SONOS-morning rutine.
« Reply #15 on: April 11, 2017, 04:56:41 pm »
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

Offline jensk

  • Sr. Newbie
  • *
  • Posts: 29
  • Karma: +1/-0
Re: Need help with luup for SONOS-morning rutine.
« Reply #16 on: April 20, 2017, 05:32:08 pm »
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 :)

There might be better ways to do all the if statements but it works for me
Code: [Select]
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
https://www.wunderground.com/weather/api/d/docs?d=resources/phrase-glossary&MR=1&_ga=1.42524774.278975447.1492550212

Hope that someone want to "localice" it as well.

Best regards,
Jens

Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Need help with luup for SONOS-morning rutine.
« Reply #17 on: April 20, 2017, 07:00:11 pm »
@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
Edge-1.7.2607, VRI06-1LX Dimr (6), VS05-1LX Sw (4), GE ZW3002 Dimr (1), HA01C Outlet (12), HA02C Sw (6), HA04C Sw (4), GE Sw (2), HA05 Sw (3), MiniMote Contr (5), HSM200/100 Sensor (2), ZW100A Sensor (8), Nest thermostat (1) & Protect Fire/CO (6) Kwikset Locks (3), F18918W (1) & IP3M-9 (6) Cam's

Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Need help with luup for SONOS-morning rutine.
« Reply #18 on: April 21, 2017, 10:28:22 am »
@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
« Last Edit: April 21, 2017, 10:34:16 am by MNB »
Edge-1.7.2607, VRI06-1LX Dimr (6), VS05-1LX Sw (4), GE ZW3002 Dimr (1), HA01C Outlet (12), HA02C Sw (6), HA04C Sw (4), GE Sw (2), HA05 Sw (3), MiniMote Contr (5), HSM200/100 Sensor (2), ZW100A Sensor (8), Nest thermostat (1) & Protect Fire/CO (6) Kwikset Locks (3), F18918W (1) & IP3M-9 (6) Cam's

Offline jensk

  • Sr. Newbie
  • *
  • Posts: 29
  • Karma: +1/-0
Re: Need help with luup for SONOS-morning rutine.
« Reply #19 on: April 21, 2017, 10:51:14 am »
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 :)

Best regards,
Jens

Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Need help with luup for SONOS-morning rutine.
« Reply #20 on: April 21, 2017, 02:31:52 pm »
Jens, thanxs for the explanation it would help if one (me) would look a little harder  ::) Yea I realized my error whilst doing google translate (Danish vs Dutch ) and made the corrected in my original post. Mike
Edge-1.7.2607, VRI06-1LX Dimr (6), VS05-1LX Sw (4), GE ZW3002 Dimr (1), HA01C Outlet (12), HA02C Sw (6), HA04C Sw (4), GE Sw (2), HA05 Sw (3), MiniMote Contr (5), HSM200/100 Sensor (2), ZW100A Sensor (8), Nest thermostat (1) & Protect Fire/CO (6) Kwikset Locks (3), F18918W (1) & IP3M-9 (6) Cam's

Offline jensk

  • Sr. Newbie
  • *
  • Posts: 29
  • Karma: +1/-0
Re: Need help with luup for SONOS-morning rutine.
« Reply #21 on: April 21, 2017, 02:56:02 pm »
There are two improvements that can be done. I will look into it later tonight or tomorrow.

1. A procedure can be made with the if's so I dont have to dublicate them.
2. The current temperature like 6.7 is "translated" to 6 7 when used by the TTS.
 I have found the solution in this post http://forum.micasaverde.com/index.php/topic,48324.0.html
I just need to implement it.

I will post an update when I have made it.

Best regards,
Jens

Offline jensk

  • Sr. Newbie
  • *
  • Posts: 29
  • Karma: +1/-0
Re: Need help with luup for SONOS-morning rutine.
« Reply #22 on: April 21, 2017, 04:17:57 pm »
Hi.
Now with a updated version

1. Function for making the temperature with no decimal
2. Function for the if's
3. Removed the dual "fog"

Code: [Select]
local AV_DEV =55 -- 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)

local function convertTextInteger(val)
    return (("%+0.0f"): format (val): gsub("^.", "%1 "))
end

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

outsideTemp=convertTextInteger(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

weatherCurrCondition=translateCondition(weatherCurrCondition)
weatherForecast=translateCondition(weatherForecast)

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)

Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Need help with luup for SONOS-morning rutine.
« Reply #23 on: April 21, 2017, 04:32:22 pm »
Jens, That was quick..

I attempted to run your latest by changing the AV_DEV, Weather_ID and TEMP_ID values to reflect mine. when I run the LUA I can see the command being issued (via PLEG) but no sound or error being reported when I run your LUA. I have verified that I'm able to get TTS sounds through the specific SONOS speaker via testing with another working LUA code.

What version of Firmware and GUI are you using? I'm using Edge with GUI UI7, version 1.7.2607, Mike

PS fix in your greeting "Goo nat" to "God nat" I think your having it say Good Night?
« Last Edit: April 21, 2017, 04:41:57 pm by MNB »
Edge-1.7.2607, VRI06-1LX Dimr (6), VS05-1LX Sw (4), GE ZW3002 Dimr (1), HA01C Outlet (12), HA02C Sw (6), HA04C Sw (4), GE Sw (2), HA05 Sw (3), MiniMote Contr (5), HSM200/100 Sensor (2), ZW100A Sensor (8), Nest thermostat (1) & Protect Fire/CO (6) Kwikset Locks (3), F18918W (1) & IP3M-9 (6) Cam's

Offline jensk

  • Sr. Newbie
  • *
  • Posts: 29
  • Karma: +1/-0
Re: Need help with luup for SONOS-morning rutine.
« Reply #24 on: April 21, 2017, 05:07:05 pm »
Like any "good" developer will say...... It works on my machine :)

I'm running UI7 1.7.2608

I dont think its that. I have seen this before I think it when one of the variables are empty. I don't know anything about PLEG so I cant help you there.
Try run in here Apps->Develop Apps->Test Luup Code (LUA) that way we can rule out PLEG.

Be aware that the WeatherID and the TEMP_ID is two different devices. I think you already have figured that out but just to be sure.

If it still not working I would put in a say command different places in the code to see where it fails. It may not be the best way to debug code but its the only way I know :)

Best regards,
Jens


Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Need help with luup for SONOS-morning rutine.
« Reply #25 on: April 21, 2017, 05:47:53 pm »
Well I took the LUA snipit and ran via the Apps->Develop Apps->Test Luup Code (LUA) and received the following;
"Success, Code sent successfully." but I've had this kind of results in the past where the Test Luup Code route doesn't flag everything.

WeatherID and the TEMP_ID is two different devices.  I assigned the parent World Weather Device ID in my case #5 to WeatherID and World Weather Child Device ID in my case #6 to TEMP_ID which is the same as my original LUA snipit.

You make note of the empty variables which ones are your referring to?
Edge-1.7.2607, VRI06-1LX Dimr (6), VS05-1LX Sw (4), GE ZW3002 Dimr (1), HA01C Outlet (12), HA02C Sw (6), HA04C Sw (4), GE Sw (2), HA05 Sw (3), MiniMote Contr (5), HSM200/100 Sensor (2), ZW100A Sensor (8), Nest thermostat (1) & Protect Fire/CO (6) Kwikset Locks (3), F18918W (1) & IP3M-9 (6) Cam's