Aqara Motion Sensor Hack for 5 sec

I had those in ZHA, too.
I found out that it is not enough to change the state of the entity in HA.
Instead I had to change the state of the ZHA device itself via a ZHA service call!
After that, all ghost switching were gone…
btw. I changed the timeout to 4 seconds, so that a motion detection after exactly 5 seconds is not missed (had that sometimes before…).

You could use the automatisation above:

alias: Aqara motion sensor - reset ZHA cluster
description: ''
trigger:
  - entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
    platform: state
    from: 'off'
    to: 'on'
condition:
  - condition: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
    state: 'on'
action:
  - service: zha.set_zigbee_cluster_attribute
    data:
      ieee: '00:15:8d:00:xxxxxxxx'  # your Aqara IEEE
      endpoint_id: 1
      cluster_id: 1280
      cluster_type: in
      attribute: 2
      value: 0
mode: single

or a short appdaemon app:

apps.yaml:

aqara:
  module: aqara
  class: Aqara
  timeout: 4
  motion_sensors:
    - binary_sensor.motion_eg_flur_ias_zone
    - binary_sensor.motion_eg_flur_innen_ias_zone
    - binary_sensor.motion_eg_kueche_ias_zone
    - binary_sensor.motion_eg_wohnzimmer_esstisch_ias_zone
    - binary_sensor.motion_eg_wohnzimmer_wohnsofa_2_ias_zone
    - binary_sensor.motion_eg_wohnzimmer_wohnsofa_1_ias_zone
    - binary_sensor.motion_eg_hauswirtschaftsraum_ias_zone
    - binary_sensor.motion_eg_bad_ias_zone
    - binary_sensor.motion_eg_treppenhaus_ias_zone
    - binary_sensor.motion_og_flur_ias_zone
    - binary_sensor.motion_og_bad_ias_zone
    - binary_sensor.motion_og_k_ias_zone
    - binary_sensor.motion_og_l_ias_zone
    - binary_sensor.motion_og_n_ias_zone
    - binary_sensor.motion_og_schlafzimmer_ias_zone
    - binary_sensor.motion_og_abstellraum_ias_zone
  motion_IEEEs:
    - 00:15:8d:00:05:49:bb:0b #binary_sensor.motion_eg_flur_ias_zone
    - 00:15:8d:00:05:d0:07:96 #binary_sensor.motion_eg_flur_innen_ias_zone
    - 00:15:8d:00:05:d0:07:97 #binary_sensor.motion_eg_kueche_ias_zone
    - 00:15:8d:00:05:cf:4c:e3 #binary_sensor.motion_eg_wohnzimmer_esstisch_ias_zone
    - 00:15:8d:00:05:6a:7d:85 #binary_sensor.motion_eg_wohnzimmer_wohnsofa_2_ias_zone
    - 00:15:8d:00:06:41:1c:6e #binary_sensor.motion_eg_wohnzimmer_wohnsofa_1_ias_zone
    - 00:15:8d:00:05:cf:4d:2c #binary_sensor.motion_eg_hauswirtschaftsraum_ias_zone
    - 00:15:8d:00:05:49:bb:53 #binary_sensor.motion_eg_bad_ias_zone
    - 00:15:8d:00:05:bd:25:c6 #binary_sensor.motion_eg_treppenhaus_ias_zone
    - 00:15:8d:00:05:7f:8b:37 #binary_sensor.motion_og_flur_ias_zone
    - 00:15:8d:00:05:7f:8a:e6 #binary_sensor.motion_og_bad_ias_zone
    - 00:15:8d:00:05:4c:e0:07 #binary_sensor.motion_og_k_ias_zone
    - 00:15:8d:00:06:33:1c:8c #binary_sensor.motion_og_l_ias_zone
    - 00:15:8d:00:05:4c:c1:a6 #binary_sensor.motion_og_n_ias_zone
    - 00:15:8d:00:06:3d:dd:a3 #binary_sensor.motion_og_schlafzimmer_ias_zone
    - 00:15:8d:00:04:5e:bb:09 #binary_sensor.motion_og_abstellraum_ias_zone

aqara.py:

import json
import time
import hassapi as hass

MODULE = 'aqara'
CLASS = 'Aqara'

CONF_TIMEOUT = 'timeout'
CONF_MOTION_SENSORS = 'motion_sensors'
CONF_MOTION_IEEES = 'motion_IEEEs'

DEFAULT_TIMEOUT = 4

class Aqara(hass.Hass):
  def initialize(self):
    """Initialize the Aqara app."""
    # define global variables
    global motion_sensors
    motion_sensors = self.args.get(CONF_MOTION_SENSORS, [])
    global timeout
    timeout = self.args.get(CONF_TIMEOUT, DEFAULT_TIMEOUT)
    global motion_ieees
    motion_ieees = self.args.get(CONF_MOTION_IEEES, [])

    # create listeners
    for entity in motion_sensors:
      self.log("Aqara: Listen to entity: %s with timeout: %s", entity, timeout, level="INFO")
      self.listen_state(self.motion_sensor_state_off, entity, new = "on", duration = timeout)

  def motion_sensor_state_off(self, entity, attribute, old, new, kwargs):
    """Set motion sensor state to off"""
    current_ieee = motion_ieees[motion_sensors.index(entity)]

    # reset motion status in ZHA
    self.log("Aqara: reset motion status of entity % s in ZHA, ieee: %s", entity, current_ieee, level="INFO")
    self.call_service("zha/set_zigbee_cluster_attribute", ieee = current_ieee, endpoint_id = 1, cluster_id = 1280, cluster_type = "in", attribute = 2, value = 0)

btw.: If somebody knows how to query the IEEE of an entity_ID in appdaemon or how to construct the args much nicer (device_ID and IEEE not separated), feel free to contribute :slight_smile:

Seb

3 Likes