How can I play a sound as a script?

I have a sound door.wav which I can play via the console either via aplay or via omxplayer:

aplay /home-assistant/www/audio/door.wav
omxplayer /home-assistant/www/audio/door.wav

I can also access it via http://192.168.178.76:8123/local/audio/door.wav.

But I fail to play it via a script within home assistant. I don’t have any smart speakers. Just normal speakers on the 3.5mm audio out of the Raspberry Pi.

My Setup

I run Home Assistant via Docker compose on a Raspberry Pi 2. The speaker is just on the normal audio output. My docker-compose.yml looks like this:

version: '3'
services:
  homeassistant:
    container_name: homeassistant
    image: "ghcr.io/home-assistant/home-assistant:stable"
    devices:
      - /dev/snd:/dev/snd
    volumes:
      - /home-assistant:/config
      - /etc/localtime:/etc/localtime:ro
    restart: unless-stopped
    privileged: true
    network_mode: host

Approach 1: Shell Command

Adding this to my configuration.yaml:

shell_command:
        doorbell: aplay /config/www/audio/door.wav

I then use service: shell_command.doorbell. I see no error, but I don’t hear the sound either. I also don’t like this approach as I would need to restart home assistant for every sound I add.

Approach 2: Media Player

Adding this to my configuration.yaml:

media_player:
  - platform: vlc
    name: speaker
    arguments: '--alsa-audio-device=hw:0,0'

Reading VLC media player - Home Assistant I guess the issue could be that I run Home Assistant via docker-compose?

1 Like

Hi there,

First try calling media player service like below for VLC media player. Make sure Vlc is started.

service: media_player.play_media
target:
  entity_id: media_player.speaker
data:
  media_content_id: http://192.168.178.76:8123/local/audio/door.wav
  media_content_type: music

If this doesnot work, I think you should try a Music Player Daemon like mopidy. I have been using mopidy for the same function and its works without any problems, also it can be run as a service so it starts up along with RPI boot.
Check out this

https://docs.mopidy.com/en/latest/installation/raspberrypi/

You can also play audio in your browser if you have HA ta open it with using this custom addon