How to stop a motion feedback loop triggering a light when its turned off

Hello,
I have modded some cheap smart lights that are controlled with an esp8266 to incorporate a PIR motion sensor in the centre of the light, the issue I’m facing when trying to get the PIR sensor to turn on the lights is that when the lights turn off, the PIR gets triggered, turning the lights back on, making a feedback loop. I figured this would happen, so I 3D printed a backout “nipple”
try to stop the light but it didn’t do anything.


I am using a automation in Home Assistant:

alias: Auto Turn On Spare Bedroom Light
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 2475214c1831bc5652fa1581575b2e11
    entity_id: binary_sensor.spare_bedroom_motion
    domain: binary_sensor
condition: []
action:
  - if:
      - condition: time
        after: '22:30:00'
        before: '07:00:00'
    then:
      - service: light.turn_on
        data:
          brightness_pct: 20
        target:
          entity_id: light.spare_bedroom_light
    else:
      - service: light.turn_on
        data:
          brightness_pct: 100
        target:
          entity_id: light.spare_bedroom_light
  - delay:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.spare_bedroom_light
mode: restart

this turns on the lights when motion is detected, and depending on what time it is, modify the brightness

and here is the esphome config

# settings
esphome:
  name: spare-bedroom-light
esp8266:
  board: esp_wroom_02

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  use_address: 192.168.2.223
  domain: .iot

logger:

api:
  encryption:
    key: !secret api_key

ota:
  password: !secret ota_password

# configuration
binary_sensor:
  - platform: gpio
    pin: GPIO4
    name: "Spare Bedroom Motion"
    device_class: motion

output:
  - platform: esp8266_pwm
    id: dimmer
    pin: GPIO5
  - platform: esp8266_pwm
    id: color_temp
    pin: GPIO13
    inverted: True

light:
  - platform: color_temperature
    id: spare_bedroom_light
    name: "Spare Bedroom Light"
    color_temperature: color_temp
    brightness: dimmer
    cold_white_color_temperature: 6500 K
    warm_white_color_temperature: 2700 K

i cant think of any solutions to this, does anyone have any way of getting the automation not to trigger when the lights turned off (and the pir fires again)?

split the automations into two - one for ‘on’ and one for ‘off’. then in the actions right before the light gets turned off put in an action to turn off the ‘on’ automation. then when the lights are off turn the ‘on’ automation back on.

I can’t think of a way to do it with one automation tho.

I do the same for some motion activated lights i have as well.

My OCD won’t allow me to have 2 automations to do 1 task, I was hoping that it was possible to disable the binary_sensor in ESPHome for a few seconds after the light is switched off. this would allow it to fit in 1 automation

IMHO, there is way too much of an emphasis that I see on “making it all in one automation” instead of making it easier, more readable/maintainable and most importantly functional.

there is literally no downside (not withstanding your OCD) to splitting it up into two automations if it helps with the above criteria.

See, but you aren’t doing one task. You are doing two (albeit related) tasks - one to turn on the light and another to turn off the light.

You are trying to force those two tasks into a single automation at the (likely) expense of functionality.

I’m not saying you can’t do that but aren’t you now just moving the “second automation” from HA into ESPHome?

Shouldn’t that also trigger your OCD? :grinning_face_with_smiling_eyes:

Not to mention then it would be way more complicated because you would have way more moving parts since ESPHome would send the motion signal to HA then later HA would need to send a signal to ESPHome that the light needs to be turned off so ESPHome should disable the motion sensor then the light gets turned off by HA then HA needs to send a signal to ESPHome to turn on the motion sensor again. :crazy_face:

If it was me I would just make them into two VERY easy automations and be done with it.

Maybe I’m missing something. But wouldn’t a state condition checking that the light has been off for at least one second be simplest:

alias: Auto Turn On Spare Bedroom Light
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 2475214c1831bc5652fa1581575b2e11
    entity_id: binary_sensor.spare_bedroom_motion
    domain: binary_sensor
condition: []
action:
  - if:
      - condition: state
        entity_id: light.spare_bedroom_light
        state: 'off'
        for:
          hours: 0
          minutes: 0
          seconds: 1
    then:
      - if:
          - condition: time
            before: '07:00:00'
            after: '22:30:00'
        then:
          - service: light.turn_on
            data:
              brightness_pct: 20
            target:
              entity_id: light.spare_bedroom_light
        else:
          - service: light.turn_on
            data:
              brightness_pct: 100
            target:
              entity_id: light.spare_bedroom_light
  - delay:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.spare_bedroom_light
mode: restart