Lambda Function and Timerdelete

Hi Blynkers,

i`m using a lambda function inside a timer function and having trouble with timer delete and conditions.

The function is to watch a state of an inverter which is controlled by a relais that is connected to pin2 of an esp32.

If the inverter is switched on and the measured current is under a defined value a counter should start. If the current stays below the value and the timer ends, the inverters should switch off. But if the current raises over the value and the timer is running, the timer should stop and restart if the low current conditions match again.

It is running fine until the current raises. Then it seems that all other timers are deleted. What is wrong with that code?

void help_of_Jarvis() {

  status_inverter = digitalRead(2);
  status_pump = digitalRead(15);
  status_boiler = digitalRead(18);

  //Disable Inverter when no Device is in use
  
  if (status_inverter == 0) {
    if (atm_amp_uni150A * -1 <= 1.5) {
      if (!timer.isEnabled(delayedOff)) {
        delayedOff = timer.setTimeout(30000L, []() {  // Run following command(s) after set time.
          digitalWrite(2, HIGH);
          Blynk.virtualWrite(V27, 0);
          terminal.println("ALARM - Inverter deactivated - no device in use");
        });  // END Lambda Function
      }
    } else {
      if (timer.isEnabled(delayedOff)) {
        timer.deleteTimer(delayedOff);
      }
    }
  }
}

Read in here about an issue with deleting/disabling timers…

…and create a simple “sacrificial timer” as your first timer in your code. Test and see if that helps.

  // "Sacrificial" Timer - needed when deleting timers as using that feature will also kill first timer created...
  timer.setTimeout(10L, []() {
    // do nothing here, or do something that is only needed once at the start of code.
  });  // END sacrificial Function

I’ve been having the same problems over here and I’m curious if any of this would be of any value to you? I’d be interested in knowing if this solves any of your problems, or if your problem still persists after trying this approach.