Motion and illumination activated lights

Hi, I am using an occupancy sensor to turn on the lights automatically on my stairs when it’s dark. The device has a built-in illumination sensor and I configured a blueprint to turn on the lights when motion/occupancy is detected, but only when ambient illumination is under a certain threshold.

It works, but I have an issue with frequent up/down sequences. It would work fine if the illumination sensor reported variations quickly, however it seems the illumination is only reported every 90 seconds. What happens is I go up the stairs, the lights come up, motion is cleared after a few seconds, the lights turn off, illumination is still reported high, I go downstairs again and the lights don’t turn on.

I tried using a smoothing filter but it doesn’t solve 100% of the cases. For example, if the lights are on for five full minutes, the smoothing sensor climbs over the threshold and I experience the issue again.
It seems to me that the ideal behavior would be to “hold the last illumination sensor value” when “stairs lights are on”, otherwise “follow the illumination sensor value”.

Is there a sensor template that would be able to achieve this?
Thanks in advance!

I feel so close! This almost works:

- platform: template
  sensors:
    ambient_upstairs_illumination:
        friendly_name: "ambient upstairs illumination"
        unique_id: ambient_upstairs_illumination
        device_class: illuminance
        unit_of_measurement: 'lx'
        value_template: >-
          {% if is_state('light.upstairs_lights', 'on') %}
            {{ states('sensor.ambient_upstairs_illumination') }}
          {% else %}
            {{ states('sensor.presence_sensor_actual_illuminance') }}
          {% endif %}

However when the lights turn off the ambient sensor still has the higher illuminance value (w/ lights on) for the following 90 seconds. Is there a way to “if lights just turned off, hold the previous value for the next 90 seconds”?