Required a step-by-step guide on how to play sounds and media through a 3.5mm audio jack

What is wrong with the auto generated automation script?
System sound working just fine.
Absolute directory is '/media/file.mp3
Card on Dashboard shows file playing, but no sound.

 action:
  - service: media_player.play_media
    target:
      entity_id: media_player.vlc_telnet
    data:
      media_content_id: '/media/classic-door-bell.mp3'
      media_content_type: music
    metadata: {}
  mode: single
1 Like

I use this and it works fine:

  - service: media_player.play_media
    target:
      entity_id: media_player.vlc_telnet
    data:
      media_content_id: http://IP.OFY.OUR.HA:8123/local/ns-ding-dong.mp3
      media_content_type: music

My mp3’s are stored in config/www/

https://thehelpfulidiot.com/playing-a-sound-directly-from-home-assistant-operating-system#comment-3820

I want to use the 3.5mm jack on the Rpi 4 I have and followed this how to I linked. But it didn’t work for me. not sure what I am doing wrong…

I am posting it here because maybe it can help someone else.

I was eventually able to get it to work using the link I sent earlier, but I found a better way

install local vlc then install telnet. Follow the instructions from the thread Iinked

1 Like

This addon works perfect!
Thank you!

Dear Guido, I get an error when I start an automation with local audio play:
wget: error getting response: Invalid argument
play FAIL formats: can’t open input file URL `http://localhost:8123/local/beep.mp3’:
[12:35:44] ERROR: Invalid audio

I don’t configure the path for audio files, only copied mp3 to config/www folder.
Could you help me?

I tried the VLC addon a while ago, and found that it would often fail to play my audio, or be very choppy in its play back and never found a solution. Its possible its better now, or something specific in my setup caused the issue (virtual box on windows running HAOS) and how audio is passed through.

In the end I went a different direction using an old mobile phone I had lying around using the bubbleupnp app. I can now select the phone as a media player, and use its 3.5mm jack to output the audio to any connected speaker.

1 Like

Dear Hakos, you should in the Notifier addon options check the music-test option, stop and restart the addon and send me the result

It’s worked, played the test song.
I think the issue is with source path: I use DuckDNS add-on and I can’t acces with local IP for HA, only with Duckdns. So this path: (`http://localhost:8123/local/beep.mp3’:slight_smile: isn’t correct.
How can I configure this correctly?

Dear Hakos, to be sure that the problem is in the path you should temporarily remove duchdns addon and check if the error is still there.

yes, the problem is the local access. I changed configuration.yaml and removed duckdns registry and installed and configured nginx. After that everything works fine! Thank you!

This solution is not good for me. I need DuckDNS, but with dnsmasq I can access in local network only with https. So this path is not good for me: http://localhost:8123/local/
could I change it?

At the moment (April 2024) the following setup works without any add-ons:

  • You will need Pulse Audio client binaries (pacat, paplay, pactl). They used to be present in the main HA container, but were removed. However, all libraries required by those binaries are still present in the main container
  • Take those binaries from the hassio_audio container and copy them to /media as follows:
docker cp hassio_audio:/usr/bin/pacat /media/
docker cp hassio_audio:/usr/bin/paplay /media/
docker cp hassio_audio:/usr/bin/pactl /media/

These commands can be run either from the host OS (if you have access to it, e.g. you are running HA supervised) or from the Advanced Terminal add-on. While doing this, you can verify whether HA would send the sound to the sound card you are expecting: run pactl info and verify whether “Default sink” is showing what you are expecting. If you have access to the host OS, the command would be: docker exec -it hassio_audio /usr/bin/pactl info.

  • Now you can define something like this:
shell_command:
    play_sound_file: "/media/paplay /media/myfile.ogg"

and use this as a service in automations. One downside: PulseAudio libraries in HA do not support MP3 files. WAV, OGG, FLAC is ok, but not MP3.

I’ve opened a feature request for a proper implementation of the local playback. Consider voting for it: click

1 Like

I read this thread some time ago but just revisited the issue. There is an official VLC add-on in the Settings > Add-ons > ADD_ON STORE.

This installs and runs the add-on under the HA OS. No manual config file editing, no yaml or other code needed. You may have to configure its inputs and outputs in the add-on’s Configuration tab (two pull-down menus), although defaults worked for me. Then you start the add-on (check the log tab for errors and if all looks good, enable the watchdog and start on boot).

With the add-on running (and this part is key), you go to Settings > Devices & Services, and should have the VLC telnet integration auto-discovered. You add it (again, via UI - no manual config needed) and there’s not much to do after that. Everything should just work out of the box - or at least it did for me on 3 HA OS instances I’ve done this so far.

If you’ve some VLC integration manual install config entries pre-dating this, you may have to take it out and clean that up very thoroughly and redo this procedure.

I have copied pacat, pactl, paplay and pa-info to the /config/scripts folder using the Advanced SSH & Web Terminal

/config/scripts/paplay -s 192.168.1.30 audifile.ogg

When running the command from Advanced SSH & Web Terminal it works fine. When running from an automation I get “return code: 127” in the logs.

Running pactl info > meh.txt works and I get the output in the meh file, but paplay won’t budge, return code 127 no matter what I do with this command. It’s also not permissions as the file is 777 and I can make the pacat work using /config/scripts/pacat --list-file-formats -v > meh.txt again. But it won’t play :frowning:

Any pointers on what I could try?

Several versions ago HA removed PulseAudio libraries from the main container (in addition to the binaries which were removed even earlier). This breaks the setup described in my earlier post.

Solution: to copy the required library from hassio_audio to the same location on the host machine, to which the binaries were copied before. There is one additional command besides the ones I listed in my previous post:

docker cp hassio_audio:/usr/lib/pulseaudio/libpulsecommon-16.1.so /media/

The command to call paplay from within the main container is modified as follows:

docker exec -it homeassistant /bin/bash -c 'LD_LIBRARY_PATH=/media /media/paplay <your_sound_file>'

The shell command is adjusted as follows:

shell_command:
    play_sound_file: "LD_LIBRARY_PATH=/media /media/paplay /media/myfile.ogg"

Hope this helps.

OK, with the LD Library, I now get “Stream error: No such entity”

homeassistant:/media# LD_LIBRARY_PATH=/media /media/paplay -s 192.168.2.30 /config/www/media/koekoeksklok-conv.ogg -v
Opening a playback stream with sample specification 'float32le 2ch 16000Hz' and channel map 'front-left,front-right'.
Connection established.
Stream error: No such entity
 /media LD_LIBRARY_PATH=/media /media/pactl list short sinks
0       auto_null       module-null-sink.c      s16le 2ch 48000Hz       IDLE

As I suspect there is no sink set, I tried:

/media docker exec -it homeassistant /bin/bash -c "LD_LIBRARY_PATH=/media /media/pacmd load-module module-tunnel-sink server=192.168.2.30"
No PulseAudio daemon running, or not running as session daemon.

This seems to be the next problem. Now at least the pa* stuff can start successfully.

Try local playback first, e.g. plug some USB sink into the HA computer, check pactl info and try paplay <file> without any options.

It’s using the network to play the audio. Audio plays fine when using the command from within the Advanced SSH and Web Terminal addon from within Home Assistant:

Two commands are identical, just running from within another container I guess

I gave up trying to make it work. I simply connect to the host using SSH and play the audio there. Audio file needs to be on the host. It’s a bit overkil, but at least it’s also futureproof as it doesn’t depend on any configuration in the homeassistant container itself.

You do need to setup SSH rsa keys for a ‘ha_audio’-like user on the host ofc.

shell_command:
play_sound_koekoeksklok: /bin/bash -c /usr/bin/ssh user@server -i id_rsa -t "/usr/bin/paplay /usr/share/hassio/koekoeksklok.wav -s hostname"