{"id":1037,"date":"2016-08-07T12:45:56","date_gmt":"2016-08-07T19:45:56","guid":{"rendered":"https:\/\/partofthething.com\/thoughts\/?p=1037"},"modified":"2017-01-02T09:45:49","modified_gmt":"2017-01-02T17:45:49","slug":"raspberry-pi-home-assistant-diy-burglar-alarm","status":"publish","type":"post","link":"https:\/\/partofthething.com\/thoughts\/raspberry-pi-home-assistant-diy-burglar-alarm\/","title":{"rendered":"Raspberry Pi + Home Assistant DIY Burglar Alarm"},"content":{"rendered":"<p>I just configured a pretty slick burglar alarm with the open-source <a href=\"https:\/\/home-assistant.io\/\">Home-Assistant platform<\/a> running on my Raspberry Pi. It can be armed to trigger when a door is opened and\/or when a motion sensor goes off.<\/p>\n<p><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/08\/burglar_arm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1039\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/08\/burglar_arm-614x1024.png\" alt=\"Arm the burglar alarm\" width=\"300\" height=\"500\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/08\/burglar_arm-614x1024.png 614w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/08\/burglar_arm-180x300.png 180w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/08\/burglar_arm.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Then, a sequence of events happens:<\/p>\n<ul>\n<li>An IR LED turns on my stereo<\/li>\n<li><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/08\/burglar.mp3\">A sound file<\/a> I cobbled together starts playing. It starts with a computerized voice saying that <em>your<\/em> <em>entry been detected and recorded<\/em>, then it beeps for a while (giving you time to disarm), and then it goes into a blaring siren noise. Quite the escalation!<audio class=\"wp-audio-shortcode\" id=\"audio-1037-1\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/08\/burglar.mp3?_=1\" \/><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/08\/burglar.mp3\">https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/08\/burglar.mp3<\/a><\/audio><\/li>\n<li>A light blinks on and off a few times<\/li>\n<li>A camera takes a series of snapshots of the area<\/li>\n<li>An email is sent to my phone with the snapshots, showing me what&#8217;s going on<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p>There&#8217;s also a silent-alarm mode that just takes pictures and emails them to me.<\/p>\n<figure id=\"attachment_1038\" aria-describedby=\"caption-attachment-1038\" style=\"width: 223px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/08\/burglar.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-1038\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2016\/08\/burglar-223x1024.jpg\" alt=\"Burglar alarm\" width=\"223\" height=\"1024\" \/><\/a><figcaption id=\"caption-attachment-1038\" class=\"wp-caption-text\">The email I get when the burglar alarm goes off<\/figcaption><\/figure>\n<p>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).<\/p>\n<p>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 &#8220;leaving home sequence&#8221; (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!<\/p>\n<h2>Configuring a Burglar Alarm in Home Assistant<\/h2>\n<p>When I started, the SMTP email notification component in HA didn&#8217;t support in-line photo attachments so I added that capability with <a href=\"https:\/\/github.com\/home-assistant\/home-assistant\/pull\/2738\">PR #2738<\/a>. It has been merged and made it into the 0.26 release.<\/p>\n<p>This system uses my Ecolink Z-wave door sensor and my Z-wave Aeotech Multisensor 6 as actuators (see <a href=\"https:\/\/partofthething.com\/thoughts\/?p=937\">an older post<\/a> for more info about these). It uses the <a href=\"https:\/\/home-assistant.io\/components\/shell_command\/\">shell_command<\/a> component to to take snapshots from my Foscam IP camera, and to issue LIRC commands\u00a0 that turn on the stereo. It uses the <a href=\"https:\/\/home-assistant.io\/components\/lirc\/\">lirc component<\/a> with my $1 IR receiver (hooked directly to the Pi&#8217;s GPIO pins) to receive the disarm command (see <a href=\"https:\/\/partofthething.com\/thoughts\/?p=1010\">my earlier post <\/a>on that). It uses the <a href=\"https:\/\/home-assistant.io\/components\/notify.smtp\/\">smtp component<\/a> to send the email notifications. Here are some config entries:<\/p>\n<h4>Burglar automation<\/h4>\n<p>This triggers the alarm. (The second condition is important for those times when the disarm button isn&#8217;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.<\/p>\n<pre class=\"lang:yaml\">- alias: Burglar Alarm Door\r\n\u00a0 trigger:\r\n\u00a0\u00a0\u00a0 platform: state\r\n\u00a0\u00a0\u00a0 entity_id: binary_sensor.ecolink_doorwindow_sensor_sensor_3\r\n\u00a0\u00a0\u00a0 from: 'off'\r\n\u00a0\u00a0\u00a0 to: 'on'\r\n\u00a0 action:\r\n\u00a0\u00a0\u00a0 service: script.turn_on\r\n\u00a0\u00a0\u00a0 entity_id: script.burglar\r\n\u00a0 condition:\r\n\u00a0\u00a0\u00a0 - platform: state\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 entity_id: input_boolean.burglar_alarm_door\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 state: 'on'\r\n\u00a0\u00a0\u00a0 - platform: state\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 entity_id: script.burglar\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 state: 'off'<\/pre>\n<h4>Burglar script<\/h4>\n<p>This is the sequence that fires off all the commands once triggered. (Note that the <strong>images <\/strong>input to the emailer is the new thing I added).<\/p>\n<pre class=\"lang:yaml\">\u00a0 burglar: \r\n\u00a0\u00a0\u00a0 alias: Burglar Alarm\r\n\u00a0\u00a0\u00a0 sequence:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 - service: shell_command.stereo_line_in\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 - service: shell_command.snapshot\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data: \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 filename: \/home\/pi\/snapshot1.jpg\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 - service: shell_command.fix_camera_jpg\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data: \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 filename: \/home\/pi\/snapshot1.jpg\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 - delay:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 seconds: 1\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 - service: shell_command.intruder_music\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 - service: script.turn_on\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 entity_id: script.night_reminder\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 - service: shell_command.snapshot\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data: \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 filename: \/home\/pi\/snapshot2.jpg\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 - service: shell_command.fix_camera_jpg\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data: \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 filename: \/home\/pi\/snapshot2.jpg\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 - service: notify.emailer\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 title: 'Intruder alert'\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 message: 'Intruder alert at apartment!!'\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 images: \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - \/home\/pi\/snapshot1.jpg\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - \/home\/pi\/snapshot2.jpg\r\n<\/pre>\n<h4>Snapshot shell_commands<\/h4>\n<p>The convert command (from imagemagick) is required to avoid MIME errors in the SMTP python library (fixes headers somehow).<\/p>\n<pre class=\"lang:yaml\">shell_command:\u00a0 \r\n    snapshot: wget \"http:\/\/camip:port\/snapshot.cgi?user=&lt;user&gt;&amp;pwd=&lt;password&gt;&amp;resolution=32\" -O {{filename}}\r\n    fix_camera_jpg: convert {{filename}} {{filename}}\r\n    intruder_music: mpg321 \/home\/pi\/Videos\/burglar.mp3\r\n\u00a0   stereo_line_in: irsend SEND_ONCE STEREO KEY_VIDEO1<\/pre>\n<h4>Input_booleans<\/h4>\n<p>To control arming and disarming.<\/p>\n<pre class=\"lang:yaml\">input_boolean:\r\n\u00a0 burglar_alarm_door:\r\n\u00a0\u00a0\u00a0 name: Burglar Alarm Door\r\n\u00a0\u00a0\u00a0 initial: off\r\n\u00a0\u00a0\u00a0 icon: mdi:lock-outline\r\n\u00a0 burglar_alarm_motion:\r\n\u00a0\u00a0\u00a0 name: Burglar Alarm Motion\r\n\u00a0\u00a0\u00a0 initial: off\r\n\u00a0\u00a0\u00a0 icon: mdi:lock-outline\r\n\u00a0 burglar_alarm_silent:\r\n\u00a0\u00a0\u00a0 name: Silent Burglar Alarm\r\n\u00a0\u00a0\u00a0 initial: off\r\n\u00a0\u00a0\u00a0 icon: mdi:lock-outline<\/pre>\n<p>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&#8217;s code and fiddle around is super rewarding and fun.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/partofthething.com\/thoughts\/raspberry-pi-home-assistant-diy-burglar-alarm\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Raspberry Pi + Home Assistant DIY Burglar Alarm<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[69,75],"tags":[],"class_list":["post-1037","post","type-post","status-publish","format-standard","hentry","category-electronics-and-physics","category-home-automation"],"_links":{"self":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/1037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/comments?post=1037"}],"version-history":[{"count":10,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/1037\/revisions"}],"predecessor-version":[{"id":1115,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/1037\/revisions\/1115"}],"wp:attachment":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/media?parent=1037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/categories?post=1037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/tags?post=1037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}