Infrared remote to control 433mhz sockets

Hi,
Im controlling 433mhz sockets with an IR remote and 433mhz transmitter connected to arduino. I would like a couple of the sockets to turn off after a set amount of time. So I press the button set amount of time passes and they auto turn off. Im trying to do this at the min with button 5 with is socket number 5. Just trying 5 sec delay so it should turn off after 5 secs but it just doesn't turn off. When I say <= autoOffTime off it turns off straight away if I put >= it wont turn off. Here is the code I have at the min. Some buttons turn off more than one socket but that is working fine.

#include <RCSwitch.h>
#include <IRremote.h>

const int mainLight = 4;
const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

unsigned long timer = 0;

RCSwitch mySwitch = RCSwitch();

struct RCStructure
{
  boolean state;
  unsigned long codeID;
  unsigned long onCode;
  unsigned long offCode;
  unsigned int codeLength;
  char letter;
  boolean autoOff;
  const unsigned long autoOffTime;
  unsigned long onCode2;
  unsigned long offCode2;
  unsigned long onCode3;
  unsigned long offCode3;
};

RCStructure remoteKey[] = {
  {false, 0xC101E57Bul, 1398211ul, 1398220ul, 24, '0', false, 0ul, 1398531ul, 1398540ul}, //when you press the 0 button
  {false, 0x9716BE3Ful, 4543795ul, 4543804ul,  24, '1', false, 0ul}, //when you press the 1 button
  {false, 0x3D9AE3F7ul, 4543939ul, 4543948ul,  24, '2', false, 0ul}, //when you press the 2 button
  {false, 0x6182021Bul, 4544259ul, 4544268ul,  24, '3', false, 0ul}, //when you press the 3 button
  {false, 0x8C22657Bul, 4545795ul, 4545804ul,  24, '4', false, 0ul}, //when you press the 4 button
  {false, 0x488F3CBBul, 4551939ul, 4551948ul,  24, '5', true, 5000ul}, //when you press the 5 button
  {false, 0x449E79Ful, 349491ul, 349500ul, 24, '6', false, 0ul}, //when you press the 6 button
  //{false, 0x32C6FDF7ul, 349635ul, 349644ul, 24, '7', false, 0ul}, //when you press the 7 button
  {false, 0x1BC0157Bul, 349955ul, 349964ul, 24, '8', false, 0ul, 351491ul, 351500ul, 357635ul, 357644ul}, //when you press the 8 button
  {false, 0x3EC3FC1Bul, 1398067ul, 1398076ul, 24, '9', false, 0ul}, //when you press the 9 button
  {false, 0x97483BFBul, 1400067ul, 1400076ul, 24, 'U', false, 0ul}, //When you press the 100+ button
  {false, 0xF0C41643ul, 1406211ul, 1406220ul, 24, 'D', false, 0ul}, //When you push the 200+ button
};

const byte remoteKeySize = sizeof(remoteKey) / sizeof(remoteKey[0]);

// -----------------------------------------------

void setup() {
  
  Serial.begin(9600);  //starts serial communication
  irrecv.enableIRIn(); // Start the receiver
;
  pinMode(mainLight, OUTPUT);
  // Transmitter is connected to Arduino Pin #10
  mySwitch.enableTransmit(10);
  mySwitch.setPulseLength(190);
}

void loop() {

  // -----------------------------
  // check the remote control
  // -----------------------------
  if (irrecv.decode(&results)) {
    for (byte i = 0; i < remoteKeySize; i++) { // check which key is pressed
      if (results.value == remoteKey[i].codeID) {
        // we found the key, do what's needed
        Serial.println(remoteKey[i].letter); // debug only, print the letter on the key
        if (remoteKey[i].state) {
          remoteKey[i].state = false;
          mySwitch.send(remoteKey[i].offCode, remoteKey[i].codeLength);
          mySwitch.send(remoteKey[i].offCode2, remoteKey[i].codeLength);
          mySwitch.send(remoteKey[i].offCode3, remoteKey[i].codeLength);
        }
        else {
          remoteKey[i].state = true;
          mySwitch.send(remoteKey[i].onCode, remoteKey[i].codeLength);
          mySwitch.send(remoteKey[i].onCode2, remoteKey[i].codeLength);
          mySwitch.send(remoteKey[i].onCode3, remoteKey[i].codeLength);
        }
       break;
      }
    } // for each key
    irrecv.resume(); // Receive the next value
  }

  // -----------------------------
  // check auto off features
  // -----------------------------
  for (byte i = 0; i < remoteKeySize; i++) {
    unsigned long AUTO_OFF_DELAY = millis();
    if (remoteKey[i].state && remoteKey[i].autoOff && AUTO_OFF_DELAY - timer >= remoteKey[i].autoOffTime) {
      mySwitch.send(remoteKey[i].offCode, remoteKey[i].codeLength);
      timer = AUTO_OFF_DELAY;
      remoteKey[i].state = false;
    }
  }


  //switch case to use the selected remote control button
    switch (results.value){
     
     case 0xFF02FD: //When you press the OK Button
     {
       digitalWrite(mainLight, HIGH);
       delay(1000);
       digitalWrite(mainLight, LOW);
     }
     break;
    }
}

Thanks

Where do you record when you turn the pin on? How can you turn the pin off 5 seconds after turning it on, when you have no idea when you turned it on?

When the socket turns on the false turns to true at the beginning of this line. I set the other one to true for the sockets I auto want to turn off.

{false, 0x488F3CBBul, 4551939ul, 4551948ul, 24, '5', true, 5000ul}, //when you press the 5 button

Then this line if i`m understanding it correctly is saying if the state and autooff are both true which they would be only if the sockets are on and if auto_off_delay - timer is more than autoofftime which is set to 5000 so 5 secs then it should turn the sockets off.

if (remoteKey.state && remoteKey.autoOff && AUTO_OFF_DELAY - timer >= remoteKey*.autoOffTime) {
_//send socket off code*
mySwitch.send(remoteKey.offCode, remoteKey*.codeLength);_
timer = AUTO_OFF_DELAY;
_//set state back to false*
remoteKey*.state = false;*
Thanks_

When the socket turns on the false turns to true at the beginning of this line. I set the other one to true for the sockets I auto want to turn off.

So? true does not tell you WHEN.

Then this line if i`m understanding it correctly is saying if the state and autooff are both true which they would be only if the sockets are on and if auto_off_delay - timer is more than autoofftime which is set to 5000 so 5 secs then it should turn the sockets off.

That line is checking that a pin is on, that it is intended to be turned off at some interval after it is turned on, AND that the interval has expired.

But, the interval is based on the value in timer which is NOT related to a specific pin, and is only assigned a (useless) non-zero value AFTER it is used in that test.