Automations clash between turning off and on

I have two automations.
One turns on lights, the other turns them off. I notice that sometimes, while entering the room, the lights turn off and then do not come on again until re-entering the room. How do i solve this? I feel i have this situation a fair amount of times.

This is automation 1:

alias: Hallway Lights Off
description: ""
trigger:
  - type: no_motion
    platform: device
    device_id: 90d912b546798d6e61bb0eb6b1907315
    entity_id: 153b7c355742636da5b8e8121cb590bc
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
condition:
  - type: is_no_motion
    condition: device
    device_id: 90d912b546798d6e61bb0eb6b1907315
    entity_id: 153b7c355742636da5b8e8121cb590bc
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
    enabled: true
action:
  - service: light.turn_on
    data:
      brightness_pct: 2
      transition: 2
    target:
      entity_id:
        - light.hallway_light_above_door
        - light.hallway_table_light
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 150
  - type: turn_off
    device_id: ee1865d3b3b90ec793750991a65c6828
    entity_id: adda73192e5a986d047d7e4331de282c
    domain: switch
  - type: turn_off
    device_id: 28976cae92bd78295c910be6b98cfe10
    entity_id: 4cd6f2fe66b259586988854890ea70a4
    domain: light
  - type: turn_off
    device_id: 6c7869f50b1be703df28f3a976b9f68d
    entity_id: 19a3eea845f9624570a14aaf30386f88
    domain: light
mode: single

And this is the second:

alias: Hallway Lights On
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 90d912b546798d6e61bb0eb6b1907315
    entity_id: 153b7c355742636da5b8e8121cb590bc
    domain: binary_sensor
condition:
  - type: is_illuminance
    condition: device
    device_id: 90d912b546798d6e61bb0eb6b1907315
    entity_id: 1d0a5a42531bf0fd5f500cf854076ff2
    domain: sensor
    below: 80
  - condition: time
    after: "06:30:00"
    before: "23:00:00"
action:
  - service: light.turn_on
    data:
      transition: 1
      brightness_pct: 100
    target:
      entity_id:
        - light.hallway_table_light
        - light.hallway_light_above_door
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 900
  - type: turn_on
    device_id: ee1865d3b3b90ec793750991a65c6828
    entity_id: adda73192e5a986d047d7e4331de282c
    domain: switch
mode: single

Appriciate the help and thanks for reading :slight_smile:

I would check the trace log inside the automation when it’s not working, it will tell you if a condition wasn’t met or was outside the threshold to trigger. Looking at those traces will help you debug your automation.

I would try to convert the automation to use state triggers rather than device and use the to: and from: to decide which state is going trigger the automation.

Also since you have a 900ms delay consider what happens if the automation triggers again before that delay is complete