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

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%