Hoping to Get an Assist w/My Current Timed Coach Lights w/Motion Adjust Automations

I built out two (it was three or four) automations for my desired light behavior that seemed to be working properly but today there were on mid-day indicating they’re not working as I’d hoped.

Goal: I’d like my exterior front lights to come on 25% at sunset and then off at 2:00am. I would also like them to increase brightness to 100% when motion is detected for 3min and then go back to the 25% if no other motion is detected.

My first attempt was an automation to have them turn off and then another one to have them turn off. I then combined them into one single automation that worked. All positive steps in creating my first automations w/HAOS. Here’s the code for the automation I created to turn lights on/off:

alias: Front Lights On @ Sunset & Off @ 2:00am
description: ""
triggers:
  - trigger: sun
    event: sunset
    offset: 0
    id: When the sun sets
  - trigger: time
    at: "02:00:00"
    id: When the time is equal to 2:00am
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - When the sun sets
        sequence:
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 25
            target:
              device_id: 1b497669b9bcf217c8292530c38c558f
      - conditions:
          - condition: trigger
            id:
              - When the time is equal to 2:00am
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              device_id: 1b497669b9bcf217c8292530c38c558f
mode: single

Note: the above was taken from the yaml file but I used only the UI to create the automation. I’m only just now getting into yaml editing in my dashboard edits but figured this was the best way to share it with others.

I then created a 2nd automation for the motion part I described above. Here’s the code from that:

alias: Front Lights 100% for 3min on Motion
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.porch_g4_bullet_person_detected
    to: "on"
    id: motion_on
  - trigger: state
    entity_id:
      - binary_sensor.porch_g4_bullet_person_detected
    to: "off"
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: motion_off
conditions:
  - condition: device
    type: is_on
    device_id: 1b497669b9bcf217c8292530c38c558f
    entity_id: 20ea4b702db0471a4b5a0d0379b08fc4
    domain: light
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - motion_on
        sequence:
          - type: turn_on
            device_id: 1b497669b9bcf217c8292530c38c558f
            entity_id: 20ea4b702db0471a4b5a0d0379b08fc4
            domain: light
            brightness_pct: 100
      - conditions:
          - condition: trigger
            id:
              - motion_off
        sequence:
          - type: turn_on
            device_id: 1b497669b9bcf217c8292530c38c558f
            entity_id: 20ea4b702db0471a4b5a0d0379b08fc4
            domain: light
            brightness_pct: 25
mode: single

I guess I’m looking for either 1) some assistance to make a small tweak to my code to correct whatever was causing it to come on outside of the designated time window or not allowing it to turn off at the end of said window or 2) a cleaner set of code that would all of this more efficiently in a single automation that I could replace both of these with.

Thanks!

add two conditions - one that it has to be after sunset and one that it has to be before 2 am:

conditions:
  - condition: device
    type: is_on
    device_id: 1b497669b9bcf217c8292530c38c558f
    entity_id: 20ea4b702db0471a4b5a0d0379b08fc4
    domain: light
  - condition: state
    entity_id: sun.sun
    state: "below_horizon"
  - condition: time
    before: '02:00:00'

also as has been recommended in many places it’s best to not use devices in automations/scripts. try to use triggers/actions/conditions with entities.

To the 2nd one, right? Like this:

alias: Front Lights 100% for 3min on Motion
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.porch_g4_bullet_person_detected
    to: "on"
    id: motion_on
  - trigger: state
    entity_id:
      - binary_sensor.porch_g4_bullet_person_detected
    to: "off"
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: motion_off
conditions:
  - condition: device
    type: is_on
    device_id: 1b497669b9bcf217c8292530c38c558f
    entity_id: 20ea4b702db0471a4b5a0d0379b08fc4
    domain: light
  - condition: state
    entity_id: sun.sun
    state: "below_horizon"
  - condition: time
    before: '02:00:00'
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - motion_on
        sequence:
          - type: turn_on
            device_id: 1b497669b9bcf217c8292530c38c558f
            entity_id: 20ea4b702db0471a4b5a0d0379b08fc4
            domain: light
            brightness_pct: 100
      - conditions:
          - condition: trigger
            id:
              - motion_off
        sequence:
          - type: turn_on
            device_id: 1b497669b9bcf217c8292530c38c558f
            entity_id: 20ea4b702db0471a4b5a0d0379b08fc4
            domain: light
            brightness_pct: 25
mode: single

I added it in the middle where it looked like it might go or did you mean it add it to the bottom of this or the other one?

Is there a better way to combine this or to start fresh to do what I’m trying to do in a single automation rather than two?

Also, not sure what you mean about not using a device for an automation. I’m trying to trigger the motion using a Unifi Protect camera. Is there a step I’m leaving out to create something using the same device trigger before putting it into an automation? Seems like an extra strep that’s not needed if the device sends a trigger automatically but maybe I just don’t understand what you mean.

yes

I try to not cram too much stuff into one automation. It makes maintenance of the automation harder unless it’s well documented. That said there are times when it makes sense to combine things into one automation like when you have several different triggers doing essentially the same thing except for a few specific conditions.

in your case I don’t think that’s the case since in one automation you are turning the lights on/off and in the other you are just changing the brightness.

but in the end it’s up to you to decide which way makes sense to you keeping maintenance in mind.