Disabling the light switch for 1 second after being turned on using motion sensor

Hello! I have a zigbee smart light switch (to be exact: a dumb switch and a zigbee relay) and a zigbee motion sensor.

I set up an automation that turns on the light when the motion sensor detects motion.

Some family members, however, still habitually turn the light on using the switch. The problem is that sometimes the motion sensor turns the light on a split second before they flip the switch - causing the light to turn off immediately.

What I did to remedy that is create a second automation that triggers when the light is turned off and if the motion was last detected less than 1 second ago, turns the light back on.

Here is the automation:

alias: Keep the light on
description: ""
trigger:
  - platform: device
    type: turned_off
    device_id: 34bf888dce5a361ce0edf058532b59ba
    entity_id: switch.0xa4c1387246f2834b
    domain: switch
condition:
  - condition: template
    value_template: "{{ now().timestamp() - states('sensor.motion_last_detected')|float < 1 }}"
action:
  - type: turn_on
    device_id: 34bf888dce5a361ce0edf058532b59ba
    entity_id: switch.0xa4c1387246f2834b
    domain: switch
mode: single

and here is the auxiliary sensor that records when motion was last detected

  - trigger:
      - platform: state
        entity_id: binary_sensor.0x00124b0025306c64_occupancy
        to: 'on'
    sensor:
      - name: motion_last_detected
        state: "{{ now().timestamp() }}"

While this does work, I’m wondering if there is a better solution? I’d like to prevent turning the light off and immediately turning it back on.

In essence: is there a way to disable switching the light switch off for 1 second after being switched on by a motion sensor?

Hey, I came across this post because I’m looking to implement the same thing.

I believe your approach is the best way. I agree it would be ideal to prevent turning the light off and back on again… but I’m not sure that’s possible. Like you said, you’d essentially have to disable the switch at the wall for 1 second, and I can’t think of a way to do that with HA.

I’ve implemented your solution in a single automation without the auxiliary helper sensor. Instead of the helper, I used the last_triggered attribute of the “turn on light on motion” automation in the template condition.

alias: "HELPER - Bathroom light keep on 1 sec after motion"
trigger:
  - platform: device
    type: turned_off
    device_id: afb34edf99e9eb3d1fd805a1b741cd1a
    entity_id: switch.bathroom_light
    domain: switch
condition:
  - condition: template
    value_template: >-
      {{ now().timestamp() -
      as_timestamp(state_attr("automation.bathroom_motion", "last_triggered")) |
      float < 1 }}
action:
  - type: turn_on
    device_id: afb34edf99e9eb3d1fd805a1b741cd1a
    entity_id: switch.bathroom_light
    domain: switch
mode: single
1 Like