Battery saving help

Actually I would not check for a specific hour alone.
Imagine what is if your device wakes up between 3am and 9am for some reason? Then the device would not go back to sleep or if this happens at 2:59am, the device would wake again 59 minutes too late.

Hence I’d do things slighty different

Knowing that Time.local() returns the local date/time in seconds allows for easy range checks by use of the fact, that a day has 86400 seconds

const int secGo2Sleep = 02*3600 + 00*60 + 00;  // 00 minutes and 00 seconds for illustration purpose only 
const int secWakeUp   = 09*3600 + 00*60 + 00;

void loop() {
  int secNow = Time.local() % 86400;  // whats the current second of the day

  if (secGo2Sleep <= secNow && secNow < secWakeUp) {
    System.sleep(SLEEP_MODE_DEEP, secWakeUp - secNow);
  }
}

If your secGo2Sleep would happen to be set before midnight, you may need to adapt this slightly.

1 Like