Used Light sensor and motion to turn on light. Does not work if continues to detect motion when it gets dark!

I have been using home assistant for a few months and slowly learning and bringing more of my home devices onboard.
I worked out motion sensors, groups, light illuminance levels to trigger for lights to only turn on if motion detected and lilluminance below x. Also solved the TV viewing and keeping lights on by use of my Harmony Remote and activity use.
However, specifically in my Study, I can be sat and working as we move from daylight into darkness, but as my sensors in the Study Sensor group keep detecting motion, it never runs unless i move out of the room for a few minutes to allow lights off to run and stop detecting motion. Now it runs again upon motion checks the condition that the light level is below and turns on the light.
Is there any way to get the light to turn on when light level drops even if motion is detected?

I have found creating a second automation to check if light illuminance drops below 60 and condition that study group detects motion, then turn on study light.

This has made me realise some of my zones may be in use when light changes to require lights and due to lack of change in sensor to trigger, the light would stay off.
So, do I need to make two variants of the turn on light scripts, or Is there a neater way to do this?

I use the script below for one of my rooms, which seems to work well. Its triggered by EITHER the occupancy changing, or the illuminance level changing.

I then use an if condition in the actions to determine if the room is actually occupied, AND the room is dark enough, AND the light isnt already on. If so, the light is turned on.

I then wait for 30 seconds of occupancy NOT being detected then turn off the light. I use mode: restart which means if its triggered again, its basically, continually refreshing the 30 second check, so the lights dont go out when I dont want them to.

alias: Lounge - Motion Lights
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.occupancy_lounge
  - platform: state
    entity_id:
      - sensor.illuminance_lounge
condition: []
action:
  - if:
      - condition: state
        entity_id: light.tradfri_bulb_lounge_main_1_light
        state: "off"
      - condition: state
        entity_id: binary_sensor.occupancy_lounge
        state: "on"
      - condition: numeric_state
        entity_id: sensor.illuminance_lounge
        below: input_number.lounge_illuminance_threshold
    then:
      - service: scene.turn_on
        target:
          entity_id: scene.lounge_normal_mode
        metadata: {}
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.occupancy_lounge
        from: "on"
        for:
          hours: 0
          minutes: 0
          seconds: 30
    timeout:
      hours: 0
      minutes: 0
      seconds: 31
      milliseconds: 0
    continue_on_timeout: true
  - if:
      - condition: not
        conditions:
          - condition: state
            entity_id: binary_sensor.occupancy_lounge
            state: "on"
    then:
      - service: scene.turn_on
        target:
          entity_id: scene.lounge_all_off_2
        metadata: {}
mode: restart

1 Like

Thanks for the info, now working as a single script!
Much better all in one than lots of separate scripts.