Use a speaker with Home Assistant on a RaspberryPi to output audio messages

Cover image

For a long time I wanted to add audio to my Home Assistant Setup. My first use case is that I want Home Assistant to literally tell me when I should air my room based on the current CO2 value.

I have Home Assistant running on a RaspberryPi 3B+. It already has an 3.5mm audio jack which I used to attach an old portable speaker. The speaker is powered over the USB of the Raspi.

In Home Assistant I did following steps to integrate the speaker.

Install MPD Add-on (Music Player Daemon)

The Add-on provides an installation of MPD, an open source music player. It can be controlled via a web ui called ympd and from Home Assistant directly as an media_player entity. To install it...

  • Add Poeschl Home Assistant Supervisor Add-ons to your Add-on Store

    • Supervisor > Add-on Store > Menu > Repositories > Add
  • Install the MPD Add-on (The default Add-on configuration worked for me, no need to adjust it)
  • Make sure to start the add-on

Adjust configuration:

Next we need to add the add-on as media_player to our configuration.yaml:

    platform: mpd
    host: <ip of the raspi>
    scan_interval: 1

# Text to speech
    platform: google_translate
    service_name: google_say        

I also configured Text to speech (TTS) so that arbatrary messages can be read to me. Here I used Google Translate but in future I would like to use Nabu Casas` TTS service, their voices sound very natural.

Don't forget to reload the core after the configuration change.

Say "Hello World"

You can test your setup via the Developer Tools. In the Services Tab use the tts.google_say service and call it with following service data:

entity_id: media_player.mpd
message: "Hello World!"
language: en

Now you should hear a friendly "Hello World" from your speaker. If not, make sure that you restarted Home Assistant after the configuration changes, that the add-on is running & that your speaker is on and not muted.

Use audio in an automation

Now you can output audio messages over your speaker from every automation by calling above service. I used it to tell me "What about airing your room?" when the CO2 value is over a certain threshold. In Node RED it looks like this:

It takes in the value from the CO2 sensor. Then the "CO2 Ampel" maps the value to a color (<800 green, <1200 yellow, else red). The RBE node ensures that the message is only played once by forwarding the message only if it has changed e.g. from yellow to red. The next node makes sure it is daytime, I don't want to get waken up during the night. The last node calls the TTS service and outputs its results on the media_player.mpd. Basically it is the same as our "Hello World" call in the developer tools.

What's next?

In my opinion it is a really easy setup but offers so much possibilities for fun and helpful automations. I already implemented a little good morning and good night message and think it would be nice to add more information to them e.g. about the weather.