Check that Doors/Windows are closed before Arming Alarm

Hi everybody,
I have created my alarm system with Home Assistant, here is the code.

alarm_control_panel:
  - platform: manual
    name: Antifurto
    code: !secret alarm_code
    code_arm_required: false

    trigger_time: 120

    disarmed:
      trigger_time: 0
    armed_home:
      delay_time: 0 
      arming_time: 20 
    armed_away:
      delay_time: 20
      arming_time: 20
    armed_night:
      delay_time: 20
      arming_time: 0

Basically, in my home there are several windows and door sensor, and my previous alarm system was able to tell me “Hey! You’re trying to arm the alarm while you have an open contact!”.

I want HA to check that doors and windows are actually closed before arming the alarm.

The check has to be different with respect to the armed_home / armed_away / armed_night, because different sensors are used for each scenario.

I really can’t understand how the template alarm works, and so I don’t know if it’s what I need.

Thanks in advance.

I think the template alarm is exactly what you want. The state of the template alarm will be the same as the state of your current alarm. The arm actions will check the conditions you set and only send the arming action to the current alarm panel if all checks pass.

The way it works is annoying though. If you use the first example here , you would add checks in the arm_home actions etc. before sending the commands to the base alarm panel. If you want to use a code, you have to check it inside each action. I did something similar in this code. Another gotcha is that the code cannot be in a secret if it is in a template.

1 Like

Hi @MatthewFlamm, thanks a lot for your help.

Following your suggestion, I need to change my configuration with the following one.

alarm_control_panel:
  - platform: manual
    name: Antifurto
    code: !secret alarm_code
    code_arm_required: false

    trigger_time: 120

    disarmed:
      trigger_time: 0
    armed_home:
      delay_time: 0 
      arming_time: 20 
    armed_away:
      delay_time: 20
      arming_time: 20
    armed_night:
      delay_time: 20
      arming_time: 0

- platform: template
    panels:
      template_alarm:
        value_template: "{{ states('alarm_control_panel.antifurto') }}" #my primary alarm panel, see above 
        
        arm_home:
          - condition: state
            entity_id: sensor.front_door
            state: 'closed'
          - service: alarm_control_panel.alarm_arm_home
            data:
              entity_id: alarm_control_panel.antifurto # main alarm panel, see above
              code: !secret alarm_code

But then, which alarm panel do I have to insert in the UI? I am confused, now I have 2 alarm panels and I don’t know if I have to use the antifurto (main and original alarm panel) or the template_alarm (new one, template).

Thanks a lot, I really appreciate your help! :slight_smile:

Use only the new one

1 Like

Hi @MatthewFlamm. Thanks a lot. I’m almost done!

Hi have two questions more. :innocent:

While from the Developer section I can enable without PIN and disable with PIN, in the UI (Lovelace) I can do whatever I want without PIN. My config is the following, I have only changed the entity.

- path: alarm
    title: Antifurto
    cards:
      - type: alarm-panel
        name: Antifurto
        entity: alarm_control_panel.safe_alarm
        states:
        - arm_home
        - arm_night
        - arm_away

#Edit MY BAD. Answer to the first question was in your example. Sorry :slight_smile:

Last question. When the Condition [of door closed] is not met, is there a way to execute something? I was thinking about an Alexa or Mobile App notification.

Thanks in advance for the precious help!

It is just like any other action. You can use notify platform for example if it doesn’t arm. I haven’t yet used this syntax, but I think you want an if/else type construct like https://www.home-assistant.io/docs/scripts#choose-a-group-of-actions

1 Like

Hello pierfrancescoelia
I am currently learning the HA and trying to integrate with Konnected. I am exactly in the same state as you were. viz to prevent the alarm panel from arming itself if one of the sensors is open. Is it possible for you to post your entire config code (minus any secret) stuff. It will help people like me who are newbies to use it as a working model and customize it. The documentation in HA is not very illuminating.

Thanks

Rsiva tech

1 Like

I use a simple value template to AND the values of all zones that need to be closed (except for overhead garage doors) in order for the system to arm.

          - condition: template
            value_template: >-
              {{ is_state("binary_sensor.11_front_door","off") and
              is_state("binary_sensor.13_office_side_door","off") and
              is_state("binary_sensor.14_office_sliding_door","off") and
              is_state("binary_sensor.15_kitchen_sliding_door","off") and
              is_state("binary_sensor.41_basement_door","off") and
              is_state("binary_sensor.43_garage_side_door","off") }}

I have the sensor named with the alarm system zone number so the physical alarm panel reports the exact same thing as HA. The garage doors are automatically closed later in the automation based on their zone status, which is why they are not in this list.

If this condition fails, I get a notify platform notification that the system is not ready, as well as a visual notification from a smart bulb.

        sequence:
          - service: script.turn_on
            data:
              variables:
                color: Yellow
                seconds: 1
            entity_id: script.script_alert_bulb_window_flash
          - service: notify.notify
            data:
              message: System not ready to be armed
              title: Security System

Hi @rsiva-tech, I am more than happy to share my code with you! :slight_smile:

Here it is my working code, feel free to ask in case of questions.

alarm_control_panel: # This is the STANDARD control panel, without any kind of check before arming.
  - platform: manual
    name: Standard Alarm
    code: !secret alarm_code
    code_arm_required: false # Don't need code to arm the alarm

    # When alarm is triggered, sets the n. of seconds of alarm siren.
    trigger_time: 120

    disarmed:
      trigger_time: 0
    armed_home:
      delay_time: 0 # delay when arming (to exit the house)
      arming_time: 0 # delay when triggering (to disable the alarm after you come back)
    armed_away:
      delay_time: 20
      arming_time: 0
    armed_night:
      delay_time: 20
      arming_time: 0
  
  - platform: template
    panels:
      safe_alarm: # This is the SMART alarm that checks that doors are closed before arming. In lovelace I use this alarm panel.
        name: Antifurto

        value_template: "{{ states('alarm_control_panel.standard_alarm') }}"

        arm_home: #Day Alarm
          - choose:
            - conditions: # If doors are closed
              - condition: state
                entity_id: binary_sensor.door1, binary_sensor.door2, binary_sensor.door3
                state: 'off'
              sequence:
                - service: alarm_control_panel.alarm_arm_home # arm
                  data:
                   entity_id: alarm_control_panel.standard_alarm
                   code: !secret alarm_code
            default:
              - service: notify.alexa_media # Else, Alexa says that some accesses are left open.
                data:
                  target: media_player.alexa
                  data:
                    type: announce
                    method: spoken
                  message: "Some doors are not closed. Alarm system not enabled."

        arm_night: #Night Alarm
          - choose:
            - conditions:
              - condition: state
                entity_id: binary_sensor.door1, binary_sensor.door2, binary_sensor.door3, binary_sensor.window1, binary_sensor.window2
                state: 'off'
              sequence: # Arm alarm if doors and windows selected are closed.
                - service: alarm_control_panel.alarm_arm_night
                  data:
                   entity_id: alarm_control_panel.standard_alarm
                   code: !secret alarm_code
            default: # Else Alexa says that something is open and so alarm is not active
              - service: notify.alexa_media
                data:
                  target: media_player.alexa
                  data:
                    type: announce
                    method: spoken
                  message: "Some doors are not closed. Alarm system not enabled."

        arm_away: # Total Alarm
          - choose:
            - conditions:
              - condition: state
                entity_id: binary_sensor.door1, binary_sensor.door2, binary_sensor.door3, binary_sensor.window1, binary_sensor.window2
                state: 'off'
              sequence: # Alarm if everything is closed
                - service: alarm_control_panel.alarm_arm_away
                  data:
                   entity_id: alarm_control_panel.standard_alarm
                   code: !secret alarm_code
            default: # Else Alexa says
              - service: notify.alexa_media
                data:
                  target: media_player.alexa
                  data:
                    type: announce
                    method: spoken
                  message: "Some doors are not closed. Alarm system not enabled."

        disarm:
          - condition: template
            value_template: "{{ code == '123123' }}" # Unfortunately it was - at the time I wrote the code - not possible to use secrets inside templates
          - service: alarm_control_panel.alarm_disarm
            data:
              entity_id: alarm_control_panel.standard_alarm
              code: !secret alarm_code

konnected: !include konnected.yaml # where I have declared sensors

Have a nice day! :smiley:

1 Like

Hello Pierfrancescoelia
Really appreciate posting the code. One of my gripes with the yaml is the strong formatting requirement (2 space indent) and when I cut and paste into the notepad++, there is some /t introduced inadvertently and the hass compiler gives trouble. I am trying to learn on these. Will experiment with your code and tune the system. I have some portions of the hass panel with konnected working but need to make it robust.

My system is very simple. Only 3 (wired) door sensors. But for experiementation, I attached an extra magnetic switch directly to the konnected board and a piezzo buzzer to see if the state changes are correct.

Thanks much

rsiva-tech

Richie
Noted your condition template where you AND the sensors. But how is this connected with the alarm panel on the UI. Do you use the original alarm panel or an “alarm panel template” like what pierfrances and Matthew have described.

If you can elucidate the binding, that will help and also a more detailed code if possible of how the sequence (bulb/notify) is triggered.

Sorry. I am still learning the templates and the yaml code.

Thanks again

rsiva-tech

I am using a physical alarm panel with many zones which is integrated to HA through another device. My code is part of an automation that arms the alarm system from a remote control. The automation is independent of the panel and its code, if one of the zones is open, the physical panel will fail to arm, so the automation catches the failure before the arm command is given.

The bulb/notify section of code is run if the AND template returns true, otherwise code is run that detects open garage doors, closes them, then arms the system with the desired alarm state. The automation is quite long and complex, and posting the whole thing will probably just lead to confusion, so I only put the most relevant code here.

Richie
Noted. I think your setup is very different than mine. I dont have any existing panel and am only going to use the HA interface to control the konncted HW board with sensors. I will try to absorb and learn from your templates for any future use. I may have to stick with Pierfrancescoeli’s suggestion and the template described in HA.

Thanks

rsiva

Thanks so much for your code, it saved my day. I have a question though. I do have some “automations” to enable the alarm with we leave the house and such, I’m assuming I will need to change those to call out the new safe_alarm right? otherwise it will bypass the check?

Thanks for the help.

1 Like

Hi! If you want to alarm only if the conditions are met, you arm the safe_alarm, otherwise the standard one.

1 Like

Hi,

Interesting and thanks for the sharing :blush:

I went with this:

But, I was wondering if you guys have a secondary automation for handling, lets say the backdoor where you maybe primarily enter the house. I guess you don’t want the alarm to sound immediately, but give you lets say 10, 15 or xx amount of time before it triggers.

I don’t want to set the “delay_time” parameter, because I do not want someone to be able to break a door and then get xx amount of time before the alarm sounds (I know that xx seconds might not do the difference). Does it make sense what I mean?

Before I was using the bwalarm and it was working well, where I had the possibilities to set which sensors should be delayed before triggering the alarm.

Any suggestions?

Hi there, I know it’s been a while that this topic has been discussed. I would like to have something like this on my HA. Right now I’m using the DSC/Envisalink integration. I don’t want to mess things up by bringing changes to my system that works oke right now.

But I would like to receive a notification on one of my echo devices telling me which contact sensor is still open, before I arm the system. At the moment I can arm the system with an Alexa routine by just say “Alexa I’m leaving Home”. The problem is that at this point nothing happens if a door or window is still open, because the system is not ready. Maybe a script can be triggered to check all contact before arming and if there is something still open notify which window or door is still open.

Any help is welcome for a suggestion on an automation or script that I can use in this routine.