Door-knock automation with Aqara Door + Vibration Sensor

Hello community,

I wanted to share my automation for recognizing when someone knocks on my door.
I use an Aqara Door Sensor and an Aqara Vibration Sensor.
I placed the vibration sensor in the center of the door on the inside where you are likely to knock on.

Here are the code snippets:

configuration.yaml (You may have to merge them correctly)

template:
  - binary_sensor:
      - name: "Door Knocked"
        unique_id: "doorknocked"
        state: ""
        device_class: vibration
        icon: mdi:door

Automation (If you use iOS you have to replace the Critial Notifications part, see docs here)

alias: Door Knock
description: >-
  Notify me when my door was knocked, resets the binary_sensor on detection
  after 10s, has a timeout of 30s set on the sensor
triggers:
  - domain: mqtt
    device_id: 070e7406423f2c47b272119ebf8ca8f2
    type: action
    subtype: vibration
    trigger: device
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.room_contact
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 5
        sequence:
          - parallel:
              - action: python_script.set_state
                data:
                  entity_id: binary_sensor.door_knocked
                  state: "on"
              - action: chime_tts.say
                metadata: {}
                data:
                  announce: true
                  message: Your door was knocked
                  chime_path: whistle
                target:
                  device_id: c30206b074fc182361cc5288f4bc67a3
              - action: notify.mobile_app_pixel_8_pro
                data:
                  title: Vibration detected
                  message: Your door was knocked
                  data:
                    ttl: 0
                    priority: high
          - delay:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
            enabled: true
          - action: python_script.set_state
            data:
              entity_id: binary_sensor.door_knocked
              state: "off"
      - conditions:
          - condition: state
            entity_id: binary_sensor.room_contact
            state: "on"
        sequence:
          - action: python_script.set_state
            data:
              entity_id: binary_sensor.door_knocked
              state: "off"
  - wait_for_trigger:
      - type: no_vibration
        device_id: 070e7406423f2c47b272119ebf8ca8f2
        entity_id: 16bedb5cb4357808803dbc0a106aa427
        domain: binary_sensor
        trigger: device
    alias: Wait for vibration sensor to reset
mode: single

Here is a variant of the automation that should also be compatible for ZHA users

alias: Door Knock (Only HA entities)
description: >-
  Notify me when my door was knocked, resets the binary_sensor on detection
  after 10s, has a timeout of 30s set on the sensor
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.vibration_sensor_vibration
    to: "on"
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.room_contact
            state: "off"
            for:
              hours: 0
              minutes: 0
              seconds: 5
        sequence:
          - parallel:
              - action: python_script.set_state
                data:
                  entity_id: binary_sensor.door_knocked
                  state: "on"
              - action: chime_tts.say
                metadata: {}
                data:
                  announce: true
                  message: Your door was knocked
                  chime_path: whistle
                target:
                  device_id: c30206b074fc182361cc5288f4bc67a3
              - action: notify.mobile_app_pixel_8_pro
                data:
                  title: Vibration detected
                  message: Your door was knocked
                  data:
                    ttl: 0
                    priority: high
          - delay:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
            enabled: true
          - action: python_script.set_state
            data:
              entity_id: binary_sensor.door_knocked
              state: "off"
      - conditions:
          - condition: state
            entity_id: binary_sensor.room_contact
            state: "on"
        sequence:
          - action: python_script.set_state
            data:
              entity_id: binary_sensor.door_knocked
              state: "off"
  - alias: Wait for vibration sensor to reset
    wait_for_trigger:
      - trigger: state
        entity_id:
          - binary_sensor.vibration_sensor_vibration
        to: "off"
mode: single

I have configured the sensor like this via Z2MQTT, should work similarly with ZHA.