In-Line TTS Audio Interupter (using a Raspberry Pi)

This is (in my opinion) a nice workaround to the lack of notification functionality in Echos / Alexa.

Intro

Ever since I started to get into home automation I’ve wanted a way to handle TTS notifications. E.g.

  • “There is someone at the door”
  • “You have left the front door open”
  • “It’s going to start raining in 10 minutes”

My setup includes an Echo Dot connected (via an amp) to speakers that cover the ground floor of our house. My ideal solution would be if the Echo allowed for notifications (without having to ask for them) but unfortunately it doesn’t look like Amazon will be introducing this any time soon.

I do like the Echo setup, I can practically talk to Alexa anywhere downstairs (and even upstairs to some degree) and it’s perfect for controlling lights and more importantly playing music from my phone or internet radio.

I started to think about whether I could plug both the Echo and a Chromecast Audio into the amp, then I could use the Chromecast Audio for the TTS anouncements. The issue was with mixing the 2 channels. You can’t just plug 2 devices into 1 audio input (in desperation I did try) and my cheap Amazon amp doesn’t have this functionality (I don’t think most amps wouldn’t tbh). I could have purchased a 2 channel audio mixer but any that I found online were more expensive than the amp!

Solution

I had a spare Raspberry Pi Zero sitting around and I wondered if I could use that somehow. After some research and playing around I eventually found a solution. It works like this:

Hardware

  • The Echo Dot is connected to a cheap USB sound card from Pimoroni. This is plugged into the Pi.
  • Using Alsa the Pi is configured to pass the input sound directly to the output.
  • The soundcards output is connected to my amp.

With this setup the Echos audio is still passed to the amp but with a Pi sitting in between.

Software

  • A custom python script runs as a service on the Pi.
  • The script listens for a message on a specific MQTT topic.
  • When a message is received it uses the gTTS Python Library to download an MP3 of the message in spoken word (this uses Googles TTS API).
  • The volume of the input is lowered (so you can hear the announcement if music is playing)
  • Using Alsa player a short notification jingle is played (found on freesound.org).
  • Then, using MPG123 the TTS MP3 file is played.
  • Lastly the input volume is returned to normal.

You can then use this in your automations or scripts like this:

automation:
  - alias: TTS Test
    trigger:
      <whatever you want>
    action:
      service: mqtt.publish
      data:
        topic: tts
        payload: <whatever you want to say>

Some Notes

  • Alsa comes with the latest Raspbian build.
  • mpg123 needs to be installed with sudo apt-get install mpg123.
  • The gTTS library needs to be downloaded using sudo pip install gTTS.
  • As does the PAHO MQTT library sudo pip install paho-mqtt.
  • When I first plugged the soundcard in the input audio wasn’t passed to the output. To change this I ran alsamixer (where you can see the levels for the input and output), moved the selection over to Mic and pressed “m” to un-mute it.
  • There are many ways to run a Python script in the background / as a service but the method I have had most luck with is described in this great article.
  • Not all USB soundcards play nicely with the Raspberry Pi. This is why I bought one that a) was advertised as for a Raspberry Pi and b) is from a well known supplier.
  • I would probably use a Raspberry Pi ZeroW if I did this again, I only used the non W version because I had one lying around.
  • In the future I might add look to add things such as doorbell noises and potentially alarm sounds.

I hope this is useful to someone :slight_smile: .

EDIT: I’ve now change the custom script to use VLC rather than Alsa Play and MPG123. This was I can pre-load and control the player in python rather than calling os commands. Also, I was having some weird jumping issues when playing the short MP3 files in MPG123.

For these new dependencies you may need to run sudo apt-get install vlc and also sudo pip install python-vlc.

7 Likes

That is a neat solution.

Btw. you actually can run two signals into your amp. It’s probably not the best idea though, as it can lead to distortion and maybe even other problems in theory, but from practical experience this would work for at least the scenario you described.

Thanks cgtobi.

you actually can run two signals into your amp

I did try this but (as you mention) had aweful sound quality. I did also read that this could damage some of the devices plugged in as well (although not sure how much of a risk this is).

1 Like

Exactly. A mixer, soft- or hardware based is definitely the better solution.

1 Like

Hi great project.

I wanted to know as to how do you get you amazon echo dot to notify you of eg some movement using your setup?

Also do I necessarilyneed an amp for this project or can I just connect the rpi and echo dot via the USB sound card?

Thanks

Hi @bachoo786,

how do you get you amazon echo dot to notify you of eg some movement using your setup?

I’m sure there are plenty of ways of doing this but the Xiaomi Body Sensors are a cheap wireless solution to this. Have a read of this thread. I have one of these turning my kitchen lights on and it works well.

I also have a Xiaomi switch as a doorbell which works well with the TTS (“There’s someone at the door.”).

do I necessarily need an amp for this project or can I just connect the rpi and echo dot via the USB sound card?

I don’t believe the Echo Dot has a sound input and so you would need an amp (or at least something like a portable speaker).

You could connect the Pi to the Echo via Bluetooth but my experiments with this weren’t very successful. If you did this you wouldn’t need this setup as you could just use the TTS and Media Player components alredy in Home Assistant.

The other reason I avoided the bluetooth connection between the Pi and Echo is that I wanted to keep the ability to connect my phone to the Echo and Amazon make it quite difficult to switch between bluetooth devices on the Dot.

I hope this helps.

Hi @Dullage

Thanks for the reply.

I have motion sensors setup and working fine it’s just that I wanted to find out how I could set up to get echo dot to announce when a motion is detected.

So for this project i believe the amp is necessary for the echo dot to output TTS? I don’t want to go through the Bluetooth route either.

Have you tried using MQTT using your setup?

The only way (at least I know of) to get the Echo to play any sound without first asking it to do so is to use bluetooth. If you don’t want to use bluetooth (and there are many reasons not to) and you want TTS Announcements you will need to introduce another speaker.

I already had the Echo plugged into another speaker so for me putting this in-between was a good solution. In your case however, if you have no need to plug the Echo into a speaker then you could ignore my project and simply plug a speaker into the machine running Home Assistant. You would just use Home Assistants in-built TTS and Media Player components (FYI - I found VLC to work quite nicely)

My code uses MQTT to transfer the intended TTS message from Home Assistant to the PiZero that sits in-between the Echo and the amp. If you just plugged a speaker into the Home Assistant host you wouldn’t need to use MQTT as you could just call the TTS Service.

I hope this makes sense.

Amazing guide. But I found a simpler way to combine both sounds from Echo and HA using this speaker. It has audio input at the back, so I suppose if I plug in Raspberry Pi audio to this speaker, it will also play the sound from my Pi and at the same time play sound from the Dot. I am still waiting for it to be delivered. Keeping my finger cross.

Perfect makes sense
So alexa cannot announce notification connected to HA until amazon release that function is that correct?

Ooooh, that looks interesting. I wonder if it does mix the audio. Let me know how you get on with it.

So alexa cannot announce notification connected to HA until amazon release that function is that correct?

Yup, unfortunately not. They’ve introduced notifications (not in the UK yet but elsewhere) but you have to ask Alexa for them. I think you get a light come up on the Echo to tell you there’s a notification waiting.

1 Like