Need help making a automation

Hi everyone, I’m new to Home Assistant and I need help to make an automation for a floodlight and a Reolink Camera. What I am trying to do is make a person-activated Floodlight that turns on the Floodlight when the person entity changes to “detected” and I would like for the lights to stay on as long as the person entity is in the “detected” state and only turn off the when the person entity is “clear” for 5 seconds. I have made an automation that works for the most part, but sometimes it doesn’t work and the floodlight stays on after the person entity is clear and only turn offs when the “wait for trigger” timeouts. I know the automation I have made is bad, and I ask for someone’s help to make it better. Thank you HA community.

alias: Motion Floodlight
description: “”
triggers:

  • trigger: state
    entity_id:
    • binary_sensor.camera_person
      from: “off”
      to: “on”
      conditions:
  • condition: sun
    after: sunset
    before: sunrise
    actions:
  • action: light.turn_on
    metadata: {}
    data: {}
    target:
    entity_id: light.camera_floodlight
  • wait_for_trigger:
    • trigger: state
      entity_id:
      • binary_sensor.camera_person
        from: “on”
        to: “off”
        for:
        hours: 0
        minutes: 0
        seconds: 5
        timeout:
        hours: 0
        minutes: 1
        seconds: 0
        continue_on_timeout: true
  • action: light.turn_off
    metadata: {}
    data: {}
    target:
    entity_id: light.camera_floodlight
    mode: single

Please format your code properly. Use the </> button in the editor menu.

Don’t use wait_for_trigger here. In fact don’t use it except in very rare circumstances, like trying to distinguish a double-click from a single click on a button entity.

Instead, add a second trigger which would be the same state trigger with the same entity but from on to off and add for 5 seconds. You can then add a trigger ID to each trigger (in the UI click the three vertical dots to the right of each trigger). You can call one “motion” and the other “clear”

Keep your condition.

In the actions section, remove everything. Add a choose block. For the choose option #1, the condition will be “triggered by” and select the “motion” trigger. The action is the light.turn_on action. For the choose option #2, the condition will be “triggered by” and select the “clear” trigger. The action is the light.turn_off action.

Thank you for taking the time to respond to my post, I will apply your recommendations and come back with the results later today.