Automation of lights by motion detection

I have a camera integration to Home Assistant with Frigate.
I want to turn on the light when motion is detected after sunset and turn off the light after no motion for 1 minute.
The automation itself works well. The problem is that when the light is turned off, the camera detects motion. So the light is turned on again. And the infinite loop begins.

I have already tried delays and durations but without any luck. The light is turn on again however I configure the automation.

Can anybody suggest me solution for this problem? I couldn’t find any option to set dependency between automation. And the other question, is it normal that an easy task becomes so complicated? :slight_smile:

The automation yaml:

alias: Motion on
description: ""
trigger:
  - type: motion
    platform: device
    device_id: XXXX
    entity_id: binary_sensor.xxxx_motion
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition:
  - condition: sun
    after: sunset
action:
  - type: turn_on
    device_id: yyyy
    entity_id: switch.yyyy
    domain: switch
mode: single

and

alias: Motion off
description: ""
trigger:
  - type: no_motion
    platform: device
    device_id: xxxx
    entity_id: binary_sensor.xxxx_motion
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 30
condition:
  - condition: and
    conditions:
      - condition: device
        type: is_on
        device_id: yyyy
        entity_id: switch.yyyy
        domain: switch
action:
  - type: turn_off
    device_id: yyyy
    entity_id: switch.sonoff_yyyy
    domain: switch
mode: single

If the camera detects motion than probably this is your I assume frigate setup. You should configure your camera to be less sensitive on motion because it will detect everything. I mean bug is flying near camera or wind is blowing, or shadow is moving and camera will detect motion. You will have to experiment a bit before you find settings that suit your needs.
The setting you are looking for is threshold. This is camera sensitivity. I use in frigate

    motion:
      threshold: 80

Thank you! I will try to adjust. But based on the frigate debug view, the motion box is huge when the light is turned off. Lot of pixels are turned from white to black. My fear is, if I would set the sensitivity to low enough it wouldn’t also recognize a person moving there.

See the image.

Well you will have to play with it. I found out that this threshold works best for me.

I think I would go for the other way. I would block the light from being turned on again for say 5 seconds after the light has turned off. The CCTV will trigger but the automation will not trigger. I am no automation expert but perhaps a delay helper as a condition. I realise this is a problem I need to resolve! My next job.

I had a similar situation with my garage lights (which are pretty much the only lights that I automated with a camera motion for a long time) and the only way I could solve it was to put as the first action of the turn off automation to turn off the on automation then turn it on after the lights turned off.

so the action becomes:

action:
  - service: automation.turn_off
    entity_id: automation.motion_on
  - delay: 
      seconds: 2 # just to make sure the automation gets disabled in time
  - type: turn_off
    device_id: yyyy
    entity_id: switch.sonoff_yyyy
    domain: switch
  - delay:
      seconds: 5 # you need to set this long enough so that the camera motion trigger has already happened before you re-enable
  - service: automation.turn_on
    entity_id: automation.motion_on

But I don’t need it anymore since I transitioned to using a real PIR motion sensor instead of a camera based motion sensor.

2 Likes

That did the trick, thank you!

1 Like