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
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:
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?
Yeah thanks 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.
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.
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:
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.
Thanks a lot jbardi! I added hass as a sudoer. Getting closer…
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:
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.
@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:
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:
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.
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.
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
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.