[SOLVED] How to play sounds through a 3.5mm audio jack

Hello all,

I am currently evaluating different solutions for my smart home system. At the moment I am testing Home Assistant.

One of the requirements I have is to play predefined sounds via the local audio output when events occur. Explicitly NO text to speech via external providers, NO DLNA to a smart speaker. Only 3.5mm audio jack, only hardcore. :wink:

I was very surprised when I couldn’t find this feature, which in my eyes is one of the absolute basics, right away.

A quick search found these threads:

https://community.home-assistant.io/t/how-can-i-play-a-sound-as-a-script/

https://community.home-assistant.io/t/playing-a-wave-file/

required-a-step-by-step-guide-on-how-to-play-sounds-and-media-through-a-3-5mm-audio-jack
(no link here because new users are only allowed to post 2 links)

Here, among other things, very adventurous solutions were developed with VLC, Telnet and so on, which are unfortunately not suitable for me. The requirement is quite simple to output a sound via cable, completely offline.

So I searched and tried and found a very simple solution. I would like to present it here, maybe someone has the same problem.

One of the promising approaches from above mentioned topics was to play the sound via “aplay”. This also works on console / via ssh, but not from an event trigger / automation.

The reason is that the automation is executed in a container where “aplay” is not available. This may be for security reasons, but it is annoying.

Fortunately, there is another program available: ffplay.

Side note: The list of available commands/programs can be obtained by executing the following shell command from the automation:

 ls /usr/bin > /config/commands.txt 

You can download the file for example via Samba Addon.

The following steps will lead to success:

  1. install Samba addon.

  2. connect to the Home Assistant system via Samba (I use Linux, for Windows it is called network share, I think).

  3. create a subdirectory in the directory “config”, for example “audio”. Possibly you can also do this in “share” or “media”, I have not tried. Config will be mounted in all containers in any case.

  4. copy all files you want to play into the new created directory “audio”.

  5. add the following in the "configuration.yaml

shell_command:
    play_local_notification: 'ffplay -nodisp -vn /config/audio/beep.wav'

Here the filename is hardcoded, but I want to keep the example simple. If you have more than one file, you should use parameters for the filename.

The parameters ‘-nodisp -vn’ are necessary because ffplay is a media player, not just audio. But since Home Assistant has no video subsystem, the player will crash when trying to initialize it. The parameters tell the player that only audio is needed.

  1. restart, then the shell command can be used in any automation.

I haven’t figured out how to update the configuration without rebooting, but as I said, just started using the system yesterday.

I hope this helps someone who has the same problem.

Kind regards
Alex

7 Likes

Thanks for sharing. For completeness could you please also tell us what hardware you are running Home Assistant on and what install method you used (HAOS, Core, Container, Supervised…?).

Oh, right, I completely forgot about that.

I installed a ready-made image (HAOS 9.2) on a Raspberry Pie 3B+. Today came an update, now the system tells me:

Home Assistant 2022.10.5
Supervisor 2022.10.0
Operating System 9.3
Frontend 20221010.0 - latest

But it still works.

1 Like

I came back here to say “Thanks, Alex!”, and also to warn others not to be as stupid as I have been:
DO NOT test this in the console directly, because it will only work via the shell_command as described above.

My version:

shell_command:
  play_local_notification: ffplay -loglevel quiet -nodisp -vn -autoexit /media/alerts/beep.wav

(The “-autoexit” because during my testing in the HA container I noticed that ffplay keeps running after the sound, and “-loglevel quiet” because my inner perfectionist cannot accept the dozen lines of logging that nobody will ever see.)

5 Likes

Thank you! This is exactly what I needed, simply a sound to play locally on the physical HA server itself with as little latency as possible when a motion sensor is triggered . A few notes:

  • The SAMBA add-on didn’t immediately work for some reason, but before I got into troubleshooting, I realized I already installed the File Editor add-on, which worked perfectly for uploading the sound file.
  • I installed HAOS on an old Dell laptop. The built-in speakers work, but the volume controls are function keys that don’t work from the CLI, so I had to use the -volume switch.
  • .mp3 files work as well as .wav

Hope that helps someone. Thanks again!

Thank you @mycodefactory, this tuto is exactly what I was looking for!
I also have the same problem as @semose for the volume, how did you implement the “volume switch”?

This was very helpful, thanks! I use this to relay a doorbell.

I want to add a warning: changes to /usr/share/hassio/homeassistant/configuration.yaml apparently cannot be hot-reloaded, it takes a full restart. I also tried the paplay command, but that didn’t work. Also, ffplay did not show up in my /usr/bin, so I’m not sure thats a reliable way to determine the available commands. The /media directory works fine btw.

Hey there, I’ve just set this up and was struggling with it a little bit.
To those who’re brand new to home assistant here’s a comprehensive guide:

  1. From the add-on store, install Studio Code Server
  2. Open it and you’ll see configuration.yaml file
  3. Just on a new line add the following to play a meow sound:
shell_command:
  play_cat_sound: ffplay -loglevel quiet -nodisp -vn -autoexit /media/Miaw.mp3

(thanks to Nik above)
4. Restart home assistant (Settings => System => top right corner power button, RELOAD doesn’t work)
5. Go to an automation and select “Service”, then select “play_cat_sound” from the list.
6. You can trigger it from here to see if it works.

Ok, so how do you put the “/media/Miaw.mp3” file there?

  1. Simple, grab your phone, open Home Assistant Companion (or download it from Play/App store)
  2. Click on Media from the sidebar, then “My media”, then “Manage” and “Add media”.
  3. Whatever you upload here will show up in the /media/ folder in home assistant that you can now use with your automation.
  4. If your sound includes spaces, make sure you enclose it in commas like this:
    play_alert_beeps: ffplay -loglevel quiet -nodisp -vn -autoexit '/media/Radioactive Sonar.mp3'

If you want to use Text-To-Speech:

  1. Click on Media => Text-to-speech => Google => make it say something
  2. This will actually save the “speech” to a folder called /config/tts/
  3. You can possibly reference it from here as well in configuration.yaml, but if not, here’s how you move it to /media/
  4. Open up Studio Code Server once again and click the hamburger menu on the left.
  5. Click terminal => new terminal
  6. move (and rename) the file the following way:
cd /config/tts/  (change folder)
ls -laht (list files in creation time order)
# select the latest file created. It'll match up with the one you just generated.
mv ee3a4fd868386883cfd5c4aa8f85888602867772_en_-_google_translate.mp3 /media/alarm-1.mp3

last command will rename your file to alarm-1.mp3 and move it to the /media folder.

That’s it!

Troubleshooting steps:

  • if you test you automation, but you can’t hear anything (was my issue) check Settings => System => Logs => Home assistant Core

You should see something coming up there, which could be an indication of an issue.

  • if you don’t see any error here and you see “Action executed” in your automation when you test it, you probably have an issue with your home assistant instance.

At least that was my issue!
I’m running this under a virtual machine, and the audio for the VM was set to something random.
Go to the AUDIO setup of your virtual machine, enable it and select the “Audio controller” that aligns with the one you have in your host machine (for me it was Intel HD Audio). Regardless, play around with these settings, as this may be the issue.
Additionally, just because it plays from the “Media” menu option in home assistant, don’t assume it also works on your home assistant host machine!

For me the above works with the in-built and 3.5 jack speakers. (I’m running this on an ubuntu HP all-in-one machine with VM)

Hope I helped someone, this is literally the only guide I found online that allows OFFLINE media playback on built-in or attached dumb-speaker.

4 Likes

To add detail to what krisztian has said above.

Note that a Terminal or ssh connection to (raspberry pi) with

Home Assistant 2023.8.4 
Supervisor 2023.08.1 
Operating System 10.5 
Frontend 20230802.1 - latest

… is NOT the same environment as the hassOS is running in, so a

[core-ssh ~]$ pacat /media/alerts/Done.wav

will work in the Terminal/ssh, but NOT as a shell_command: as shown above.

Only if you do:

shell_command:
  list_cmds: "echo $PATH | tr ':' '\n' | sed -re 's/^/ls -l /' | sh > /media/alerts/cmdlst.txt"

and then execute that thing as shown above,
you will get a cmdlist.txt file which contains the ACTUALLY available commands - as this will run in the actual environment where these things get executed.

Now this fact is just one of the idiosyncrasies in hassOS that has had med stumped.
This detail IS NOT shown anywhere as far as I know currently, “it seems you’re just expected to know it”.

1 Like

@mycodefactory Thanks for sharing. Before I’ll purchase a speaker: Are you using a speaker with or without external power? I wonder if my Raspberry Pi 4 has enough Watt to play a sound without crashing (either the external SSD that’s powered via the USB port or the system itself). I’d prefer not to install another power supply.

WARNING

The solution seems not to work with Home Assistant Core 2024.2.x anymore. The last known working Version is 2024.1.6.

I haven’t tried to find out the reasons yet. For those for whom this functionality is critical, I recommend to stop updating, or at least make a backup so that you can roll back.

1 Like

Although this solution does not work, I was able to get a bluetooth speaker to output audio through it in a similar fashion. This worked for me.

See here: GitHub - adrgumula/HomeAssitantBluetoothSpeaker: Home Assistant and Bluetooth speakers (based on the Xiaomi Mi Compact BLE Speaker 2 XMYX02YM)

THANK YOU I KNEW it stopped after I updated the core!

I just figured out you can just set up the VLC add on manually or using hacs and it will just auto detect it (at least my laptop that’s running home assistant’s speakers and so probably the audio Jack too).