timing question - turn off relay

I have a simple alarm.
uses motion sensors. the output 12VDC when in normal, and then open the circuit (0VDC) when in alarm.

I have a latching relay. one pulse to set the relay into alarm.
a second pulse to take it out of alarm

for testing, I was looking to just have the second pulse wait about 5 seconds after the sensors go high.

it seems I am continuously pulsing the relay

int led = 13;
const int buttonPin = 2; 
int buttonState = 0; 
int lastButtonState = LOW; 
int ledState = HIGH;
int interval = 5000;
unsigned long then = 0;
int lastState = HIGH;
int relay1 = 8;
int relay2 = 9;

void setup() {                

  pinMode(led, OUTPUT);   
  pinMode(buttonPin, INPUT);   
  pinMode(relay1, OUTPUT);  
  pinMode(relay2, OUTPUT);  
  Serial.begin(9600);

}


void loop() {

  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {     
    digitalWrite(led, HIGH);  
  } 
  else {
    digitalWrite(led, LOW); 
  }

  if (buttonState == LOW){ // natural state is high for normal - low is alarm
    if (lastState == HIGH){
      digitalWrite (relay1,HIGH);
      delay (200);        // this is to pulse the relay coil one half second.
      digitalWrite (relay1,  LOW);
    }
  }  


  if (buttonState == HIGH){ 
    if (lastState == LOW){
      then = millis();   //stores the time for the interval
    }
  }

  if (millis()-then > interval ){  // I think this is where I am going wrong
    digitalWrite (relay2, HIGH);
    delay(200);  // this is to pulse the relay coil one half second.
    digitalWrite (relay2,LOW);
  }
  lastState=buttonState;


} // end of loop

I thought that once the sensor value goes high, and it was lastly LOW
that I would set 'then'

so, the timer would start at the time the sensor changed state from low to high.
then the timer would count for 5 seconds and then execute the pulse.

I need to put the
' if millis()-then....'
inside of
if (buttonState == HIGH);

part of my problem, exactly as you found, was that even on the first section, 'then' never re-set to 0 and was always high.,

your idea of adding
'activeAlarm = false;'
will allow me to pulse it one time and then not pulse it again until after it goes changes.

thanks.

still a glitch

I added

if (buttonState == LOW){
 then = millis();
}

to keep 'then' = mills() unless it was out of alarm