how to send Alert and avoid doing it second time (loop)?

Okay i was unable to understand the above provided code (sorry it was too complex for me)
so i tried search the forum for a little guidance... i found one door open and close alarm so i used it. It is perfect and i tweaked it to work in my condition....
But i want to make it short and good so can anyone help me to optimize it because i have a lot of code
So here it is what i want now..

  1. Optimize and minimize the code...because i have other functions and procedures too this is only one part of the arduino... i am using all the digital ports and analogue ports of arduino currently the code is (without this function) -
    program space - 72% (without adding the electricity monitor code)
    Sram space - 62% (without adding the electricity monitor code)
    still there is more code to add like function to match date/time and execute according to it... if any direction for that it would save my time thank you...
  2. Add more devices to check same things as electricity i mean like door,window open/close on/off state with minimizing the code...same alert as electricity is doing SMS once on ONLY state change.
  3. This code might help others....
int electricityContactPin = 5; // pin electricity contact is attached to
int ledPin = 6; // led connected to pin 6 thru resistor to gnd
unsigned long electricityOpenTime = 0;
unsigned long electricityClosTime = 0;
unsigned long time_threshold = 5000; //time in ms => 5 sec.
int currelectricityState = digitalRead(electricityContactPin);
int prevelectricityState = digitalRead(electricityContactPin); // Assumes that LOW means closed and electricity is gone when the Arduino starts
boolean electricityIsClosed = true; // Let's assume it's gone for starting
boolean SMSSent = true;   // Tells us whether we've sent an SMS for the latest instance of the electricity being present

void setup()
{
  Serial.begin(9600);
  pinMode(electricityContactPin, INPUT);
  pinMode(ledPin, OUTPUT);
}


//Send sms (replaced with led status)
void sendSMS()
{
  digitalWrite(ledPin, HIGH);
  delay(3000);
  digitalWrite(ledPin, LOW);
  Serial.println("SMS SENT");
  }

void loop()
{

 //1
  currelectricityState = digitalRead(electricityContactPin);
  
  
  //2
  if (currelectricityState != prevelectricityState)  // electricity was closed and is now open or electricity was open and is now closed
  {
    Serial.println("!!!DETECTED STATE CHANGE!!!");
    delay(1000);
    if (currelectricityState == LOW)
    {
      // electricity is now gone
      electricityIsClosed = true;
      electricityClosTime = millis();
      delay(2000);
    }
    else
    {
      // electricity is now present
      electricityIsClosed = false;
      electricityOpenTime = millis();
      //       Serial.println("electricity is OPEN");
      delay(2000);
    }
    SMSSent = false; // electricity state changed, we may have a new opportunity to send SMS
  }
  
  //3
  prevelectricityState = currelectricityState;

//4
  // Now see if the electricity is open
  if (!electricityIsClosed && !SMSSent)
  {
    Serial.println("Present");
    if (millis() - electricityOpenTime >= time_threshold)
    {
      Serial.println("Present SMS");
      sendSMS();
      SMSSent = true;
    }
  }

  if (electricityIsClosed && !SMSSent)
  {

    Serial.println("GONE");
    if (millis() - electricityClosTime >= time_threshold)
    {
      Serial.println("GONE SMS");
      sendSMS();
      SMSSent = true;
    }
  }
}