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

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.

Me too Cory. Thanks for doing this.

before HAOS upgrade to 12.3, i had no problem playing stereo “wav” files (44.1 KHz/16bit stereo .wav files) with Guido’s Notifier Add-On.
See my request on Guido Github:

Guido’s notifier Add-on is my prefered bet, i have tried ton’s of other solutions for local playback and also for calling remote audio script on another slave HomeAssistant instances from a master one.
Guido’s notifier handles perfectly “playback queuing” and that’s not the case with, by example, VLC-TELNET… vlc-telnet, with the local VLC add-on, plays fine for “single shot” playback, but if you do NOT want to miss important audio warnings, you have to queue messages and in that case, vlc-telnet just makes bullshit=crappy noise :slight_smile: (this was the case 1 year ago… maybe bug corrections have improve the behaviour).

Before Guido’s Notifier, i was using paplay and pactl (volume) commands with a unique and central queued audio script for playing (and another for volume control).
PAPLAY and PACTL from pulse audio library are (still) included in Frenck’s SSH&Webterm Add-on (A “Must have” add-on). You do not need to install any package…

But actually, to be able to pass PAPLAY and PACTL commands from HA-Core to SSH&Webterm, you need to send the commands with SSH with public/private local keys between HA-Core container and SSH&WebTerm Add-on container.

Why SSH ? BECAUSE SSH&WebTerm AddOn does not anymore support STDIN direct commands from HA-Core (remove after SSH&WT 9.1.1) !

Setup of a working ssh config between these 2 containers is not easy but i can share all the steps if someone is interrested. Feel free to ask !
Advantage: you can very easely pass these commands between several Home assistant instances (several raspi…), just change the target ip in the commands.

Example of commands setup in configuration.yaml and script (1 HA instance, 192.168.1.15 is the local ip of my master raspi4 homeassistant):

shell_command:
  audio_input_2_ssh_webterm: ssh -i /config/ssh_keys/id_rsa -o 'StrictHostKeyChecking=no' [email protected] paplay {{ filename }}
  audio_volume_2_ssh_webterm: ssh -i /config/ssh_keys/id_rsa -o 'StrictHostKeyChecking=no' [email protected] pactl {{ commande }}

With PACTL set-sink-volume, it’s possible to set a different volume level on left and right channels.

automation example for volume control (need a input_number slider):
Those codes have been thoroughly tested !

# VOLUME CONTROL OF LOCAL RASPI4
- id: audio_vol_changed
  alias: "audio_vol_changed"
  trigger:
    platform: state
    entity_id: input_number.ha_volume
  action:
  - delay: 
      milliseconds: 100
  - service: shell_command.audio_volume_2_ssh_webterm
    continue_on_error: true
    data:
      commande: set-sink-volume 0 "{{ trigger.to_state.state | int }}"%
    response_variable: command_response
  #----------------------------------------
  # if command return an error, second try !
  - if: "{{ command_response['returncode'] != 0 }}"
    then:
    #----------------------------------------
      - delay:
          milliseconds: 100
      - service: shell_command.audio_volume_2_ssh_webterm
        continue_on_error: true
        data:
          commande: set-sink-volume 0 "{{ trigger.to_state.state | int }}"%
    #----------------------------------------
  - delay:
      milliseconds: 100
  mode: queued

And the playback script (must be “queued” if you do not want to lose audio messages):
WARNING: PAPLAY plays wav files, not mp3 !

script_audio_input_ssh_2_sshwt:
  alias: script_audio_input_ssh_2_sshwt
  sequence:
    - delay:
        milliseconds: 100
    #----------------------------------------
    - service: shell_command.audio_input_2_ssh_webterm
      continue_on_error: true
      data:
        filename: /config/www/audio/"{{ audiofile }}"
      response_variable: command_response
    #----------------------------------------
    # If error code in return, try again !
    - if: "{{ command_response['returncode'] != 0 }}"
      then:
      #----------------------------------------
        - delay:
            milliseconds: 100
        - service: shell_command.audio_input_2_ssh_webterm
          continue_on_error: true
          data:
            filename: /config/www/audio/"{{ audiofile }}"
      #----------------------------------------
    - delay:
        milliseconds: 100 
  mode: queued

And you can now call the above script in your automations passing the audio filename as variable. Example below:

  - service: script.turn_on
    target:
      entity_id: script.script_audio_input_ssh_2_sshwt
    data:
      variables:
        audiofile: systeme_desarmed.wav
        #  wav audio files in /www/audio

For the volume, just use the your input_number somewhere in UI, script,…

Actually, i’m still using guido’s notifier on all my homeassistant instances (several raspi4). And i spread audio commands from the master instance to the others with MQTT messaging (very easy). With the only exception of the “volume control”, for this one i’m using SSH commands to the SHH&WebTerm add-on of the slave raspi4 boxes.

Hope this little contribution may serve some of you :cowboy_hat_face:

PS: All my audio files are in /config/www/audio/
and apologise: some words/variables are in french in the above code (like: “commande”).

Did you solve the volume control of the audio mixer ? If it’s a raspberry pi, you can pass the shell_command “pactl parms…” through a SSH connection to the SSH&WebTerm Add-on container. Works with all versions i think but i’m using the latest one.
(you’ll have to setup first the ssh with priv and public keys between Core and Add-on).

here are some commands you may use to control volume:

pactl

pactl command is used to control a running PulseAudio sound server.

Increase volume by 10%
pactl – set-sink-volume 0 +10%

Decrease volume by 10%
pactl – set-sink-volume 0 -10%

Set volume to 80%
pactl – set-sink-volume 0 80%

Set volume to 200%
pactl – set-sink-volume 0 200%

Balance control example
amixer sset Master 80%,20%