Author Topic: Text To Speech (TTS) With Sonos - Creating Dynamic Messages  (Read 80681 times)

Offline michelhamelin

  • Full Member
  • ***
  • Posts: 150
  • Karma: +2/-0
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #210 on: March 20, 2016, 01:48:01 pm »
And if i want Sonos to check and Say only battery level what i have to change?


Sure here you go:

-- Door/Window sensor check and speech annotation by Pasqual Zottola
local vol = 60
local son = 23
local devcnt = 0
local devno = 0
local numopen = 0
local isare = "is"
local opensensors = " "
luup.log('Commencing sensor device search loop')
for deviceNo,d in pairs(luup.devices) do
   if d.category_num == 4 and d.subcategory_num == 1 then
           
      local tripped = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", deviceNo) or "Nil"
     
      if (tripped == "1") then
         opensensors = opensensors .. ". " .. d.description .. ","
         numopen = numopen + 1
      end
   luup.log(d.description .. ' status:' .. tripped .. ' Dev #' .. deviceNo .. ' id:' .. d.id ..  ' cat #:' .. d.category_num .. ' type:' .. d.device_type)     
   devcnt = devcnt + 1
    end
end
if numopen == 0 then   --If there are no sensors tripped, log and announce!
   luup.log('There were ' .. devcnt .. ' door and window sensors found and all are closed')
   luup.call_action("urn:micasaverde-com:serviceId:Sonos1", "Say", {Text=devcnt .. " sensors were checked, and all are closed. House is secure. Goodbye!", Language="en", Volume=vol}, son)
   
else   --If there are any sensors tripped, log and announce!
   if numopen > 1 then
      isare = "are"
   end
      luup.log('There were ' .. devcnt .. ' door and window sensors found and ' .. numopen .. " " .. isare .. ' open' .. opensensors)
      luup.call_action("urn:micasaverde-com:serviceId:Sonos1", "Say", {Text="Warning! House may not be secure," .. devcnt .. " sensors were checked and " .. numopen .. " " .. isare .. " open. Check," .. opensensors, Language="en", GroupDevices="32", Volume=vol}, son)
     
end
« Last Edit: March 20, 2016, 01:52:19 pm by michelhamelin »

Offline notchy

  • Jr. Member
  • **
  • Posts: 50
  • Karma: +0/-1
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #211 on: May 28, 2016, 01:08:13 pm »
Another new one for anyone who is interested..

Sonos Period Of The Day Greetings

This one will check the time of day and then great you with either Good Morning, Good Afternoon or Good Evening

Code: [Select]
url = require("socket.url")
local t = os.date('*t')

local AV_DEV = 5
local LS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local current_second = t.hour * 3600 + t.min * 60 -- number of sec since midnight
local morning = 0 * 3600 +  0 * 60             -- 00:00
local afternoon = 12 * 3600 +  0 * 60       -- 12:00
local evening = 18 * 3600 +  0 * 60             -- 18:00
local name = "Chris"

   if (current_second > morning) and (current_second < afternoon) then
        luup.call_action(LS_SID, "Say", {Text = string.format("Good Morning %s ", name)}, AV_DEV)
   else
     
        if (current_second > afternoon) and (current_second < evening) then
             luup.call_action(LS_SID, "Say", {Text = string.format("Good Afternoon %s ", name)}, AV_DEV)
        else

             luup.call_action(LS_SID, "Say", {Text = string.format("Good Evening %s ", name)}, AV_DEV)
        end
   end

 Hi everyone, I know this is an old  thread but I am new to Veraplus and I wanted to get this code working with my sonos.
I have the Microsoft TTS setup and working as I tested this but when I copy the above code and change out my Sonos ID , this does not execute when i run it.
Can somone tell me where they are applying this code to get it read aloud?

Thanks

Offline kiethr

  • Full Member
  • ***
  • Posts: 108
  • Karma: +1/-2
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #212 on: June 28, 2016, 09:27:12 pm »
I am hoping someone that could help me out is still reading this thread.  I have been using Becky's code for sometime.  I recently upgraded from a vera 3 on UI5 to a vera plus on UI7.   When i added the scene to my new vera it doesn't work  I  put the code in the test area ofthe vera plus and it states CODE FAILED.  Could someone help with this. 
Code: [Select]
url = require("socket.url")
local AV_DEV = 42,17
local LS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local time = os.date('%I:%M')
local todaydate = os.date('%Y-%m-%d')
local SONOSZONES="ALL"
local SameVolume="true"
local TEMP_SID ="urn:upnp-org:serviceId:TemperatureSensor1"
local WUGTempHigh = luup.variable_get(TEMP_SID, "CurrentTemperature", 20)
local currentCondition = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1", "Forecast.1.Condition", 19)
local GS_SID = "urn:srs-com:serviceId:GCalIII"
local jsonEvents = luup.variable_get(GS_SID, "gc_jsonEvents", 55)

if ((jsonEvents == "[]") or (jsonEvents == nil)) then
  TodaysEvents="There are no events for today."
else
 TodaysEvents="Todays events are "
end

package.loaded.json = nil
local json = require("json")
local eventList =json.decode(jsonEvents)  <--------I think the issue could be here
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, endTime, eventname, event

for i = 1,numberEvents do
  startevent = eventList[ i ].eventStart
  startDate = os.date("%Y-%m-%d", startevent)
  startTime = os.date("%H:%M:%S", startevent)
  endevent = eventList[ i ].eventEnd
  endTime = os.date("%H:%M:%S", endevent)
  eventname = eventList[ i ].eventName

  if (startDate == todaydate) then
   TodaysEvents = TodaysEvents .. eventname ..  " .       "
   luup.log(event)
  end

end
package.loaded.json = nil

luup.call_action(LS_SID, "Say", {Text = string.format("Hello, Rumbold family!  It is now %s. Here is your daily calendar. Today will be %s with a high of %s degrees. %s. Have a great day.", time, currentCondition, WUGTempHigh, TodaysEvents), GroupZones=SONOSZONES, SameVolumeForAll=SameVolume, Volume=40}, AV_DEV)

Offline ctguess

  • Full Member
  • ***
  • Posts: 145
  • Karma: +4/-2
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #213 on: June 28, 2016, 11:11:46 pm »
Try looking at this forum topic - http://forum.micasaverde.com/index.php/topic,26692.480.html Page 33, post #480. It has the following code snippet

 
Code: [Select]
local json = require("dkjson") -- depending on your vera version you may need to use require("json")
  local eventList =json.decode(jsonEvents)
  package.loaded.json = nil

Maybe you need the dkjson "version"

Offline kiethr

  • Full Member
  • ***
  • Posts: 108
  • Karma: +1/-2
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #214 on: June 29, 2016, 07:22:53 am »
Well i changed that line and the code did not fail.  However i'm not home so I will have to wait until later to see if it is working.

Thanks for the tip!!!

Offline lloydshearer

  • Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #215 on: January 25, 2017, 12:14:59 pm »
Hi
i am using the great piece of code posted in this forum to do a security sensor check. the code is below.  Does anyone know how to delay the execution of this code.  I need the delay because i am triggering the scene from a amazon echo through HA-Bridge, and the echo is responding ok as the code starts to respond over the sonos system in the same room, i know its trivial but its a bit annoying. 
Thanks in advance
Lloyd


local vol = 100
local son =399
local devcnt = 0
local devno = 0
local numopen = 0
local isare = "is"
local opensensors = " "
luup.log('Commencing sensor device search loop')
for deviceNo,d in pairs(luup.devices) do
   if d.category_num == 4 and d.subcategory_num == 1 then
           
      local tripped = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", deviceNo) or "Nil"
     
      if (tripped == "1") then
         opensensors = opensensors .. ". " .. d.description .. ","
         numopen = numopen + 1
      end
   luup.log(d.description .. ' status:' .. tripped .. ' Dev #' .. deviceNo .. ' id:' .. d.id ..  ' cat #:' .. d.category_num .. ' type:' .. d.device_type)     
   devcnt = devcnt + 1
    end
end
if numopen == 0 then   --If there are no sensors tripped, log and announce!
   luup.log('There were ' .. devcnt .. ' door and window sensors found and all are closed')
   luup.call_action("urn:micasaverde-com:serviceId:Sonos1", "Say", {Text=devcnt .. " sensors were checked, and all are closed. House is secure. ", Language="en", Volume=vol}, son)
   
else   --If there are any sensors tripped, log and announce!
   if numopen > 1 then
      isare = "are"
   end
      luup.log('There were ' .. devcnt .. ' door and window sensors found and ' .. numopen .. " " .. isare .. ' open' .. opensensors)
      luup.call_action("urn:micasaverde-com:serviceId:Sonos1", "Say", {Text="Warning! House may not be secure," .. devcnt .. " sensors were checked and " .. numopen .. " " .. isare .. " open. Check," .. opensensors, Language="en", GroupDevices="Basement, Kitchen", Volume=vol}, son)
     
end

Offline cybrmage

  • Hero Member
  • *****
  • Posts: 1087
  • Karma: +110/-40
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #216 on: January 25, 2017, 02:08:17 pm »
Does anyone know how to delay the execution of this code.

Add an appropriate delay before the luup.call_action commands...

ie:

luup.sleep(1000) -- delay 1 second

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 10061
  • Karma: +759/-141
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #217 on: January 25, 2017, 04:12:49 pm »
Just remember ... the total for any delay by a single function (loops included) should not be much more than 1 second without destabilizing Vera.
In this case this should work ... in general  luup.sleep is a very bad function.
 

Offline lloydshearer

  • Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #218 on: January 27, 2017, 12:29:20 pm »
Thanks very much, worked perfectly

Offline lloydshearer

  • Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #219 on: February 01, 2017, 08:50:52 pm »
Hi
i am using the great piece of code posted in this forum to do a security sensor check. the code is below.  Does anyone know how to delay the execution of this code.  I need the delay because i am triggering the scene from a amazon echo through HA-Bridge, and the echo is responding ok as the code starts to respond over the sonos system in the same room, i know its trivial but its a bit annoying. 
Thanks in advance
Lloyd


local vol = 100
local son =399
local devcnt = 0
local devno = 0
local numopen = 0
local isare = "is"
local opensensors = " "
luup.log('Commencing sensor device search loop')
for deviceNo,d in pairs(luup.devices) do
   if d.category_num == 4 and d.subcategory_num == 1 then
           
      local tripped = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", deviceNo) or "Nil"
     
      if (tripped == "1") then
         opensensors = opensensors .. ". " .. d.description .. ","
         numopen = numopen + 1
      end
   luup.log(d.description .. ' status:' .. tripped .. ' Dev #' .. deviceNo .. ' id:' .. d.id ..  ' cat #:' .. d.category_num .. ' type:' .. d.device_type)     
   devcnt = devcnt + 1
    end
end
if numopen == 0 then   --If there are no sensors tripped, log and announce!
   luup.log('There were ' .. devcnt .. ' door and window sensors found and all are closed')
   luup.call_action("urn:micasaverde-com:serviceId:Sonos1", "Say", {Text=devcnt .. " sensors were checked, and all are closed. House is secure. ", Language="en", Volume=vol}, son)
   
else   --If there are any sensors tripped, log and announce!
   if numopen > 1 then
      isare = "are"
   end
      luup.log('There were ' .. devcnt .. ' door and window sensors found and ' .. numopen .. " " .. isare .. ' open' .. opensensors)
      luup.call_action("urn:micasaverde-com:serviceId:Sonos1", "Say", {Text="Warning! House may not be secure," .. devcnt .. " sensors were checked and " .. numopen .. " " .. isare .. " open. Check," .. opensensors, Language="en", GroupDevices="Basement, Kitchen", Volume=vol}, son)
     
end


I have setup the sonos  api by jishi to use amazon poly for a tts engine, the voice implementation is very very good, does anyone know how i would this tts engine in the above code.  The execution line is http://192.168.10.112:5005/sayall/this is the spoken text

Thanks in advance

Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #220 on: April 03, 2017, 01:02:20 pm »
@kiethr, did you per chance have any success in getting your LUA snippit to work, and if so would you be so kind and share your solution? Mike


Well i changed that line and the code did not fail.  However i'm not home so I will have to wait until later to see if it is working.

Thanks for the tip!!!
« Last Edit: April 03, 2017, 01:16:38 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 MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #221 on: April 03, 2017, 08:17:07 pm »
@keithr, after taking your updated of @becky's code, I did a few modifications to finally getting the LUA snippit to work on my VERA - Edge. I'm hoping that @becky was able to get the Weather Alarm(s) to also work. Does anyone know of a way to get the calendar event to also have the start times announced?

Code: [Select]
-- Morning Announcement (Becky) modified by Michael Blackwell. To let the family know what's going on for the day before we get out of bed. It includes, current time, current weather condition, current temperature and any calendar events for the day from google calendar via GCal3 plugin plugin
local AV_DEV = 15 -- Sonos Device ID within Vera
local LS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local LS_ZONES = "Kitchen,Living Room,Patio Room" -- Valid GroupZones = "All", "Kitchen", "Patio Room", "Living Room", "Master Bedroom", "Lily's Cabin" Note: if you are grouping more than one zone, ensure that there are no spaces between the deliminating comma.
local LS_VOLUME = 70
local time = os.date('%I:%M')
local todaydate = os.date('%Y-%m-%d')
local WEATHER_ID = 5 -- World Weather Device ID
local WEATHER_ID2 = 6 -- World Weather Child Device ID
local WEATHER_SID = "urn:upnp-micasaverde-com:serviceId:Weather1"
local TEMP_SID = "urn:upnp-org:serviceId:TemperatureSensor1"

local WeatherCurrCondition = luup.variable_get(WEATHER_SID, "Condition", WEATHER_ID)
local WeatherCurrTemp      = luup.variable_get(TEMP_SID, "CurrentTemperature", WEATHER_ID2)

local GS_SID = "urn:srs-com:serviceId:GCalIII"
local jsonEvents = luup.variable_get(GS_SID, "gc_jsonEvents", 243) --GCal3 Sensor Device ID

-- checking to see if there are any events for the day. if not, tell us that there are no events for today.
if ((jsonEvents == "[]") or (jsonEvents == nil)) then
  TodaysEvents="There are no Calendar events scheduled for today. "
else
 TodaysEvents="  The Calendar Events for Today are as follows;   "
end

-- unwrapping all the different variables in the device variable.
local json = require("dkjson") -- depending on your vera version like Edge or Plus, you may need to use require("json")
  local eventList =json.decode(jsonEvents)
  package.loaded.json = nil
local numberEvents = table.getn(eventList)
local startevent, startDate, startTime, endevent, endTime, eventname, event

-- looping around the dumping the variables into an array
for i = 1,numberEvents do
  startevent = eventList[ i ].eventStart
  startDate = os.date("%Y-%m-%d", startevent)
  startTime = os.date("%H:%M:%S", startevent)
  endevent = eventList[ i ].eventEnd
  endTime = os.date("%H:%M:%S", endevent)
  eventname = eventList[ i ].eventName

-- if the event happens today, add it to the TodaysEvents variable. if not, don't add it.
  if (i == 1) then
   TodaysEvents = TodaysEvents .. eventname ..  " .       "
   else
   TodaysEvents = TodaysEvents .. " then " .. eventname ..  " .       "
  end
  luup.log(event)
end
package.loaded.json = nil

-- Establishing proper greeting depending on the time of day
local time2 = os.date('It is  %H:%M.')
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 evening"
end

-- and then of course sending it all to the sonos device to make the announcement.
luup.call_action(LS_SID, "Say", {Text = string.format("%s    Blackwell family!  The time is.   %s  Here is your daily report. Today's weather will be   %s   with a high of    %s  degrees    %s     Have a great day.", greeting, time, WeatherCurrCondition, WeatherCurrTemp, TodaysEvents), GroupZones=LS_ZONES, SameVolumeForAll="true", Volume=LS_VOLUME}, AV_DEV)

Made correction in the Say statement had some flaws..
« Last Edit: April 07, 2017, 05:16:01 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 MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #222 on: April 05, 2017, 11:21:48 am »
I've noticed that this thread seems to be a little quite aka not much new content (e.g. new techniques, solutions and code) has been added of late, is this due to everything has been previously addressed or are people moving to something other devices? 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 michelhamelin

  • Full Member
  • ***
  • Posts: 150
  • Karma: +2/-0
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #223 on: October 06, 2017, 10:21:54 pm »
Do You know how to Say also the Room where is the sensor?

Door & Window Sensor Check and Annunciation
Ok, so it took more than "a few hours" but here it is!!! This script checks for all connected door/window sensors, announces how many there are and if any are open (tripped,) it tells you which ones. I need to do more tinkering with the speech strings to make them smoother and maybe a bit slower. I plan to have two scenes, one for upstairs sonos and one for downstairs. The variables should be self explanatory. Feel free to reply with questions, comments, improvements, etc.

-- Door/Window sensor check and speech annotation by Pasqual Zottola
local vol = 50
local son = 27 --mast bdr =33, kitch=27
local devcnt = 0
local devno = 0
local numopen = 0
local isare = "is"
local opensensors = " "
luup.log('Commencing sensor device search loop')
for deviceNo,d in pairs(luup.devices) do
   if d.category_num == 4 then
           
      local tripped = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", deviceNo) or "Nil"
     
      if (tripped == "1") then
         opensensors = opensensors .. ". " .. d.description
         --[[ This line will change to recording info to an array so sonos speaks only once.
luup.call_action("urn:micasaverde-com:serviceId:Sonos1", "Say",
     {Text=d.description .. " is Open", Language="en", Volume=vol}, son)
         --]]     
         -- luup.log(d.description .. " status was " .. tripped)
         numopen = numopen + 1
      end
   luup.log(d.description .. ' status:' .. tripped .. ' Dev #' .. deviceNo .. ' id:' .. d.id ..  ' cat #:' .. d.category_num .. ' type:' .. d.device_type)     
   devcnt = devcnt + 1
    end
end
if numopen == 0 then   --If there are no sensors tripped, log and announce!
   luup.log('There were ' .. devcnt .. ' door and window sensors found and all are closed')
   luup.call_action("urn:micasaverde-com:serviceId:Sonos1", "Say", {Text="There were " .. devcnt .. " door and window sensors found, and all are closed", Language="en", Volume=vol}, son)
   
else   --If there are any sensors tripped, log and announce!
   if numopen > 1 then
      isare = "are"
   end
      luup.log('There were ' .. devcnt .. ' door and window sensors found and ' .. numopen .. " " .. isare .. ' open' .. opensensors)
      luup.call_action("urn:micasaverde-com:serviceId:Sonos1", "Say", {Text="There were " .. devcnt .. " sensors found and " .. numopen .. " " .. isare .. " open." .. opensensors, Language="en", Volume=vol}, son)
     
end