Indigo Speaks As Alexa

Have you modified a device or piece of software to do something cool? Post it here!
User avatar
mgolden50
Posts: 274
Joined: Mon Jan 29, 2007 7:55 pm
Location: Chandler, AZ
Contact:

Indigo Speaks As Alexa

Post by mgolden50 »


I have found that it is very desirable to have Indigo impersonate Alexa and speak as Alexa through Airfoil to one or more of the speakers in my home to make various Indigo generated announcements.

Amazon provides a service that they call Polly which provides the same text-to-speech capability they use for the Echo Alexa service. https://aws.amazon.com/polly/ The Polly service has a free tier that includes 5 million characters per month for speech or Speech Marks requests, for the first 12 months, starting from your first request for speech. There after it's $4 for every 1 million characters processed. I've never come close to that. You will need to create an Amazon account to access Polly. You can also use the free Polly demonstration web app at Amazon to make .mp3 audio announcement files that Indigo can play at no ongoing cost. https://us-east-2.console.aws.amazon.co ... sizeSpeech I use the mp3 solution for standard recurring announcements and the live real-time synthesis for complex announcements about the state of my home systems.

I have attached a modified version of Amazon's example code that works as an Indigo Python script. It converts text from a variable into Alexa's speech. I have been using it very reliably for more than a year.

More complex, and not at all essential:I also wanted the audio coming directly from my Alexa Dot's and the audio coming from my Airfoil Airport Express units in each room to play from the higher quality speakers in each of those those rooms--I breadboarded small simple passive audio mixers by cutting two-way stereo splitter cables and inserting 1k ohm resisters in the left and right audio paths of both of the two splitter cables to function as a combiner/mixer. Alexa's voice is indistinguishable regardless of which system is the source (Echo or Indigo) for speech of music sourced from either system.

Also, not essential; I wanted to be able to trigger certain Alexa routines from Indigo without needing a human to actually speak to Alexa to trigger the routines. [An example is telling Alexa to "resume music" once an hour to prevent playing background music from timing out. Also Indigo can request specific playlists with this mechanism.] I set up a special Dot (named House) whose audio output I have connected to the audio input of the Mac Mini that hosts Indigo and Airfoil. This House-Dot is the music source that plays announcements and music house-wide the through Airport Expresses in each designated room using Airfoil. To have the Indigo/Alexa tell Amazon/Alexa to do something, I placed a small amplified speaker adjacent to the HouseDot in a cabinet. Indigo/Alexa speaks to Amazon/Alexa by electing this speaker. Originally I used one of the Airport Expresses to do this. But to avoid having "self-talking commands disrupt Airfoil's music distribution I added a USB audio adapter (Sabrent USB External Stereo Sound Adapter) to the Mac Mini. By directing Indigo's synthesized or recorded Alexa commands this way (as show in the code example) schizophrenic Alexa can say to herself "Alexa, do this... " and expect it to happen.

Hope this solution works for any others who might find it useful. If There's sufficient interest perhaps someone with the necessary skills could convert it into a plugin.

Code: Select all

## Alexa Speaks Announcement Text In Variable Through Airfoil To House Speakers
import os, boto3, time ## Amazon's boto3 Python module must be installed on Mac

SpeakThis = indigo.variables[740708485].value
str (SpeakThis) ## assure the variable to be spoken is a string

## Settings taken from Amazon's sample Polly code
defaultRegion = 'us-east-1'
defaultUrl = 'https://polly.us-east-1.amazonaws.com'

indigo.actionGroup.execute(348168416) ## Enable Default Announce Airfoil House Speakers Volume Settings
indigo.actionGroup.execute(1112870329) ## Enable Airfoil For System Audio
time.sleep(3) ## Delay for Airfoil to set up speakers

## boto3 functions provided in Amazon's Polly example code 
def connectToPolly(regionName=defaultRegion, endpointUrl=defaultUrl):
    return boto3.client('polly', region_name=regionName, endpoint_url=endpointUrl)
 
def speak(polly, text, format='mp3', voice='Joanna'): ## Joanna is Alexa
    resp = polly.synthesize_speech(OutputFormat=format, Text=text, VoiceId=voice)
    soundfile = open('/tmp/sound.mp3', 'w')
    soundBytes = resp['AudioStream'].read()
    soundfile.write(soundBytes)
    soundfile.close()
    os.system('afplay /tmp/sound.mp3')
    os.remove('/tmp/sound.mp3')

polly = connectToPolly()
speak(polly, str(SpeakThis))
time.sleep(2) ## Time required for processing and speaking
indigo.actionGroup.execute(278671718) ## Restore Current Speakers Saved Volume Levels & Connections
indigo.actionGroup.execute(1082305604) ## Enable Airfoil For Line-In Audio

Code: Select all

## Alexa Speaks To Alexa
import os, boto3, time ## Amazon's boto3 module must be installed on Mac

SpeakThis = indigo.variables["AlexaSaysThisVariable"].value
str (SpeakThis) ## Assure that the variable value to be spoken is a strig

defaultRegion = 'us-east-1' ## values assigned in Polly example code
defaultUrl = 'https://polly.us-east-1.amazonaws.com'

## the Python module "SwitchAudioSource" must be installed on the Mac
## to make use of the USB audio adapter
os.system("switchaudiosource -t output -s 'USB Audio Device'")

def connectToPolly(regionName=defaultRegion, endpointUrl=defaultUrl):
    return boto3.client('polly', region_name=regionName, endpoint_url=endpointUrl)

def speak(polly, text, format='mp3', voice='Joanna'): ## Joanna is Alexa
    resp = polly.synthesize_speech(OutputFormat=format, Text=text, VoiceId=voice)
    soundfile = open('/tmp/sound.mp3', 'w')
    soundBytes = resp['AudioStream'].read()
    soundfile.write(soundBytes)
    soundfile.close()
    os.system('afplay /tmp/sound.mp3')
    os.remove('/tmp/sound.mp3')

polly = connectToPolly()
speak(polly, str(SpeakThis))
time.sleep(2) ## Delay needed for Polly to process and speak

os.system("switchaudiosource -t output -s 'Built-in Output'")

Code: Select all

## Alexa Records mp3 Announcement From Text In Variable
import os, boto3, time ## Amazon's boto3 Python module must be installed on Mac

SpeakThis = indigo.variables["AlexaSaysThisVariable"].value
str (SpeakThis) ## assure the variable to be spoken is a string

## Settings taken from Amazon's sample Polly code
defaultRegion = 'us-east-1'
defaultUrl = 'https://polly.us-east-1.amazonaws.com'

newFileName = SpeakThis + ".mp3" # new file named with what is spoken
newFilePath = "/Users/bentley/AnnouncementRecordings/"+newFileName
indigo.server.log(unicode(newFilePath))

## boto3 functions provided in Amazon's Polly example code 
def connectToPolly(regionName=defaultRegion, endpointUrl=defaultUrl):
    return boto3.client('polly', region_name=regionName, endpoint_url=endpointUrl)
 
def speak(polly, text, format='mp3', voice='Joanna'): ## Joanna is Alexa
    resp = polly.synthesize_speech(OutputFormat=format, Text=text, VoiceId=voice)
    soundfile = open(newFilePath, 'w')
    soundBytes = resp['AudioStream'].read()
    soundfile.write(soundBytes)
    soundfile.close() # New recordings left in Announcements Directory
    os.system('afplay /newFilePath')
    
polly = connectToPolly()
speak(polly, str(SpeakThis))
User avatar
mgolden50
Posts: 274
Joined: Mon Jan 29, 2007 7:55 pm
Location: Chandler, AZ
Contact:

Controlling Alexa from Indigo 2.0

Post by mgolden50 »

During the middle of the pandemic lockdown I was frustrated that even though I had available the wonderful Alexa Hue-Bridge Plug-in that allowed me to use Alexa to control virtually anything that Indigo could in turn control I was still wanted a way to trigger Alexa routines without having to speaking to her.

So I created a somewhat complex and relatively "Micky Mouse" solution:
See: viewtopic.php?f=135&t=24233

This earlier solution solved two different problems for me:

The first: Since I couldn't make Indigo directly trigger spoken announcements or execute other routines using any kind of direct connection, I adapted a Python script provided by the AWS' Polly service to convert text contained in an Indigo Variable into Alexa-like spoken announcements that could be played throughout my home via the Airfoil plugin.

The second: Was a solution for using Indigo to command Alexa's functions and routines using the text-to-speech solution above with a dedicated Airfoil speaker and an Alexa Dot. I placed speaker in front of the DOT in a cabinet to speak Indigo generated commands to the Alexa system. this sort of worked but was very picky about the speaker placement and volume levels.

Short of native Indigo/Alexa integration promised at some point by Matt and Jay, a new and much better solution for controlling Alexa from Indigo has recently become available as an Alexa Skill.

The new Skill is called IFTTTrigger. It's free for up to three triggers and only $5 per year for an unlimited number. It's a little tricky to get started with this skill but it works solidly.

To start, search Alexa Skills for IFTTTrigger
Launch the skill and enable it.
A screen will show up with an Amazon log-in button. Press it.
Sign in with your Amazon credentials.
Choose between the free and paid option.
Default trigger names e.g ifttt-1 are presented in a list.
Type over the default names with your own custom names.
Only the characters A-Z, a-z, 0-9, and "-" __" are allowed.
Click Next and the Triggers in the list will be added as devices to your Alexa account.
These trigger devices can be used in the "When this happens..." in an Alexa routine.

Note: Every time you add or modify a trigger you need to disable and then re-enable the
IFTTTrigger skill

Also, Trigger names are case sensitive so every character in a web-hook request to
trigger the Alexa routine must match the trigger name exactly.

To trigger an IFTTTrigger based Alexa routine I use an script action with this code like
this example:
import requests
requests.get("https://mkzense.com/webhook/Alexa/
4d8abd8cff6dc969bxxxxxxx68b9fa7af32ddba/StopMusicEverywhere")

The long string of characters after Alexa/ is your IFTTT key which can be found in your
IFTTT Account.

Hope this works as well for all of you Alexa/Indigo fans as it has for me.

Mike
User avatar
mundmc
Posts: 1064
Joined: Fri Sep 14, 2012 4:34 pm

Re: Indigo Speaks As Alexa

Post by mundmc »

I just noticed this, and it is awesome.
ckeyes888
Posts: 2431
Joined: Thu Nov 26, 2009 2:41 pm
Location: Kalispell, MT

Re: Indigo Speaks As Alexa

Post by ckeyes888 »

Love to try this but I can’t get the IFTTTrigger skill to enable.
I can get as far as defining the trigger names, I did the paid version, then click next, it says “Processing”
then shows a page to Allow the Alexa actions, then immediately reverts to the login page.

Anyone else have this working that might have an idea why the skill won’t enable?

Thanks,

Carl
ckeyes888
Posts: 2431
Joined: Thu Nov 26, 2009 2:41 pm
Location: Kalispell, MT

Re: Indigo Speaks As Alexa

Post by ckeyes888 »

As per usual for me it decided to link/enable today for no known reason.
Works great!! This is rather huge for me to be able to control any Alexa device/rountine etc. from Indigo.

Thanks,

Carl
ckeyes888
Posts: 2431
Joined: Thu Nov 26, 2009 2:41 pm
Location: Kalispell, MT

Re: Indigo Speaks As Alexa

Post by ckeyes888 »

Doh. Having setup a few IFTTT triggers a year ago I of course now can’t seem to remember all the steps.
I created the triggers in the IFTTT Trigger skill, created the rountines in Alexa to turn the lights on and off and restarted the IFTTT skill.
I must be missing a step as I see scenes for all the triggers I created last time but none for the new ones I’m trying to create.

Appreciate any help in getting it working.

Thanks,

Carl
User avatar
mgolden50
Posts: 274
Joined: Mon Jan 29, 2007 7:55 pm
Location: Chandler, AZ
Contact:

Re: Indigo Speaks As Alexa

Post by mgolden50 »

i had the same problem. Old triggers showed up but new ones did not.

The solution was to look for any duplicate triggers in the list of triggers and delete the duplicates.
Hope this works for you.
ckeyes888
Posts: 2431
Joined: Thu Nov 26, 2009 2:41 pm
Location: Kalispell, MT

Re: Indigo Speaks As Alexa

Post by ckeyes888 »

Not seeing any duplicates of anything.
Do you know how Alexa “discovers” the new scene?
Is just making a new routine considered a scene?

Thanks,

Carl
User avatar
mgolden50
Posts: 274
Joined: Mon Jan 29, 2007 7:55 pm
Location: Chandler, AZ
Contact:

Re: Indigo Speaks As Alexa

Post by mgolden50 »

After you've added one or more new IFTTT triggers you need to say Alexa, discover devices. as you wood for any new devices added to Alexa.
ckeyes888
Posts: 2431
Joined: Thu Nov 26, 2009 2:41 pm
Location: Kalispell, MT

Re: Indigo Speaks As Alexa

Post by ckeyes888 »

Not sure how or why it started working but it is.

Thanks!

Carl
Post Reply

Return to “User Modifications”