ATtiny85 - issue when power is disconnected during sleep

Hi all,
my ATtiny is used to realise big power save for ESP8266.
the circuit and code worked fine, until the battery was empty and I replaced the battery. As of that moment, the ATtiny was no longer performing the same cycles. After a few times disconnecting and reconnecting the power, it all worked fine.
I started to investigate this issue and I notice:
When I disconnect (and reconnect) the power while the ATtiny is running : No issue: the ATtiny does his job perfectly after the power cycle: software starts up as expected.
When I disconnect (and reconnect) the power while the ATtiny is in sleep mode: the ATtiny does not start the job. After a few (3 to 5) times of powercycling, the ATtiny starts to funciton correctly. Sometimes it also become "normal" working without additional powercycles... If I wait long enough..

#include <avr/sleep.h>
#include <avr/wdt.h>

#define ESP 4
boolean on = true;
boolean off = false;
// int watchdogIterations = 38; // multiply this by 8 seconds, that is the time the sleep will take. 37.5 = 300 sec = 5 min
int watchdogIterations = 1 ; // multiply this by 8 seconds, that is the time the sleep will take.
int currentIteration  = 1; // I want the ESP to be powered when I connect power to the ATtiny.


ISR(WDT_vect) // this part of the code is executed each time the ATtiny wakes up.
  { 
  }

void sleepSetup() {
  byte bb = 9 & 7; 
  bb |= (1<<5); //Set the special 5th bit
  //This order of commands is important and cannot be combined
  MCUSR &= ~(1<<WDRF);            //Clear the watchdog reset
  WDTCR |= (1<<WDCE) | (1<<WDE);  //Set WD_change enable, set WD enable
  WDTCR = bb;                     //Set new watchdog timeout value
  WDTCR |= _BV(WDIE);  //Set the interrupt enable, will keep unit from resetting after each int
  
}

void setup()
  {
  pinMode (ESP, OUTPUT);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Power down everything, wake up from WDT
  sleep_enable();
  }  

void loop(){
  pinMode (ESP, OUTPUT);
  currentIteration++;
  if (currentIteration > watchdogIterations)  
  {
  digitalWrite (ESP, on);
  delay(12000);  // This delay (in millisec.) is the amount of time the ESP gets power.
  digitalWrite (ESP, off);
  currentIteration = 0;
  }
  sleepSetup();
  sleep_mode();

}

What might be the difference if the power interruption occurs during the sleep mode?
How can I correct this? (as the power off cycle should become 10 minutes and the power on cycle is only 12 seconds... I have a high chance that the power is disconnected while the ATtiny is sleeping.)

ATtiny85_ESPswitch_6.ino (1.39 KB)

Not enough information. However, do not expect sleep mode to persist across power cycles. If the processor does not reset and start up correctly, you are violating the power-up requirements stated in the data sheet.

For informed help, please read and follow the instructions in the "How to use this forum" post. Code must be properly posted along with a complete circuit diagram.

Tnx for the info. I added code accordingly + schematic.
I don't think it's power as spec's are indicating Vcc max = 5.5V
Strange thing is that the issue is only there after the power is interrupted while the ATtiny is sleeping.

Where is the complete circuit diagram?

Since you MUST have decoupling capacitors on your board, a likely explanation is that they are not discharging completely during your "power interruption", in which case you violate the reset requirements.

See the ATtiny85 data sheet, "System Control and Reset" section for some of the power on reset requirements.

Yeah, I think the ATtiny is not fully resetting due to residual power in the decoupling caps and/or other caps on the board. If you're willing to sacrifice a little bit of battery life by enabling BOD, you could set that such that the BOD would trigger a reset when the battery went dead.

I'd wager that pulsing reset LOW would also sort it out.

The schematic is only missing the connected Adafruit HUZZAH ESP8266 breakout.
I did not add an additional capacito, I assume some are on the breakout board. But I don't think they keep much power for long time after the power is interrupted.
I'll try some testing this evening: It's not a capacitor issue if I disconnect the power for an hour and I still have the problem. (I'll physically disconnect the USB connector to the USB battery pack.)
What do you mean by pulsing the reset low?