How to play sound files as alarms and notifications via Raspberry Pi 4 audio socket?

Hi
I’m a total beginner with home assistant and I wonder what would be the most straight forward way to do the following:

Use case:
I live in a top floor apartment with roof windows. When I leave my apartment in the morning and one of the roof windows is still open I want to get an audio notification through a speaker near the apartment door.
The notification must be immediate. It won’t help if it plays when I’m already on my way to the car.
I have no network connected audio system like Sonos.
Speech synthesis is not necessary. Being able to play specific wave files for specific notifications would be enough.

Preferred solution:
I want to connect a cheap powered speaker to the audio output of the Raspberry Pi 4 that runs home assistant and use it for all kinds of audio notifications. I would upload wave or mp3 files to the Raspberry Pie and specify which file to play in my automations.

It already existed:
I have searched for existing solutions and I know that there have been a couple of working ones in the past but none of them seem work with the current version.

How would you archieve the described functionality?

Best Regards
Zebi

I believe the way to do this now is by using the VLC addon:

Thanks. Is it no more correct that I first need to install a sound service like pulse audio to enable local audio output?

I have now installed the Add-On and the integration. When I play a short sound file from the media page and select VLC_TELNET as target the sound is played but it is interrupted several times.

Meanwhile I noticed that VLC is adding every played sound file to an evergrowing playlist. This is not what I want.
I need a simple sound output. Is there really no way to do this in Home Assistant?

I was under the impression that VLC worked now. There is a simple way that I use but it is not by connecting a speaker to home assistant.

I have looked into ESPHome and it seemed that they had just started developing an audio integration. I’ll have a second look.

In the meantime I have found more possible ways to get local audio running:

Markus Pöschl maintains currently working versions of MPD and Mopidy which both are said to enable local audio output.

An even simpler approach seams to be “local audio player” by Dingedan.

I will try these options, too, but since these are community provided add-ons like all the other local sound output solutions that don’t work anymore, I fear that they will also stop working after a couple of updates.

Thanks
Zebi

I found a solution! :smiley:

The MPD and Mopidy approaches had the same problem as VLC: They can’t just play a file. These media players all require a playlist. Playing a file means:

  1. Clear the playlist
  2. Add the file to the playlist
  3. Play the playlist
  4. Clear the playlist again

Dingedan had the much better idea by harnessing the power of SoX.

Unfortunately Home Assistant became incompatible to his add-on about a year ago. At this time Manzari made a fork which worked for about 6 months until Home Assistant became incompatible to this version, too. Now I decided to continue this legacy by forking Dingedan’s version and adapting it to the current Home Assistant version which is

  • Home Assistant Core 2022.6.7
  • Home Assistant Supervisor 2022.05.3
  • Home Assistant OS 8.2"

This is the currently working solution for playing sounds and signals through the local audio output of Raspberry Pi 4:

Zebi / Triangulix

1 Like

Looks nice, i’ll give it a try asap :slight_smile:

I’m still using the old method with “SSH & Web Terminal” version 9.1.1 by Frenck (Franck Nijhof) witch uses paplay of pulse audio by passing commands (STDIN).
My automations just need to pass the audiofilename to play (.wav files) to the script below


 script_audio:
   alias: script_audio
   sequence:
   - delay:
       milliseconds: 250 # avoid commands conflict in STDIN
   - service: hassio.addon_stdin
     data_template:
       addon: a0d7b954_ssh
       input: paplay {{ audiofile }}
   - delay:
       milliseconds: 250
   mode: queued

Audio Volume of HA Raspberry pi handling:

- id: audio_vol_changed
  alias: "audio_vol_changed"
  trigger:
    platform: state
    entity_id: input_number.ha_volume
  action:
  - delay:
      milliseconds: 250 # STDIN conflict avoiding
  - service: hassio.addon_stdin
    data:
      addon: a0d7b954_ssh
      input: pactl set-sink-volume 0 "{{ trigger.to_state.state | int }}"%
  - delay:
      milliseconds: 250

Still working actually with latest HA version but i can not upgrade ssh & Web Terminal because for security, Frenck broke the ability to pass command after 9.1.1

Also, notifier add-on (Guido) is working fine too for me:

script_audio_input_2_notifier:
  alias: script_audio_input_2_notifier
  sequence:
  - service: hassio.addon_stdin
    data_template:
      addon: 3ebf62bd_notifier
      input:
        volume: 100
        # mp3 files in \config\www
        music: "{{ audiofile }}"
  mode: queued

:wink:
Eddy
[Message edited for code formatting]

Hello,
I am absolutly new to homeassistant and I try to play audio files with home-assistent.
My homeassistant runs in a virtual enviroment (proxmox).I passed an audio USB stick to homeassistant. This also seems to work when I run paplay /media/alarm.wav in homeassistant’s terminal.

I now want to play the audio file when a lamp turns on.

But how do I use your scripts? Where do I insert them? And how do I connect them to an automation?

It would be great if you could give me a little help getting started

Thanks in advance.
Alex

Hello Alex,
For the method below, you’ll have to use an old version of SSH&Web terminal, the 9.1.1
Here is an example of automation i use to play a sound when a device change his state. The automation call a script passing the audio file name as a variable. The script is the one in my previous message.

# Boule RGB salon ON
- id: boule_rgb_on
  trigger:
  - platform: state
    entity_id: light.extended_color_light_19
    from: 'off'
    to: 'on'
  condition: []
  action:
  #----------------------------------------
  - service: script.turn_on
    target:
      entity_id: script.script_audio_input
    data:
      variables:
        audiofile: /share/Speech_On.wav
  #----------------------------------------
  mode: single

Here is the script:

script_audio_input:
  alias: script_audio_input
  sequence:
  - delay:
      milliseconds: 250 # Avoid STDIN possible command conflict
  - service: hassio.addon_stdin
    data_template:
      addon: a0d7b954_ssh
      input: paplay {{ audiofile }}
  - delay:
      milliseconds: 250 # Avoid STDIN possible command conflict
  mode: queued

PS: I apply same variable method with “notifier” add-on. If you play audio with notifier add-on, you don’t need to stay with an old SSH&Web version of course.

Hello Eddy,
many thanks for the quick response.
I completely overlooked the fact that an old version of the SSH&Web terminal has to be used. What a shame!

I couldn’t find Guido’s notifier anywhere. Is it perhaps no longer included in the home assistant OS 11.1, or am I missing something?

French version of HA: go to “Paramètres, Modules complémentaires…”
Good luck.
Eddy
PS: I’ll be playing Audio tests to an ESP32 with ESPHome 'cause i have to automate sound FX to a very old Xmas train :slight_smile:

##########   SCRIPT  FOR TEST  ########
# NOTIFIER supports mp3 and wav ! ! !
# place audio files in config/www

script_audio_input_2_notifier:
  alias: script_audio_input_2_notifier
  sequence:
  - service: hassio.addon_stdin
    data_template:
      addon: 3ebf62bd_notifier
      input:
        volume: 100
        # mp3 files in \config\www
        music: "{{ audiofile }}"
  mode: queued
######################################
########## CALL THE SCRIPT ########
  action:
  - service: script.turn_on
    target:
      entity_id: script.script_audio_input_2_notifier
    data:
      variables:
        audiofile: coucher_du_soleil.wav
###################################

Thanks Eddy, I was missing exactly this link to Github because unfortunately I couldn’t find the notifier via Google.
Many thanks again :slight_smile:

Yep ! you’re welcome :wink:

For speakers at a certain distance of the HA system, you may use this solution (the one i’ll be trying for my Xmas train) .

HA => ESPHome (NodeMCU,…) => DFPLAYER Mini with Speaker.

(connection HA to ESP via Wifi)

Just put mp3 files on the SD card in DFPlayer.

In HA Automation, you specify the number of the files (announcement) to play…

Eddy

I have been able to play sounds through the Rapberri Pi 4 audio output (jack 3,5mm). This is what I did:

  1. Add the following repository in Addos: GitHub - Poeschl/Hassio-Addons: The repository for my Home Assistant Supervisor Add-ons.

  2. Install MPD addon. Go to Configure and select Output as Bulti-In Audio Stereo:

  1. Include the following code in your “configuration.yaml”
media_player:
  - platform: mpd
    host: "the IP of your local host"
  1. Run the Addon.

  2. Save your MP3 files in the folder /media/

  3. Create a Script like the following:

alias: Doorbell
sequence:
  - service: media_player.play_media
    data:
      media_content_id: media-source://media_source/local/doorbell-1.mp3
      media_content_type: audio
    target:
      entity_id: media_player.mpd
mode: restart

Note: The folder /local/ of the script is equivalent to the /media/ folder.

3 Likes

Hello,
Thank you very much for the great instructions, I tried the variant with MPD straight away and it works, with some limitations.
Unfortunately the sound is terrible. Maybe he’s oversteering? Is there a way to set the volume to 50%?

The variant with the notifier also works for me, unfortunately it doesn’t support stereo (or am I doing something wrong?). But stereo would be very helpful for me because I could control different rooms via the left or right channel. Is there a way to activate stereo on the Notifier?

Thank you again for your support, it helps me a lot!

I have a solution for you , if you are still looking. I’m in the process of writing it up.

I’m interested in your solution, looking forward to seeing the posting of it.