On door open - Logic problem

I've tried to read and amalgamate all of your previous hints at what you would like here. Because the more I pry the more info we're getting.

It seems to me like the setup is like an 'occupied' notifier for something like a meeting room, is that right?

So if the door is closed (meeting in session), you want the red LED to come on, to tell people that the room is busy and not to disturb.

If the door is open, you want the red LED to go off.

If the door has been left open for more than 5 seconds you want the green LED to come on so that people can see that the room is free to be used again?

Like this? sketch.ino - Wokwi Arduino and ESP32 Simulator

#define ECHO_PIN 8
#define TRIG_PIN 7
#define RED_LED_PIN 5
#define GREEN_LED_PIN 6
#define TIMEOUT 5000

float readDistance() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  int duration = pulseIn(ECHO_PIN, HIGH);
  return duration * 0.034 / 2;
}

void setup() {
  Serial.begin(9600);
  pinMode(ECHO_PIN, INPUT);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
}

void loop() {
  static unsigned long timeCapture;

  if (!timeCapture && readDistance() <= 31) {  // Door is Open - Waiting for Timeout...
    timeCapture = millis();
    digitalWrite(RED_LED_PIN, LOW);
  }

   if (readDistance() >= 30) { // Door is Closed - Room Occupied
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(GREEN_LED_PIN, LOW);
    timeCapture = 0;
  }

  
  if(timeCapture && (millis() - timeCapture) >= TIMEOUT) { // Open Timeout expired - Room Available
    digitalWrite(GREEN_LED_PIN, HIGH);
    timeCapture = 1;
  }
}