Help needed with Unifi Protect - Home assistant integration for outdoor lights

Hi all

I am new to this Home Assistant game and I recently was able to get smart lights setup in the garden and integrate my Protect into it for some automations

So far automations work fine but there is some fine tuning required in some of them and I was wondering if I could get some help

Currently I have two automations for the garden lights, one for power on and one for power off. They both use Unifi Protect cameras API, in particular the AI Person/Animal Detection. And I have two cameras tied into this automation, both in the garden at two different points

Power on works great, person/animal walks into the frame and all lights turn on in the garden

Issue seems to be with power off, ill be in the frame of the camera (Lets say I sit down on a chair) the lights stay on for maybe 5 minutes and then turn off.

Is this more of an issue on the Protect side as I reviewed the footage back and I can see one camera sees me and draws a box around me, the other doesnt see me and then cuts off the lights as its a trigger, it sends a response back to HA saying that Person is not detected.

Is there a way maybe I can balance this such as “Only if both cameras say person not detected, the lights will turn off” rather than one?

I have a copy of the code here in YAML

alias: Garden Lights (Power off)
description: ""
triggers:
  - type: turned_off
    device_id: 55383f660fb81204eba3d43bac523f94
    entity_id: 6ebc6e5b36c9f5a78f4bb172fd0868f4
    domain: binary_sensor
    trigger: device
  - type: turned_off
    device_id: 55383f660fb81204eba3d43bac523f94
    entity_id: ff9a9cd9c312e243a3cf62b993b28cdd
    domain: binary_sensor
    trigger: device
  - type: turned_off
    device_id: 425193de48b8170f9d5aa9286d09f844
    entity_id: 94e962bc43eedf9c9edfd332f51722a6
    domain: binary_sensor
    trigger: device
    enabled: true
  - type: turned_off
    device_id: 425193de48b8170f9d5aa9286d09f844
    entity_id: 0f24bd890aeef5dab4612ba16f60d546
    domain: binary_sensor
    trigger: device
    enabled: true
conditions:
  - condition: sun
    before: sunrise
    after: sunset
actions:
  - type: turn_off
    device_id: 808a2e16c0822d249e70594b719fd9ad
    entity_id: 300aecfb2ed930185b920a00f7d1a92e
    domain: switch
  - type: turn_off
    device_id: 8bca12a8f6b5e6b63fa2ecb6bca19de9
    entity_id: 47526887b8bcf25f44708b18dbe97b10
    domain: switch
mode: single

Any help would be great, thanks!

Add ANDed (default) state conditions to your ‘off’ automation so that all sensors must be off to turn the light off.

conditions:
  - condition: state
    entity_id: 6ebc6e5b36c9f5a78f4bb172fd0868f4
    state: 'off'
  - condition: state
    entity_id: ff9a9cd9c312e243a3cf62b993b28cdd
    state: 'off'
  - condition: state
    entity_id: 94e962bc43eedf9c9edfd332f51722a6
    state: 'off'
  - condition: state
    entity_id: 0f24bd890aeef5dab4612ba16f60d546
    state: 'off'

This will allow the actions only when the last remaining ‘on’ sensor turns ‘off’.

Note I have also removed the sun condition in case you are out in the garden early in the morning and all the sensors turn off after sunrise. In that case the light would remain on. Only use that condition in your ‘on’ automation.

Also just FYI: Why and how to avoid device_ids in automations and scripts

Hi, thanks for this

So ive checked and it seems to be an unifi issue actually

For some reason even at a 92% confidence rate, it sees me as a person but then drops off, which then sends a response back to home assist and cuts off the lights even by adding the code you provided