Xiaomi Gateway - Alarm

No, that’s not possible for the time being.

Arm & disarm the manual alarm defined in Home Assistant, not what it is in Mi Home app.

So what are you try to make it?

Using a manual alarm defined within HA, be able to loop play gateway ringtones, to mimic the actual behavior of a real life alarm.

All started with one stupid bug in Mi Home. When alarm is triggered it shoots alarmsound right away. Since usually the disarm switch/panel is inside the door I couldn’t go in my house without triggering the alarm.

So I wanted to replicate the original Alarm feature in Mihome but in Home Assistant. And with these guys help I finally could do something similar, and now I have 15 seconds to disarm it. (couldn’t make their code work, so I made a way more stupid but working code)

Next step is intense testing on volume 1, and testing device tracker in AH and the final boss will be the Alarm+device tracker code.

At least this is what I try to make in a long term: Alarm function without interaction.

I’m intrested in youre code, can you please share it?
Thank you.

I’m interested too in your code if you share it.
Thx for your work.

You can use this

and this

Please share the code

@pplucky please can I have your code too?
Thanks in advance

Better late than never, so here it is:

####################################################
#                                                  #
#                  Alarm Platform                  #
#                                                  #
####################################################
alarm_control_panel:
  - platform: manual
    name: "Home Alarm"
    pending_time: 30
    trigger_time: 30
    disarm_after_trigger: false

####################################################
#                                                  #
#         Alarm/Gateway Mute Input Boolean         #
#                                                  #
####################################################
### Input boolean to mute gateway sound ###
input_boolean:
  mute_gateway_sounds:
    name: Mute
    icon: mdi:volume-off

####################################################
#                                                  #
#       Alarm Group with all movement sensors      #
#                                                  #
####################################################
group:
  all_motion:
    name: "All Motion Sensors"
    view: false
    control: hidden
    entities:
    - binary_sensor.motion_sensor_a
    - binary_sensor.motion_sensor_b
    - binary_sensor.motion_sensor_c

####################################################
#                                                  #
#                 Alarm Automations                #
#                                                  #
####################################################
automation:
### Play specific sound while alarm is arming ###
  - alias: "Alarm arm pending"
    trigger:
      platform: state
      entity_id: alarm_control_panel.home_alarm
      from: 'disarmed'
      to: 'pending'
    condition:
      condition: state
      entity_id: alarm_control_panel.home_alarm
      state: 'pending'
    action:
### Turn off gateway mute to play sound ###
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.mute_gateway_sounds
### Play ringtone sound in loop until arm is done ###
      - service: script.play_sound
        data:
          ringtone_id: 10006
          ringtone_vol: 4
          delay: 2

### Stop sound when alarm is disarmed or armed ###
  - alias: "Alarm Turn off or Armed"
    trigger:
      - platform: state
        entity_id: alarm_control_panel.home_alarm
        to: 'disarmed'
      - platform: state
        entity_id: alarm_control_panel.home_alarm
        to: 'armed_away'
      - platform: state
        entity_id: alarm_control_panel.home_alarm
        to: 'armed_home'
    action:
### Turn on gateway mute NOT to play sound ###
      - service: input_boolean.turn_on
        data:
          entity_id: input_boolean.mute_gateway_sounds
### Stop ringtone sound --> didn't seem to work due to loop ###
      - service: xiaomi_aqara.stop_ringtone
        data:
          gw_mac: !secret xiaomi_mac
### Wait 10 seconds & then turn off gateway mute ###
      - delay: 
          seconds: 10
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.mute_gateway_sounds

### Alarm trigger while armed away ###
  - alias: "Alarm trigger while armed away"
    trigger:
### Front Door opened ###
      - platform: state
        entity_id: binary_sensor.door_window_sensor_a
        to: 'on'
### Any movement detected ###
      - platform: state
        entity_id: group.all_motion
        to: 'on'
### Alarm status is armed away ###
    condition:
      condition: state
      entity_id: alarm_control_panel.home_alarm
      state: 'armed_away'
    action:
### Trigger alarm ###
      - service: alarm_control_panel.alarm_trigger
        entity_id: alarm_control_panel.home_alarm

### Alarm triggered ###
  - alias: "Alarm triggered"
    trigger:
      - platform: state
        entity_id:  alarm_control_panel.home_alarm
        to: 'triggered'
    action:
### Turn off gateway mute to play sound ###
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.mute_gateway_sounds
### Play ringtone sound in loop ###
      - service: script.play_sound
        data:
          ringtone_id: 10004
          ringtone_vol: 50
          delay: 2
### Send a notification ###
      - service: notify.iOS
        data:
          message: 'Alarm fired away'
          title: 'Home Assistant alert'
          data:
            push:
              category: "alarm"

### Alarm trigger while armed home ###
  - alias: "Alarm trigger while armed home"
    trigger:
### Front Door opened ###
      - platform: state
        entity_id: binary_sensor.door_window_sensor_a
        to: 'on'
### Alarm status is armed home ###
    condition:
      condition: state
      entity_id: alarm_control_panel.home_alarm
      state: 'armed_home'
    action:
### Trigger alarm ###
      - service: alarm_control_panel.alarm_trigger
        entity_id: alarm_control_panel.home_alarm

### Disarm Alarm when action is chosen from iOS ###
  - alias: Alarm Disarm from iOS
    trigger:
      platform: event
      event_type: ios.notification_action_fired
      event_data:
        actionName: SILENCE_ALARM
    action:
      - service: alarm_control_panel.alarm_disarm
        data:
          entity_id: alarm_control_panel.home_alarm

####################################################
#                                                  #
#               Alarm Customizations               #
#                                                  #
####################################################
homeassistant:
### All Alarm Automations ###
  customize_glob:
    "automation.alarm*":
       icon: mdi:alarm-light

  customize:
### Alarm ###
    alarm_control_panel.home_alarm:
      icon: mdi:alarm-light

####################################################
#                                                  #
#             Alarm Sound Play Scripts             #
#                                                  #
####################################################
### Play Provided sound in loop on Gateway ###
script:
  play_sound:
    alias: "Play Sound"
    sequence:
### Turn off gateway mute to play sound ###
      - condition: state
        entity_id: input_boolean.mute_gateway_sounds
        state: 'off'
### Play ringtone sound provided ###
      - service: xiaomi_aqara.play_ringtone
        data_template:
          gw_mac: !secret xiaomi_mac
          ringtone_id: "{{ ringtone_id }}"
          ringtone_vol: "{{ ringtone_vol }}"
### Wait & call another script that call this one ###
### in order to generate an infinite loop until gateway ###
### mute is turned on by some other action ###
      - delay: '00:00:{{ delay | int }}'
      - service: script.play_sound_loop
        data_template:
          ringtone_id: "{{ ringtone_id }}"
          ringtone_vol: "{{ ringtone_vol }}"
          delay: "{{ delay }}"

  play_sound_loop:
    alias: "Play Sound in Loop"
### Wait & call script that called this one ###
### in order to generate an infinite loop until ###
### gateway mute is turned on by some other action ###
    sequence:
      - delay: '00:00:{{ delay | int }}'
      - service: script.play_sound
        data_template:
          ringtone_id: "{{ ringtone_id }}"
          ringtone_vol: "{{ ringtone_vol }}"
          delay: "{{ delay }}"
5 Likes

Do you have some nice sounds stored here instead of the built-in?

Hey Plucky,

I took you well written script, but when I arm my system it starts looping the built-in countdown audio file (chaged it because I haven’t uploaded anything yet), but then the service logs goes crazy… it spams me with this and the whole HA seems to be slow:

EDIT: After a reboot - it now seems to be ok when arming - No errors. Unfortunately now it just started when the alarm is triggered - same error logs.

SOLVED: For some reason I had to run the script by arming → reboot. Then Arm again and trigger the alarm and reboot again. No error at this time!

Thanks

I need to check if I can still find the original ones I used to upload to the gateway

That would be nice :).

One question, I tried to set the volume to 0 in both arming and trigger and afterwards I restarted the HA. For some reason my gateway still just plays it with sound. Doesn’t the gateway know the value 0 or is the minimum 1 - do you know that?

Also are you using iOS?
If so, can you then perhaps share your conf for that?
I tried setting it up in my config file and I used your iOS automation to disarm it, but when I type in the code in the notification placeholder and clicks silencio, nothing happens :blush:

IS there someone with a big big heart who can give me the code updated to get working my mi gateway with different door/Windows sensors and ha? I’m a newbe and i realized Now i need to write in an updated way not as shown in this post or I’m wrong? Thanks

@pplucky thankz for all, where i write this code? configuration.yaml, automation.yaml or where?

Myself, I have a package with all this configuration, but it all depends on how you have your configuration structured: it may also be in configuration.yaml.

Thank you for sharing your code @pplucky. I think I’ve got it working. I had a few errors with the indentation here and there, yaml can be picky - but I managed to tackle that.

Tomorrow I’ll try and connect the arming sequence to my location tracker. And find a nicer sound for the ‘pending’ operation.

And I want to add some flashing lights when the alarm is triggered later. But first I must really, really go to sleep :wink:

Here they are, better late than never.

Yes, using iOS.

Aside from the configuration already shared above, I also have this:

ios:
  push:
    categories:
      - name: Alarm
        identifier: 'alarm'
        actions:
          - identifier: 'SILENCE_ALARM'
            title: 'Silence Alarm'
            activationMode: 'background'
            authenticationRequired: yes
            destructive: yes
            behavior: 'default'

After having this added to your configuration and restarted Home Assistant, don’t forget to go to the app and under Settings / Notification settings, click button ‘Update push settings’.

It works fine here, but my alarm has no code, not sure if that may be the issue.