I just configured a pretty slick burglar alarm with the open-source Home-Assistant platform running on my Raspberry Pi. It can be armed to trigger when a door is opened and/or when a motion sensor goes off.
Then, a sequence of events happens:
- An IR LED turns on my stereo
- A sound file I cobbled together starts playing. It starts with a computerized voice saying that your entry been detected and recorded, then it beeps for a while (giving you time to disarm), and then it goes into a blaring siren noise. Quite the escalation!
- A light blinks on and off a few times
- A camera takes a series of snapshots of the area
- An email is sent to my phone with the snapshots, showing me what’s going on
There’s also a silent-alarm mode that just takes pictures and emails them to me.
To disarm, you have to type the proper disarm sequence with an infrared remote I have sitting on the counter (re-purposed from and old DVD player).
To arm the system, I can flip a switch in the web-based GUI, but I also have it automatically arm at a certain time of night and when I activate the “leaving home sequence” (by turning off the light by the door when the door is open). It temporarily deactivates in the morning when the system detects me waking up. Pretty awesome!
Configuring a Burglar Alarm in Home Assistant
When I started, the SMTP email notification component in HA didn’t support in-line photo attachments so I added that capability with PR #2738. It has been merged and made it into the 0.26 release.
This system uses my Ecolink Z-wave door sensor and my Z-wave Aeotech Multisensor 6 as actuators (see an older post for more info about these). It uses the shell_command component to to take snapshots from my Foscam IP camera, and to issue LIRC commands that turn on the stereo. It uses the lirc component with my $1 IR receiver (hooked directly to the Pi’s GPIO pins) to receive the disarm command (see my earlier post on that). It uses the smtp component to send the email notifications. Here are some config entries:
Burglar automation
This triggers the alarm. (The second condition is important for those times when the disarm button isn’t working yet and your wife triggers like 5 copies of the script that play the siren noise 5 times overlapping and even if she turns off the stereo it keeps turning itself back on and she gets really mad at you. Just saying.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- alias: Burglar Alarm Door trigger: platform: state entity_id: binary_sensor.ecolink_doorwindow_sensor_sensor_3 from: 'off' to: 'on' action: service: script.turn_on entity_id: script.burglar condition: - platform: state entity_id: input_boolean.burglar_alarm_door state: 'on' - platform: state entity_id: script.burglar state: 'off' |
Burglar script
This is the sequence that fires off all the commands once triggered. (Note that the images input to the emailer is the new thing I added).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
burglar: alias: Burglar Alarm sequence: - service: shell_command.stereo_line_in - service: shell_command.snapshot data: filename: /home/pi/snapshot1.jpg - service: shell_command.fix_camera_jpg data: filename: /home/pi/snapshot1.jpg - delay: seconds: 1 - service: shell_command.intruder_music - service: script.turn_on entity_id: script.night_reminder - service: shell_command.snapshot data: filename: /home/pi/snapshot2.jpg - service: shell_command.fix_camera_jpg data: filename: /home/pi/snapshot2.jpg - service: notify.emailer data: title: 'Intruder alert' message: 'Intruder alert at apartment!!' data: images: - /home/pi/snapshot1.jpg - /home/pi/snapshot2.jpg |
Snapshot shell_commands
The convert command (from imagemagick) is required to avoid MIME errors in the SMTP python library (fixes headers somehow).
1 2 3 4 5 |
shell_command: snapshot: wget "http://camip:port/snapshot.cgi?user=<user>&pwd=<password>&resolution=32" -O {{filename}} fix_camera_jpg: convert {{filename}} {{filename}} intruder_music: mpg321 /home/pi/Videos/burglar.mp3 stereo_line_in: irsend SEND_ONCE STEREO KEY_VIDEO1 |
Input_booleans
To control arming and disarming.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
input_boolean: burglar_alarm_door: name: Burglar Alarm Door initial: off icon: mdi:lock-outline burglar_alarm_motion: name: Burglar Alarm Motion initial: off icon: mdi:lock-outline burglar_alarm_silent: name: Silent Burglar Alarm initial: off icon: mdi:lock-outline |
Feels pretty good to have all this, plus tons of other elaborate automations for just a few hundred bucks! Plus being able to get into home-assistant’s code and fiddle around is super rewarding and fun.
THANK YOU!
hello friend. i followed your tutorial.. i receive the email…but photos no… why that happens? i think the configuration is correct.
I’m not sure. Are there any useful log messages? Can you confirm that the photos are being created in the file locations that you expect them to be in?
Hello. Pretty amazing stuff.
I’m new in home automation but have some good knowledge in linux and shell programming. I am making my own bulgar alarm using HA in pi with a multisensor and a siren. I’ve done a good progress but would like to make it more efficient. I would like my HA to remember the previous states of “Input booleans” after a PI restart e.g. after a temp power interruption. I am thinking to use shell commands and files acting as flags. So the intial state would be defined by the file name.
Do you have any idea how i could incorporate “if-like” statements in HA?
Thanks
We should build that feature into HA, but right now I don’t think it’s there. Someone got it working using shell commands and automations similar to your suggestion. I don’t really have a better idea at the moment. Oh you could use an external MQTT server with persistence I guess but that requires two machines.
how do you disarm the sound, if you enter home and forget to disarm the alarm before?
You can either go into the phone app and press the DISARM button that I made (it runs the disarm script), or (more conveniently), you can use the IR remote control to send the “disarm sequence” which is basically just a numeric code. So it’s just like a regular alarm system in that you have to type the secret code to disarm.
Too cool! Of course I’d want to have lasers but then that might not be legal. Just saying….
Hello! Just a silly question. Is the censored address from the first pic your Raspberry’s IP address or is it the http://hassbian.local:8123?
Hello,
thank you for sharing your work.
Can you please tell me in wich files do I have to copy-paste those code lines ?
Thank you.
Ben