Complex automation problem - combining timer, motion sensor, button and light

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.

  1. If the timer finishes but the occupancy is still detected, the light turns off. Moving within the room does not turn it on.
  2. 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:

  1. 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.

  2. 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.

  3. using waiting time in the automation does not allow me to make the timer visible in Lovelace.

  4. 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

I addressed this slightly differently. I have one automation that triggers on motion, turns on the light, delays 5 minutes, and turns off the light. However, I have the mode of the automation set to “restart” so that if another motion detection happens before the 5 minutes, it cancels the first automation and starts the 5 minute timer again. This works well for me!
-David

How do you turn off the light? By triggering state of the motion sensor to “clear”? That would work well.

Your approach is not a solution to my problem. If I understand it correctly, you do not use timer for the countdown that would be used in the automation and as a countdown visible in Lovelace:
image

I am using the restart mode as well. As I stated above, the problem is that the motion sensor does not produce an event of “motion detected” when it is already detected, so this way I cannot restart the timer, and when the timer finishes, the lights turn off but do not turn on because the motion cannot be triggered as it is already detected. So I am thinking whether the timer can be somehow bound with the motion sensor or whether there is another solution e.g. to have the countdown restarted when the motion is (re)detected.

I don’t have any need to see the timer so I don’t track it. However, you could have the timer reset each time the motion is detected, so you would get the same thing as what you have. The only limitation is that you have to make sure the timer is longer than the built-in blackout window of the motion sensor. For example, IKEA motion sensors will send a motion event, and then not detect changes in motion for 2 or 3 minutes. So as long as the timer is longer than that, you will reset the timer if there is motion after that. There is no way with those sensors to reset it sooner. That’s just a limitation of the sensor and there is nothing you can do about it unless you can reconfigure the sensors. (Also note that these passive IR sensors cannot have zero reset times as they compare the current IR scene to the average of some past time, so they need time to rebuild their averages.)

To have a timer is my objective, as I can use any automation based on the knowledge of its value and state.

No I cannot, that’s the point. You cannot track the beginning of a repeated motion. You can track the change of a state from “clear” to “detected”, when you are in the state “detected” the occupancy_timeout begins countdown but you cannot track its reset because it’s probably hardcoded in the hardware. If you change the timeout to 1s or even 15s, the Z2M report’s clear but immediate trigger to motion is not reflected in Home Assistant, so it does not work (as I stated in my initial post, section The problems, point 2).

The whole point is that you cannot track the moment when actual motion stops and the countdown of timeout_occupancy begins and recurring motion does not produce an event. Actually, some countdown is happening in the hardware because that is what you can set in the Z2M settings timeout_occupancy but it does not work. The moment reported to HA is when the occupancy changes to “clear” after the timeout and that’s minutes (90-120s usually) after the real motion is over.

So the whole objective is to use the timer in HA instead of timout_occupancy in the hardware that is configurable in Z2M but not working or finding another way I am not aware of.

It seems I solved it.

Solution: the timer (helper) must be longer than the timer (occupancy_timeout) in the hw motion sensor. Then the restart mode in the automation does its job.

However, it still does not work if you set occupancy_timeout on 1s because the hw motion sensor is somehow limited to its factory default minimum (90-120s, dunno exactly), shorter periods does not trigger motion detection.

Home Assitant has really well-done timer services and timer events and events generally. It might be also a solution to read the events from the motion sensor (clear) instead of change of the state but I did not try.

Rright now, I can have the following

  • turn a light by a button > timer starts counting
  • move into the room > timer REstarts counting
  • move out from the room, hw motion sensor stops detecting (this is unfortunately 90s) period
  • move into the room > restarts timer

What is not perfect and not solved yet is the moment when I move to the room during the timeout_occupancy > timer DOES NOT RESTART

I am surprised this is not a common problem that could be found in this community discussion.

1 Like

Hello, did you solve it? I’m looking for something related…

Seems to me he’s overthinking it. Have a look at this as a starting point:

Motion activated lights automation.