We have moved at community.getvera.com

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

Offline Brientim

  • Sr. Hero Member
  • ******
  • Posts: 2497
  • Karma: +78/-7
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #90 on: February 28, 2014, 04:23:40 pm »
ParkerC,
thanks for the tip..

but what is the 5 in the SONOS variable, what this value set ?
Looking at the script, it is the Device ID of the Sonos that will play the TTS

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2479
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #91 on: February 28, 2014, 06:06:54 pm »
Simon Says Game

A big thank you to Rex for helping me get this one done, I never would've got there myself, it's all him..

The trouble with random selection is that you can get repetition - which doesn't work too well for Simon says. This version does not allow that to happen.

Code: [Select]
local count = math.random(2,5)
local prms = string.format("%d,%d",count,0)
luup.call_delay("simonSays",1,prms)

function simonSays(sprms)
local SONOS = 105
local LS_SID = "urn:micasaverde-com:serviceId:Sonos1"
local scnt,slst = string.match(sprms,"(%d+),(%d+)")
local cnt = tonumber(scnt)
local lst = tonumber(slst)
local says = {"put your hands on your head!", "put your hands on your hips!", "turn around!", "sit down!", "touch your nose!", "stand on one leg!", "Touch your toes!"}
local pick = math.random(1,table.maxn(says))
if pick == lst then
if pick == table.maxn(says) then
pick = pick - 1
else
pick = pick + 1
end
end
local thissay = says[pick]
if (cnt > 1) then thissay = "Simon says, " .. thissay end
luup.call_action(LS_SID, "Say", {Text = thissay}, SONOS)
cnt = cnt - 1
if cnt > 0 then
local parms = string.format("%d,%d",cnt,pick)
luup.call_delay("simonSays",10,parms)
end
end

Offline jimpapa

  • Sr. Member
  • ****
  • Posts: 454
  • Karma: +28/-1
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #92 on: February 28, 2014, 06:36:16 pm »
Now just to make the adult version.

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #93 on: February 28, 2014, 07:01:41 pm »
Now just to make the adult version.
Vera says, you're a bad boy!  ;)
« Last Edit: February 28, 2014, 07:05:27 pm by RexBeckett »

Offline pasqualz

  • Sr. Newbie
  • *
  • Posts: 41
  • Karma: +2/-0
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #94 on: May 07, 2014, 06:32:28 pm »
These programming ideas are awesome and I plan to implement al of them. right now i'm coding a time triggered event that will check all door and window sensors, then verbally report to me which ones are open around bed time. does anyone know of a way to store values from a previous run so I can make the system talk louder if the same sensors are still tripped? i'm assuming the code can handle arrays, right? the question is how to clear the arrays after a certain time so that the system starts anew the next evening?

Also, a question about the simon says code. What's the best way to trigger this event to run on demand? can I create a virtual light switch that I can "turn on" from the dashboard, or better yet, my iphone Vera mobile app? Also, how can I stop the program from looping when i'm done with it?
thanks
Pasqual

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #95 on: May 08, 2014, 12:15:34 am »
Quote
does anyone know of a way to store values from a previous run so I can make the system talk louder if the same sensors are still tripped?

Just use a global variable (without a local tag). It will vanish after a restart so will return  nil.

Quote
i'm assuming the code can handle arrays, right?

Lua is very comfortable with arrays.

Quote
the question is how to clear the arrays after a certain time so that the system starts anew the next evening?

You could have another scene scheduled to run at a certain time that cleared the global variable.

Quote
What's the best way to trigger this event to run on demand?

Just run the scene.

Quote
can I create a virtual light switch that I can "turn on" from the dashboard, or better yet, my iphone Vera mobile app?

Yes. You could use VirtualSwitch or MultiSwitch.

Quote
Also, how can I stop the program from looping when i'm done with it?

It stops automatically after saying the last phrase (without Simon says).

Offline pasqualz

  • Sr. Newbie
  • *
  • Posts: 41
  • Karma: +2/-0
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #96 on: May 13, 2014, 03:06:58 pm »
I know that this post may be moved out of this section for being off topic, but it does have much relevance to dynamic TTS messages using the Sonos Plugin, so I thought I'd start here. So I'm planning to start programming some LUA code in a scene that will do the following:

1) Identify all door/window sensors and get their device IDs. I could hard code all my sensors into the script, but I think I can use the devices variable. I found this code in the Luup Lua extensions wiki page which seems to gather all attributes for attached devices. I can probably search for devices that are within a specific device type for my purposes:

for k, v in pairs(luup.devices) do
    for k2, v2 in pairs(v) do
        luup.log("Device #" .. k .. ":" .. k2 .. "=" .. tostring(v2))
    end
end

2) Loop through all the devices and check if any are tripped

3) Generate a dynamic TTS message that either tells me that all windows/doors are closed, or identify the open ones by name using the device information gathered in step 1.

I think this is pretty straightforward, and could put it together in a few hours, but in the spirit of community, I realized that others may have already created such a script and would be willing to post it here for consumption. This is where I'm probably going to be told to make my request in a more generic programming forum.
Thanks
Pasqual

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2479
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #97 on: May 13, 2014, 03:15:43 pm »
Hi @pasqualz

This thread is largely focussed on sharing the end product, rather than the journey - but this is a community so we all try and help where we can :)

You  might however be better served in the Programming section of the forum.  However if it is a straight forward as you say, then please do post your code examples here, as I think it would be great to have some code that can more effectively scan all devices and report information via TTS.

Offline pasqualz

  • Sr. Newbie
  • *
  • Posts: 41
  • Karma: +2/-0
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #98 on: May 15, 2014, 11:21:56 pm »
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

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2479
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #99 on: May 16, 2014, 01:44:28 pm »
Nice, thanks so much for sharing,

As is, it will look for everything that uses that service, so for example it will also report the status of the Ping Sensor plugin  and any motion sensors too e.g.

Quote
Ping Sensor status:0 Dev #9 id: cat #:4 type:urn:schemas-demo-ted-striker:device:PingSensor:1
_Motion Sensor status:1 Dev #246 id:30 cat #:4 type:urn:schemas-micasaverde-com:device:MotionSensor:1     
   

Maybe you could consider an option in your code so you can specify a certain device type, at least that could exclude the Ping sensor being reported.

If you click the '#' hash/number option on the tool bar, it will allow you to wrap your code up nicely with some HTML so it can stand out more
« Last Edit: May 16, 2014, 02:23:15 pm by parkerc »

Offline pasqualz

  • Sr. Newbie
  • *
  • Posts: 41
  • Karma: +2/-0
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #100 on: May 16, 2014, 08:50:12 pm »
Thanks Parkerc, I had a feeling that there are more than just door/window sensors in category 4 but it was a simple starting point. Can anyone think of a specific attribute of these devices that is unique? I noticed that when I check the type of my devices, the Wintop sensors show up as motion sensors, and my Schlage sensors show up as window sensors so type doesn't seem reliable either. Since The names of all of my sensors have either the word "door" or "window" in them, I could add a string.find() command to look for either "door" or "window" in the description.
Thanks
Pasqual

Offline Brientim

  • Sr. Hero Member
  • ******
  • Posts: 2497
  • Karma: +78/-7
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #101 on: May 16, 2014, 09:01:07 pm »
You then need to look at sub-categories but as you may find, they are either not defined or incorrectly defined.
http://wiki.micasaverde.com/index.php/Luup_Device_Categories

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2479
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #102 on: May 17, 2014, 01:24:33 am »
Hi Pasquel

As it is now - it actually works nicely for me as an 'occupancy checking tool' as the Ping sensor is used to track a few mobile phones.

Good idea about adding a filter, maybe if your code could allow for optional filters - so you could go from the top level Device, to the service, and then (if needed) down to aspects of the device description itself  - that would certainly give it flexibility to be used for any broad or specific set of TTS checks you would want done.

Another idea for you..

- add the ability to retrieve and use the 'name' of the room the sensor is in to the TTS - that way it could say "the <room name> door is open"

Offline pasqualz

  • Sr. Newbie
  • *
  • Posts: 41
  • Karma: +2/-0
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #103 on: May 17, 2014, 04:02:08 pm »
I'll definitely look into subcategories but was worried that one improperly reported device would mess that up. That's why I just decided to put the words "door" or "window" in the description. Of course there could be other devices with those words in there too!

Parkerc, I was going to retrieve and announce the room name for the devices, but my descriptions for the door & window sensors have the room name in them (i.e. Family Room Window 1, Front Door) bottom line is it would be minimal coding to add room names in if needed.
Thanks
Pasqual

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2479
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: Text To Speech (TTS) With Sonos - Creating Dynamic Messages
« Reply #104 on: May 18, 2014, 03:15:11 pm »
Thanks @Pasqualz

I understand, people will have different set ups and like you I also have a some devices that have the room name in the description, however since adopting the room approach,  I've tried to change more of them over.

Keep up the good work, I like what you've done, mix your code with the random words post (previous page) and that could make it a little more dynamic/different.

I'm always excited to see how people are building TTS with Vera and Sonos - so please keep sharing :)