The objective:
To control light with a motion sensor (and button) with a configurable timer keeping the light on until the timer finishes with a visible countdown on Lovelace and restarting the timer using different triggers (button, motion etc.).
The desirable outcome:
The timer restarts with each motion, respectively restarts the countdown when the last motion was just detected (not cleared). Respectively to use the timer (helper in HA) instead of timeout_occupancy in Z2M configuration (because setting to 1s does not work) so I can restart the timer by new immediate motion or by other means (button turns on).
The problems:
Occupancy_timeout of the motion sensor is not synchronizable with the timer and as such it is causing several logical problems.
- If the timer finishes but the occupancy is still detected, the light turns off. Moving within the room does not turn it on.
- During the state of detected motion, any other motion does not cause an event to restart the timer, only prolongs the occupancy_timeout leading to point 1 above.
I am using Aqara/Xiaomi RTCGQ01LM motion sensors. In some areas, I combine it with TuYa IH012-RT01.
Possible workarounds that are not perfect:
-
I can check whether the motion sensor is in the detected state when the timer finishes and restart it for another 30s and repeat this loop until the occupancy is clear. However, this makes the timer unprecise because it restarts unnecessarily, so configuring the timer for e.g. 5mins loses sense. I can do 5mins and then another 5mins but still, it is not 5mins from the last detected motion.
-
setting the occupancy_timeout to one second in zigbee2mqtt, however, this simply does not work because the hardware (Aqara, Tuya has only 30/60/120 possibilities) is still in the “detected” mode, while the home assistant sees it as clear. Result: motion sensor does not detect motion in this period between the cleared state in HA (even in Z2M) and the clear state in the hardware itself.
-
using waiting time in the automation does not allow me to make the timer visible in Lovelace.
-
Newly detected motion during the automation in progress and restarting the timer will enable me to turn off the light exactly after the configured time (e.g. 5mins) from the ‘last’ motion detection, however, when a motion sensor already detects motion, new motion only prolongs it, it does not create a new event, so we are back in the problem in the point 1. The timer finishes, and occupancy remains detected effectively barring the automation to restart.
There are possibly simply other configurations achieving similar states but the whole point is that I’d like to see a countdown timer on Lovelace from the moment of the last detected state, which might not be possible at all.
The workaround no. 1 I currently use looks as follows, it is a night light in the hall working only over night:
alias: LIGHTS - Hall - LED strip lights up on motion (sun/20mins offset)
description: ""
trigger:
- platform: state
entity_id:
- light.hall_1
from: "on"
to: "off"
- platform: state
entity_id:
- light.hall_2
from: "on"
to: "off"
- platform: state
entity_id:
- binary_sensor.motion_hall_group
from: "off"
to: "on"
condition:
- condition: state
entity_id: light.hall
state: "off"
- condition: sun
after: sunset
after_offset: "-00:20:00"
before: sunrise
enabled: true
action:
- service: timer.start
data_template:
entity_id: timer.light_hall_led
duration: 00:00:{{ states('input_number.light_hall_led') | int }}
- service: homeassistant.turn_on
target:
entity_id: light.hall_led
data: {}
- wait_for_trigger:
- platform: state
entity_id:
- timer.light_hall_led
to: idle
- if:
- condition: state
entity_id: binary_sensor.motion_hall_group
state: "off"
then:
- service: homeassistant.turn_off
data: {}
target:
entity_id: light.hall_led
else:
- repeat:
until:
- condition: state
entity_id: binary_sensor.motion_hall_group
state: "off"
sequence:
- service: timer.start
data:
duration: "{{ states('input_number.motion_check') }}"
target:
entity_id: timer.light_hall_led
- wait_for_trigger:
- platform: state
entity_id:
- timer.light_hall_led
to: idle
- service: homeassistant.turn_off
data: {}
target:
entity_id: light.hall_led
mode: restart