Window opener opens window again and again due to Void loop

Update:
I "solved" the Problem that I thaugt I had, only to see that it was not the problem.
I used "&&" instead of two if- statements:

// These constants won't change:
const int analogPin = A0;    // pin that the sensor is attached to
const int analogPin1 = A1;    // pin that the sensor is attached to
const int ledPin = 9;       // pin that the LED is attached to
const int ledPin1 = 8;       // pin that the LED is attached 
const int threshold = 20;   // Temperatur threshold
const int threshold1 = 145;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
   pinMode(ledPin1, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(analogPin);
  int analogValue1 = analogRead(analogPin1);
  int lastAnalogValue = 0;     // previous state of the button
  //Temperatur in Spannung umrechnen
  float voltage = (analogValue1/1024.0) * 5.0;
  //Temperatur in °C ausrechnen
  float temperature = (voltage - .5) * 100;

  // if the analog value is high enough, turn on the LED:
  if (temperature > threshold && lastAnalogValue<threshold){ 
    digitalWrite(ledPin, HIGH);
       Serial.println("Fenster öffnet");
    delay(4000);
    digitalWrite(ledPin, LOW);
   }else {
    digitalWrite(ledPin, LOW);
     Serial.println("Fenster öffnet nicht");

  }
 if (temperature < threshold && lastAnalogValue>threshold) {
   digitalWrite(ledPin1, HIGH);
       Serial.println("Fenster schließt");
    delay(4000);
    digitalWrite(ledPin1, LOW);
  
     } else {
       digitalWrite(ledPin1, LOW);
        Serial.println("Fenster schließt nicht");
        }
  // print the analog value:
 Serial.println("Dunkelheit ADC");
  Serial.println(analogValue);
  Serial.println("Temperatur ADC");
    Serial.println(analogValue1);
   Serial.println("Temperatur in °C");
    Serial.println(temperature);
      Serial.println("--------------------------------------------------------------  10s Delay");
  delay(10000);        // delay in between reads for stability
  lastAnalogValue=temperature;
 }
 
 // Je heller, desto niedriger ist der Dunkelheit- Wert
 // Je wärmer,desto höher temperature

So, at that point, I not only dont know how to solve the problem, I dont even know what is the problem.
Because, at least to me, the sketch seems to be logically correct.