Automation help: How do I make a "only trigger if a condition has been met for X seconds"

Hi everyone.

I have a automation for my kitchen, where different lights turn on/off based on presence + lux values.

But the current headache is when there is cloudy weather, and the sun will be coming and going rather rapidly. Is there a way to modify the automation below, with a “If the lux drops below a value for X seconds, then turn off / on the lights”

Or is it a fools errand to venture into all this?

alias: Køkken Dagligdagsbelysning
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.presence_sensor_kokken_illuminance_lux
    enabled: true
  - platform: state
    entity_id:
      - binary_sensor.presence_sensor_kokken_presence
    to: "on"
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: input_select.hus_tilstand
        state: Hjemme
action:
  - alias: Lux mellem 0 og 20
    if:
      - condition: numeric_state
        entity_id: sensor.presence_sensor_kokken_illuminance_lux
        below: 40
      - condition: state
        entity_id: binary_sensor.presence_sensor_kokken_presence
        state: "on"
    then:
      - data:
          brightness_pct: 100
        action: light.turn_on
        target:
          entity_id: light.spisebordet
      - action: light.turn_on
        metadata: {}
        data:
          brightness_pct: 70
        target:
          entity_id: light.kokkenbordet
  - alias: Lux mellem 30 og 80
    if:
      - condition: numeric_state
        entity_id: sensor.presence_sensor_kokken_illuminance_lux
        above: 70
        below: 150
      - condition: state
        entity_id: binary_sensor.presence_sensor_kokken_presence
        state: "on"
    then:
      - data:
          brightness_pct: 70
        target:
          entity_id:
            - light.spisebordet
        action: light.turn_on
      - target:
          entity_id: light.kokkenbordet
        data: {}
        action: light.turn_off
  - alias: Sluk hvis over 90
    if:
      - condition: numeric_state
        entity_id: sensor.presence_sensor_kokken_illuminance_lux
        above: 190
        below: 3000
    then:
      - data: {}
        target:
          entity_id: light.kokken
        action: light.turn_off
mode: single

You might try using the lux sensor to create a threshold helper, and using that for the trigger.

A threshold sensor allows you to define a hysteresis - a zone on either side of the threshold value where nothing happens. This smooths out “bounce” when the light is changing rapidly.