Automation to play a sound when door sensor is triggered

So I’ve got my door sensor working and now I’d like to do something with it. I’m trying to start simple and have my Pi play a sound through the audio jack where I have powered speakers plugged in. Is there a component for this or will I have to use a shell command? If the latter, I’m at a loss. (If this is not simple, I’m also interested in getting notifications on my phone.)

There are some stock sound effects (.wav files) in the subfolders of this directory for example: /usr/share/scratch/Media/Sounds

Can anyone help or point me to an example or tutorial that I missed?

My system:
Raspberry Pi 3 w/ Raspian Jessie
Home Assistant etc installed from the All-in-One-Installer

2 Likes

May try something like “audioplay /usr/share/scratch/Media/Sounds/file.wav” as shell command? :slight_smile:

1 Like

Yeah, if you want to play a single wav or mp3 file, you have to use the command line.

I would recommend using omxplayer as it can play both .wav and .mp3 files. On the command line of your Raspberry Pi, type omxplayer and hit enter, to see if it is already installed.

If not, then run:

sudo apt-get install omxplayer -y

Then, in your configuration.yaml, add a command line notifyer:

notify:
  name: omxplayer
  platform: command_line
  command: "omxplayer /path/to/audio_file.mp3"

Then create an automation like below:

automation:
  alias: 'Play Audio File'
  trigger:
    platform: state
    entity_id: sensor.front_door
    state: on
  action:
    service: notify.omxplayer

If you don’t want to create it as a notifyer, you can also set it up as a shell_command service call in your configuration.yaml:

shell_command:
  play_sound: omxplayer /path/to/audio_file.mp3

and then use the same automation, but call the shell_command:

automation:
  alias: 'Play Audio File'
  trigger:
    platform: state
    entity_id: sensor.front_door
    state: on
  action:
    service: shell_command.play_sound
4 Likes

Thanks a lot! I’ll give one of these a try.

Alright, first attempt didn’t work.

  • omxplayer is installed
  • I checked the yaml at yamllint.com
  • The sensor change registered in my HA log
  • I copied the entity id from elsewhere in my configuration.yaml
  • The shell command service doesn’t work when I call it directly from the HA user interface.

So I’m wondering if my path to audio is wrong? I’m running HA in a virtual environment. Does my virtual environment user “hass” have access to “/usr/share/scratch/Media/Sounds/Instruments/Trumpet2.wav” or is there something else wrong with that path?

`shell_command:
play_sound: /usr/share/scratch/Media/Sounds/Instruments/Trumpet2.wav

automation:
alias: ‘Play Audio File’
trigger:
platform: state
entity_id: binary_sensor.aeotec_dsb29_doorwindow_sensor_2nd_edition_sensor_3
state: on
action:
service: shell_command.play_sound`

Your shell command is missing the player

shell_command:
play_sound: omxplayer  /usr/share/scratch/Media/Sounds/Instruments/Trumpet2.wav
1 Like

If you want to try stuff out with shell commands you should always try to run it in a usual shell. If it works there you’re good to go :wink:

Yeah thanks :slight_smile: I did try it out as a shell command beforehand but failed to transfer it all to the config. Unfortunately it’s still not working!

When I run “omxplayer /usr/share/scratch/Media/Sounds/Instruments/Trumpet2.wav” as the default “pi” user the shell command works. However, under my virtual environment user “hass” (“sudo su -s /bin/bash hass”) - where I run Home Assistant - the same command doesn’t produce sound. It gives an error:

  • failed to open vchiq instance

Under the “hass” user I can run that shell command, substituting “aplay” for “omxplayer”… In that case, it says it’s playing it, but again there’s no sound. :confused:

1 Like

I searched that error and this guy had a related issue; it’s a permissions error.
https://digitalchild.info/raspberry-pi-failed-open-vchiq-instance-solved/

So I’m looking for a way to give my user “hass” permission to use audio I guess. I tried this:

sudo usermod -a -G audio hass

The result was that “aplay /usr/share/scratch/Media/Sounds/Instruments/Trumpet2.wav” now works inside the virtual environment, but the omxplayer version still returns the error. I tried a HA configuration with aplay instead of omxplayer but it still didn’t work.

1 Like

Add your user hass to the sudoers list.

Edit the sudoers file

sudo nano /etc/sudoers

and add the following at the bottom:

hass ALL=(ALL) NOPASSWD: ALL

then in your shell_command add sudo in front of the player:

shell_command:
  play_sound: sudo omxplayer /usr/share/scratch/Media/Sounds/Instruments/Trumpet2.wav

Also, sometimes certain accounts don’t have access to all the path environment variables, so probably best to make sure by adding the full path to omxplayer:

shell_command:
  play_sound: sudo /usr/bin/omxplayer /usr/share/scratch/Media/Sounds/Instruments/Trumpet2.wav

Now some people will say that adding hass to the sudoers list is a potential security risk, in case your Home Assistant is accessible from outside your home network, thus allowing someone to stumble across your API and try and do something nefarious, but I as all shell_command services are running only specific commands and not giving anyone access to a root terminal, I wouldn’t worry about it. I have Home Assistant running on 3 separate Raspberry Pi’s in 2 different houses and all of them have the hass account on the sudoers list, just makes it easier for me to ensure I can run any command line applications without issue.

5 Likes

Thanks a lot jbardi! I added hass as a sudoer. Getting closer… :slight_smile:

I can now successfully call the shell command service from the Home Assistant user interface. It’s still not triggering when the door opens though. Here’s my code:

shell_command:
 play_sound: sudo /usr/bin/omxplayer /usr/share/scratch/Media/Sounds/Instruments/Trumpet2.wav
automation:
  alias: 'Play Audio File'
  trigger:
    platform: state
    entity_id: binary_sensor.aeotec_dsb29_doorwindow_sensor_2nd_edition_sensor_3
    state: on
  action:
    service: shell_command.play_sound

I’ve double-checked the entity ID. For example, here’s my customize code in the same configuration file:

  customize:
    # Only the 'entity_id' is required.  All other options are optional.
    binary_sensor.aeotec_dsb29_doorwindow_sensor_2nd_edition_sensor_3:
      friendly_name: Front Door

Everything looks fine with your YAML code, and since you can copy and paste the command from the play_sound line into a terminal window and hear the sound, you know the commands works fine, so I’m still not sure why it isn’t fireing.

Just to be sure, are any of your other automations fireing correctly? If you have a bootstrap error in another automation, it will bring down the entire automation platform. This sometimes makes it hard to know if the problem is in your current automation or if the automation system is simply not running.

Do you have any errors in your log?

In the home assistant frontend, click the icon at the bottom of the side menu that has the letter “i” inside of a circle. This is the about page, and it shows a log of the current session. Search that page for any bootstrap errors, or any errors of any kind, actually, so maybe we can get a better idea of what is going on.

1 Like

@jbardi - hey, that “i” is much easier than looking at the log directly, hehe. This is my only automation and I did have a bootstrap error!

16-08-29 08:42:18 homeassistant.bootstrap: Invalid config for [automation]: expected str for dictionary value @ data['trigger'][0]['state']. Got None (See /home/hass/.homeassistant/configuration.yaml:92)

I searched for this and read in other topics that I need single quotes on the states. Here is my corrected, working configuration:

shell_command:
 play_sound: sudo /usr/bin/omxplayer /usr/share/scratch/Media/Sounds/Instruments/Trumpet2.wav
automation:
  alias: 'Play Audio File'
  trigger:
    platform: state
    entity_id: binary_sensor.aeotec_dsb29_doorwindow_sensor_2nd_edition_sensor_3
    state: 'on'
  action:
    service: shell_command.play_sound

SOLVED! Thanks everybody :smiley:

3 Likes

Awesome, so glad you got it figured out :smiley:

Also, with the latest release 0.27, there is check_config option of the hass executable, so that you can check the validity of all of your scripts before starting up hass.

Since you are running this on a pi, I’m assuming with the All-In-One installer, then type the following in a terminal as the hass user:

/srv/hass/hass_venv/bin/hass --script check_config

It will scan all your configurations and tell you exactly what is wrong in any of your YAML files

3 Likes

Excellent guide. How about randomly play from a list of sound clips each time the automation is triggered?

1 Like

Greetings,

Anyone knows if this should work with Philips Hue motion sensor as well? I follow the exact steps in the Solution reply. I could trigger playing the sound from HA interface/browser. I do see the sensor icon change from False to True when I walk in. However, no sound could be heard. I assigned homeassistant account to audio group and also sudoers file.

Here are the lines in configuration.yaml

sensor 3:
platform: rest
resource: http://hue_bridge_ip/api/my_api_key/sensors/6
value_template: ‘{{ value_json.state.presence }}’
scan_interval: 2
name: ‘Driveway sensor’

shell_command:
play_sound: sudo /usr/bin/play /usr/share/totem/siren.wav

automation 5:
alias: ‘Play Audio File’
trigger:
platform: state
entity_id: sensor.driveway_sensor
state: ‘True’
action:
service: shell_command.play_sound

As stated above, I could trigger the sound to play from HA interface so I don’t think the issue is playing the file. Not sure why it doesn’t get triggered.

Thanks for any tips!

I guess after 3-4 hours reading around, finally figured it out lol. This is what I ended up with in configuration.yaml
This is for Philips Hue sensor. If you will notice, i have to: ‘True’ instead of on.

automation 5:

  • alias: driveway motion
    trigger:
    platform: state
    entity_id: sensor.driveway_sensor
    to: ‘True’
    action:
    service: shell_command.play_audio

shell_command:
play_audio: ‘/usr/bin/play /usr/share/totem/siren.wav’

1 Like

Awesome! Thanks Guys! It worked. I installed in on my rasberry Pi 2 running Hassbian. I tried also play the audio from different folders. Now I have music notification. The only issue is the audio dosen’t stops until it plays to the end.
But am ok with that. Thanks guys.