PIR sensor debounce from strong LED light being turned on/off

Hi, I have hacked a cheap LED panel ceiling light from China, added Wemos D1 Mini, relay to turn it on and replaced the stock 230V PIR sensor with HC-SR501.

Problem is that the LEDs are shining from the sides, through a transparent acrylic glass to produce the light and the PIR sensor is in the middle of the glass, so the PIR sensor is hit by the strong light from all sides, which causes the PIR to trigger when there is a change off->on, on->off.

I tried shielding the PIR sensor with black electrical tape from all sides, but it is still not perfect, so I want to add SW “debounce”

I managed to get it somewhat working. But I am using the PIR as a part of home security and also Home Assistent sometimes turns the light on even without PIR (to make it look like somebody is home when we are actually not). This is a problem because as soon as HA turns the light back off, it triggers the PIR sensor, and triggers home security.

This is what I have so far:

globals:
  - id: no_detect
    type: bool
    restore_value: no
    initial_value: 'false'
	
binary_sensor:

  - platform: gpio
    pin: D1
    name: "F Hall Ceiling Light Motion"
    id: motion
    device_class: motion
    on_press:
      then:
        - if:
            condition:
              lambda: 'return !id(no_detect);'
            then:
              - if:
                  condition:
                    or:
                      - binary_sensor.is_off: brightness
                      - switch.is_on: ignore_brightness
                  then:
                    - script.stop: turn_off_lights
                    - if:
                        condition:
                          switch.is_off: night_mode
                        then:
                          - light.turn_on: main_light
                          - light.turn_off: rgb_light
                        else:
                          - light.turn_on: rgb_light
                          - light.turn_off: main_light
                          
        
    on_release:
      then:
        - script.execute: turn_off_lights
              
script:
  - id: turn_off_lights
    then:
      - if:
          condition:
            switch.is_off: always_on
          then:
            - delay: 10s
			- script.execute: motion_debounce
            - light.turn_off: main_light
            - light.turn_off: rgb_light
            
  - id: motion_debounce
    then:
      - globals.set:
          id: no_detect
          value: 'true'
          
      - delay: 5s
      - globals.set:
          id: no_detect
          value: 'false'
          

I was thinking about actually making a template binary_sensor as a pseudo PIR sensor for HA, which would get triggered only after the debounce checks, but this solution seems rather clunky.

I would just somehow want to effectivelly disable the PIR sensor events within 5s of the light being turned on or off, no matter if triggered by HA or PIR sensor.
Can anybody think of a better solution please?
Thanks

I think any solution to this would be “clunky” and I like your idea of conditioning the turn on via a binary sensor.
My idea would be when turning on/off the light via lighting schedule manual switch or pir : -

  1. To turn on an input boolean (that has an automation to turn itself off after 2 secs)
  2. Have a condition that requires the input boolean be ‘off’
  3. THEN turn on/off the light

Haven’t considered full implications of this so it may need some tweaking.

Thank you @Mutt for the response.
Problem with this is that from HA I am turning on/off directly the entity for light, so this wouldn’t work this way. I guess I would have to create a template switch as you said, and hookup the light on/off actions to switch’es on_turn_on/off events, to make these checks/delays in the process, then the PIR and HA would flip this switch insted of the light directly. In this scenario it is ok, but I have also added few of WS2812B LEDs for night mode lighting and for these I need direct access from HA to control color, etc… but thankfully these don’t produce this problem.
Yea I think I will go for this solution.

Is there by any chance a turn_on/turn_off event on Light entity, which gets triggered when the state changes? I couldn’t find it in the docs, if it would be possible I could add the delay there.

I only first started using ESPHome yesterday so I don’t know all it’s features.

Eh ?
But that is exactly what I’m saying …
Post an example of code where : -

  1. When you turn it on and it causes an issue
  2. Where you turn it off and it causes an issue
  3. When you have the pir operate the light (ie other than the 2 above)

What I mean is this:

  1. Using the code I already posted, the light works fine, however HA still detects movement because the PIR gets triggered, it just doesn’t do anything in ESP.

  2. When motion_debounce script is removed, it causes issue both for HA and light turning right back on after turning off because PIR gets false positive.

  3. I need to add binary input (switch) to tap into on_turn_on/off events, which will be ‘connected’ directly to the light, but it will contain the debounce boolean for the PIR for 3-5sec after switching both ways, within this time the PIR won’t trigger the binary switch again. But I also need to add another switch as a pseudo PIR for the HA, which will be triggered the same as the first binary switch for light. This way because I can turn on ‘always_on’ switch which will keep the light on (as well as binary switch for light), but I still want the HA to have movement detection present, but debounced (turned off for 3-5sec after switching light). If there was a way to disable the PIR binary_sensor both from firing an event inside ESP and also triggering in HA, I wouldn’t need the pseudo PIR binary.

I don’t know if it makes sense the way I write it, a lot of times it gets a bit complicated in my head and I cannot clearly write it down lol.

However as I said in the previous post I agree with you, and I said that’s a good idea, I just need to extended on it a bit. Probably it will be best if I implement it in the evening and I will post the resulting code to make it clearer what I mean.

So, this is the code I have come up with to properly use the debounce to prevent false positives from the PIR sensor when the light is turn on or off:

globals:
  - id: no_detect  # Stores the motion debounce value which is checked after motion sensor has been triggered.
    type: bool
    restore_value: no
    initial_value: 'false'
    
output:
  - platform: gpio
    pin:
      number: D2
      inverted: True
    id: relay
    
binary_sensor:
  - platform: gpio
    pin: 
      number: D6
      inverted: True
    name: "F Hall Ceiling Light - Ambient Brightness"
    id: brightness
    device_class: light
    filters:
      - delayed_off: 500ms
      - delayed_on: 500ms
      
  - platform: template
    name: "F Hall Ceiling Light - Motion"
    device_class: motion
    id: motion
    
  - platform: gpio
    pin: D1
    id: motion_int
    internal: True
    on_press:
      then:
        - if:
            condition:
              lambda: 'return !id(no_detect);' # Check if motion_debounce is not active to prevent false positive after turning on/off the main light
            then:
              - script.stop: turn_off_lights
              - binary_sensor.template.publish: # Turn on pseudo Motion detect sensor to be used in HA / home security 
                  id: motion                    # (already debounced - to ignore false positives caused by switching the light manually)
                  state: ON
                  
              - if:
                  condition:
                    or:
                      - binary_sensor.is_off: brightness # If ambient light is not enough, we will turn on the lights
                      - switch.is_on: ignore_brightness # Or if we choose to ignore the light sensor
                  then:
                    - if:
                        condition:
                          switch.is_off: night_mode # Determine if we want to turn on the main light or just night mode RGB light
                        then:
                          - switch.turn_on: light_switch  # Turn on the main light switch (not the light entity directly, because we need to listen to turn on/off events to start the debounce timeout)
                                                          # This is to allow the motion debounce to start when turned on manually from HA GUI aswell
                          - light.turn_off: rgb_light
                          
                        else:
                          - light.turn_on: rgb_light
                          - switch.turn_off: light_switch
                          
        
    on_release:
      then:
        - script.execute: turn_off_lights # After the motion has stopped, we will start a timeout script after which the light is turned off
        
        - delay: 5s   
        - binary_sensor.template.publish: # We turn the pseudo motion sensor back off
            id: motion
            state: OFF
              
script:
  - id: turn_off_lights
    then:
      - if:
          condition:
            switch.is_off: always_on
          then:
            - delay: 10s
            - switch.turn_off: light_switch
                              
            - light.turn_off: rgb_light
            
  - id: motion_debounce
    then:
      - globals.set:
          id: no_detect
          value: 'true'
          
      - delay: 2s
      - globals.set:
          id: no_detect
          value: 'false'
          
    
light:
  - platform: fastled_clockless
    chipset: WS2812B
    pin: D7
    num_leds: 4
    rgb_order: GRB
    name: "F Hall Ceiling Light - RGB"
    id: rgb_light
    effects:
      - flicker:
      - addressable_rainbow:
      - addressable_color_wipe:
      - addressable_scan:
      - addressable_twinkle:
      - addressable_flicker:
    
  - platform: binary
    output: relay
    id: main_light
    internal: True
    

switch:
  - platform: template
    name: "F Hall Ceiling Light"
    id: light_switch
    icon: "mdi:lightbulb"
    optimistic: True
    turn_on_action:
      - script.execute: motion_debounce
      - light.turn_on: main_light
      
    turn_off_action:
      - script.execute: motion_debounce
      - light.turn_off: main_light
    
  - platform: template
    name: "F Hall Ceiling Light - Night Mode"
    id: night_mode
    optimistic: True
  
  - platform: template
    name: "F Hall Ceiling Light - Always On"
    id: always_on
    optimistic: True
  
  - platform: template
    name: "F Hall Ceiling Light - Ignore Brightness"
    id: ignore_brightness
    optimistic: True