Home Assistant Manual Security System with Xiaomi Gateway as Siren

I’ve owned a iSmartAlarm system for over 5 years, it’s worked well until early this year when iSmartAlarm went bankrupt rendering my security system’s functionality to being a paperweight. I’ve been researching to buy another system, however I’m not keen to invest in another solution and becoming beholden another cloud solution that will inevitably go bust at some point.

Anyway below is a breakdown of my new alarm system using Home Assistant with a Xiaomi Aqara Gateway and Sensors. I will not go through how to connect any devices\integrations, but i will share the code I used to for Home Assistant to mimic the behaviours of the iSmartAlarm system including countdown warnings etc…

Let me know if you have any questions or feedback

Home Assistant Code for the Manual Alarm:

alarm_control_panel:
  - platform: manual
    name: Home Security
    code: 0000
    code_arm_required: false # Don't require the code to arm the alarm
    disarm_after_trigger: false # Arm again after triggering
    arming_time: 300 # Delay from arming and becoming armed, eg. to leave the house.
    delay_time: 120 # Allow time to disarm the alarm before it triggers, eg. when arriving home
    trigger_time: 600 # Amount of time the alarm is triggered for
    disarmed:
      trigger_time: 0 # Ensure the alarm can never be directly triggered when disarmed
    armed_home:
      arming_time: 0 # Leave no delay between arming -> armed
      delay_time: 0 # Leave no delay between pending -> triggered

Xiaomi Gateway Siren and Countdown Audio:
Note: these are required as the Xiaomi gateway alarms/ringtones only run for a few seconds. These scripts allow the countdown warning (when arming) and the alarm (when security system is triggered) to play continously when activated.

script:
  security_alarm_loop_1:
    sequence:
    - service: script.turn_off
      target:
        entity_id:
          - script.security_countdown_loop_1
          - script.security_countdown_loop_2
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: f0b429ccxxxx
        ringtone_id: 2 
        ringtone_vol: 100
    - service: script.turn_off
      entity_id: script.security_alarm_loop_2
    - delay: "00:00:6"
    - service: script.turn_on
      entity_id: script.security_alarm_loop_2
  security_alarm_loop_2:
    sequence:
    - service: script.turn_off
      target:
        entity_id:
          - script.security_countdown_loop_1
          - script.security_countdown_loop_2
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: f0b429ccxxxx
        ringtone_id: 1 
        ringtone_vol: 100
    - service: script.turn_off
      entity_id: script.security_alarm_loop_1
    - delay: "00:00:10"
    - service: script.turn_on
      entity_id: script.security_alarm_loop_1
  security_countdown_loop_1:
    sequence:
    - service: script.turn_off
      entity_id: script.security_countdown_loop_2
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: f0b429ccxxxx
        ringtone_id: 29 
        ringtone_vol: 10
    - delay: "00:00:15"
    - service: script.turn_on
      entity_id: script.security_countdown_loop_2
  security_countdown_loop_2:
    sequence:
    - service: script.turn_off
      entity_id: script.security_countdown_loop_1
    - service: xiaomi_aqara.play_ringtone
      data:
        gw_mac: f0b429ccxxxx
        ringtone_id: 29 
        ringtone_vol: 10
    - delay: "00:00:15"
    - service: script.turn_on
      entity_id: script.security_countdown_loop_1
  security_stop_loop_sounds:
    alias: Stop Security Sound Scripts on Gateway
    sequence:
    - service: script.turn_off
      target:
        entity_id:
          - script.security_countdown_loop_1
          - script.security_countdown_loop_2
          - script.security_alarm_loop_1
          - script.security_alarm_loop_2
    - service: xiaomi_aqara.stop_ringtone
      data:
        gw_mac: f0b429ccxxxx

Automation Events for Alarm Status:
These include automations for what happens when:

  1. Home Security is Disarmed
  2. Home Security is in Arming State (i.e. play countdown warning sound)
  3. Home Security is in Armed State
  4. Event has occurred whilst Home Security is in Armed State
  5. Home Security is in Pending State (i.e. play countdown warning sound)
  6. Home Security is in Triggered State (i.e. play the Alarm Siren)

You can add triggers, actions and notifications as you require

automation:
  - alias: SECURITY Disarmed State
    trigger:
      - platform: state
        entity_id: alarm_control_panel.home_security
        to: "disarmed"
    action:
      - service: media_player.volume_set
        data:
          volume_level: 0.6
        target:
          entity_id: media_player.nest_hub_max
      - service: tts.google_translate_say
        entity_id: media_player.nest_hub_max
        data:
          message: 'Security System has been Disarmed'
      - service: script.security_stop_loop_sounds
  - alias: SECURITY Arming State
    trigger:
      - platform: state
        entity_id: alarm_control_panel.home_security
        to: "arming"
    action:
      - service: media_player.volume_set
        data:
          volume_level: 0.6
        target:
          entity_id: media_player.nest_hub_max
      - service: tts.google_translate_say
        entity_id: media_player.nest_hub_max
        data:
          message: 'Security System is Arming, you have 5 minutes'
      - service: script.security_countdown_loop_1
  - alias: SECURITY Armed_Away State
    trigger:
      - platform: state
        entity_id: alarm_control_panel.home_security
        to: "armed_away"
    action:
      - service: script.security_stop_loop_sounds
      - service: media_player.volume_set
        data:
          volume_level: 0.6
        target:
          entity_id: media_player.nest_hub_max
      - service: tts.google_translate_say
        entity_id: media_player.nest_hub_max
        data:
          message: 'Security System is now armed'
  - alias: SECURITY Triggered by the Sensors whilst Armed_Away
    trigger:
      - platform: state
        entity_id: binary_sensor.door_window_sensor_158d0001b7xxxx #front door
        to: "on"
      - platform: state
        entity_id: binary_sensor.door_window_sensor_158d0001b7xxxx #rear sliding door
        to: "on"
    condition:
      - condition: state
        entity_id: alarm_control_panel.home_security
        state: armed_away
    action:
      - service: alarm_control_panel.alarm_trigger
        target:
          entity_id: alarm_control_panel.home_security
  - alias: SECURITY Pending State
    trigger:
      - platform: state
        entity_id: alarm_control_panel.home_security
        to: "pending"
    action:
      - service: media_player.volume_set
        data:
          volume_level: 0.6
        target:
          entity_id: media_player.nest_hub_max
      - service: tts.google_translate_say
        entity_id: media_player.nest_hub_max
        data:
          message: 'Security is armed, alarm countdown commencing'
      - service: script.security_countdown_loop_1
  - alias: SECURITY Triggered State
    trigger:
      - platform: state
        entity_id: alarm_control_panel.home_security
        to: "triggered"
    action:
      - service: script.security_alarm_loop_1

BONUS Content:
Use a Xiaomi/Aqara button to Arm and Disarm the alarm system

automation:
  - alias: AQARA Button press
    trigger:
      platform: event
      event_type: xiaomi_aqara.click
      event_data:
        entity_id: binary_sensor.switch_158d00010axxxx
        click_type: single
    action:
      - service: >
          {% if is_state("alarm_control_panel.home_security", "disarmed") %}
            alarm_control_panel.alarm_arm_away
          {% else %}
            alarm_control_panel.alarm_disarm
          {% endif %}
        data:
          entity_id: alarm_control_panel.home_security
          code: 0000
  - alias: AQARA Button LONG press
    trigger:
      platform: event
      event_type: xiaomi_aqara.click
      event_data:
        entity_id: binary_sensor.switch_158d00010axxxx
        click_type: long_click_press
    action:
      - service: >
          {% if is_state("alarm_control_panel.home_security", "disarmed") %}
            alarm_control_panel.alarm_arm_away
          {% else %}
            alarm_control_panel.alarm_disarm
          {% endif %}
        data:
          entity_id: alarm_control_panel.home_security
          code: 0000

Lovelace YAML to display your cameras in the main view only when the Home Security is in any other state outside of “disarmed”. NOTE: To save space on the UI, I use BlueIris to create a single camera that combines all my security cameras into a single feed

views:
  - id: main-tab  # title: Main
    title: Main
    badges:
    cards:
      - id: home-cameras-when-armed  
        type: conditional
        conditions:
          - entity: alarm_control_panel.home_security
            state_not: disarmed
        card:
          type: picture-entity
          title: Home Cameras
          entity: camera.all_cameras
          camera_image: camera.all_cameras
          show_info: false
          #tap_action: dialog
          show_name: false
          show_state: false
3 Likes

I would be super grateful to use this, as Im having all the necessary devices which you use on your Security System. How ever, im just getting errors with these codes. I managed to get the Alarm panel work, but thats it.

Would you be that kind and help me a bit with these. Im a complete beginner with all kind of coding ig QBasic from the early 90s does not count. Thanks a million times already! :slight_smile:

What issues are you seeing, share here and I can try to help when I can…

The hardest part of this process would be to get the gateway working with Home Assistant first.

Thanks for Your response, but somehow I managed to get the entire system up and running. Have you got any tips how to alarm and disalarm the system based on locations?

Glad you got it going… I use a hidden button to arm and disarm the system. I’m not using location triggers myself.