Actually, there is at least one more use case. I’m using Aqara motion sensor with HW hack that sends updates every 5 seconds. But still, when it sends the data, there is hardcoded delay of 60 or 90 seconds (not sure at the moment). So even the sensor itself updates every 5 seconds, HA will not react, because it respects the hardcoded delay. I’ve created an AppDaemon app that reset that machine state inside the HA. Actually it set the motion state from ON to OFF in 5 seconds after motion was detected. If there is another motion detected, it again set it to ON in HA, which triggers the AppDaemon app, which set it to OFF. This repeats in loop until there is no more movement. This way I can much more precisely react on the real movement.
If anyone interested, this is the AppDaemon app:
import hassapi as hass
import json
import time
# MODULE = 'aqara_motion_reset'
# CLASS = 'AqaraMotionReset'
# CONF_TIMEOUT = 'timeout'
CONF_MOTION_SENSORS = 'motion_sensors'
DEFAULT_TIMEOUT = 5
class AqaraMotionReset(hass.Hass):
def initialize(self):
"""Initialize the Aqara app."""
if self.args["writedebug"] == 1:
self.set_log_level("DEBUG")
self.timeout = self.args["timeout"]
motion_sensors = self.args.get(CONF_MOTION_SENSORS, [])
for entity_id in motion_sensors:
# self.listen_state(self.motion_sensor_state_on, entity_id)
self.log("{} detected motion".format(entity_id), log = "debug_log", level = "DEBUG")
self.listen_state(self.motion_sensor_state_on, entity_id, new = "on")
def motion_sensor_state_on(self, entity_id, attribute, old, new, kwargs):
"""Set motion sensor state to on"""
if not entity_id:
return
if new != "on":
return
state = self.get_state(entity_id, attribute="all")
#self.set_state(entity_id, state="on", attributes=state.get("attributes", {}))
timeout = self.timeout
self.run_in(self.motion_sensor_state_off, timeout, entity_id=entity_id)
def motion_sensor_state_off(self, kwargs):
"""Set motion sensor state to off"""
entity_id = kwargs.get("entity_id")
if not entity_id:
return
state = self.get_state(entity_id, attribute="all")
self.set_state(entity_id, state="off", attributes=state.get("attributes", {}))
And this is how I call it from apps.yaml:
aqara_motion_reset:
module: aqara_motion_reset
class: AqaraMotionReset
timeout: 5
motion_sensors:
- binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone_kitchen
log: debug_log
writedebug: 0