Setting up an alarm system using Z-Wave door sensors and Sonos speakers

Setting up an alarm system is fairly intrinsic to a lot of Home Assistant implementations. If you're looking at ADT, Simplisafe...other options, you get their system and their controls - and you pay a monthly subscription for the good stuff. Home ownership, you get a discount on your home insurance if you have a security system that hits a third-party reporting service. Home Assistant doesn't get you there until you add something like Noonlight.

Noonlight will be a future addition - this is already a long process.

Materials:

  • Door Sensors (I used Ecolink Zwave Plus [DWZWAVE2.5-ECO], $30 for each set of sensors - so $60 for front/back door)
  • A Z-Wave USB for Home Assistant (I used Nortek HUSBZB-1, $50)
  • Speakers (I used some early-model Sonos speakers)
  • Probably a tablet to act as a disarm/arm location (I used Amazon Fire 7 (2019) $50 on Amazon) - you can also use any phone with the HA app, or any computer.

Steps to get it working:

  1. Get your door sensors connected to your Z-Wave coordinator
  2. Add basic alarm code to your configuration.yaml file
  3. Create "triggering" automations for your armed_away and armed_home
  4. Create a "alarm enters pending state" - optional, but nice if you want the heads-up that in 30 seconds, a siren will go off.
  5. Create an "alarm enters triggered state" - this is where the siren code will go
  6. Set up the tablet (covered in part 2)

Adding sensors are relatively easy

The only complex part of it is pairing the sensor and coordinator. The coordinator is fairly basic. You're going to go to Configuration > Integrations > Z-Wave JS, then select "Add Node". Your sensor will have some kind of button (or perhaps just putting in a battery), where it will start pairing. 

Add Node - you'd think...device? But no.
 

The alarm code is fairly stock

You can use this Home Assistant page as the basis.

I added comments to the yaml

I added a 30 second delay time to away and home, because when my sensors trigger, I have an mp3 calling out "Alarm has been triggered" (just in case my partner or I didn't know the alarm was set). When it is in the "delay_time", the alarm will be "pending", which we'll use in an automation.
 

Creating armed_away and armed_home triggers

The alarm Home Assistant page I linked before has some of this content under the "SENSORS" portion. I don't have motion sensors in the current set-up, but armed_home is identical except for the automation's name and "armed_home" instead of "armed_away" in the yaml. (If you're looking for cut-and-paste, just grab text from that webpage, and make changes as needed. I just had to change the binary_sensor entity_id's to match my system, as well as changing the entity_id name for my alarm control panel.

Armed_away yaml

 At this point, armed_away and armed_home are working. They don't DO anything, but you can drop an alarm panel in a Lovelace tab, and it should work. You can arm and disarm the panel, and if you look at Developer Tools, you should be able to find alarm_control_panel.alarm to have a status...armed, disarmed, pending...

Create a "Pending state" automation for the triggered alarm

This is where I went off the rails a little. I used the automation creation assistant, creating a blank automation.

- alias: Alarm enters Pending state
  description: ''
  trigger:
  - platform: state
    entity_id: alarm_control_panel.alarm
    to: pending
  condition: []
  action:
  - service: sonos.snapshot
    data:
      entity_id: media_player.living_room
  - service: media_player.volume_set
    target:
      entity_id: media_player.living_room
    data:
      volume_level: 0.35
  - service: media_player.play_media
    target:
      entity_id: media_player.living_room
    data:
      media_content_type: music
      media_content_id: http://192.168.4.51:8123/local/AlarmTriggered.mp3
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: sonos.restore
    data:
      entity_id: media_player.living_room
  mode: single
 

I initially tried to code the yaml by hand, but it never quite worked. It helped in some small ways, because when I used the automation creation wizard, I had some idea what I was looking for. The "-service: sonos.snapshot" and "sonos.restore" are great - snapshot copies your queue and the Sonos volume level, allowing you to play an alarm...and letting you come back to your queue/volume with the restore command. The "media_content_id" is just my home assistant IP and the standard port number of 8123.

You'll store whatever AlarmTriggered.mp3 in the "www" folder in your Home Assistant. (Using File Editor, select the folder icon, and scroll down to the www folder. If you don't have it for whatever reason, you can create it.) Once you have the www folder, select it, and upload your mp3. I gave it a 5-second delay, to give a gap between going back to possibly playing music. As I refine it, I might just make it longer...that way there's no music playing while you try to disable the alarm.

My "AlarmTriggered" mp3 is just me saying "Alarm has been triggered". It's really just starting that 30-second "pending" window - giving us time to hustle over to the tablet, or deactivate the alarm on a phone app.

Create the ACTUAL alarm triggered automation

Both armed_away and armed_home funnel into the same result: the alarm_control_panel.alarm goes into the triggered state.

- alias: Alarm enters triggered state
  description: ''
  trigger:
  - platform: state
    entity_id: alarm_control_panel.alarm
    to: triggered
  condition: []
  action:
  - service: notify.notify
    data:
      message: Alarm is going off.
  - service: sonos.snapshot
    data:
      entity_id: media_player.living_room
  - repeat:
      until:
      - condition: state
        entity_id: alarm_control_panel.alarm
        state: disarmed
      sequence:
      - service: media_player.play_media
        target:
          entity_id: media_player.living_room
        data:
          media_content_id: http://192.168.4.51:8123/local/sirenblare.mp3
          media_content_type: music
      - delay:
          hours: 0
          minutes: 0
          seconds: 3
          milliseconds: 0
  - service: sonos.restore
    data:
      entity_id: media_player.living_room
  mode: single

The automation performs multiple actions. First, it sends a notify.notify - basically any entity that has a notify action gets the message. Currently, it's just phones, but maybe I'll rig up a magic mirror in the bathroom. Second, it does the sonos.snapshot I mentioned previously. Third, it starts a "repeat-until" sequence. Until the alarm_control_panel.alarm goes to the "disarmed" state, it will continue cycling the siren.mp3 and a short delay.  Finally, it will use sonos.restore to bring back the Sonos queue and volume level, once it leaves the repeat-until state.

I still need to test a proper volume level to the sirenblare.mp3, but I'll wait until the dog is out and about, so I don't scar it for life. (The sirenblare.mp3 is also in the www folder mentioned earlier.)

Setting up a Fire 7 as a security dashboard

There's a variety of ways to do this, but I wanted a cheap tablet to run just the alarm control panel. My research led me to installing LineageOS on a Fire 7, and buying a copy of Fully Kiosk browser.

I'll cover this in a future topic.

Comments

Popular posts from this blog

Home Assistant Controlling Octoprint through MQTT - Chapter 1

Setting up MQTT on HA and Octoprint

Home Assistant Controlling Octoprint through MQTT - Chapter 2