I'm not understanding what you are trying to achieve with the counters. How do they help you at all? You asked to capture the time that power is lost but none of your rules address that.
This type of thing is best done server side. Since tasmota primarily relies on MQTT comms (the clue is in the name ...) the monitoring can be done via an mqtt listener.
On your remote side a simple script should do the trick. Something like this:
#!/usr/bin/php
<?php
$host = "my.mqtt.host";
$port = 1883;
$user = "my user name";
$password = "my mqtt password";
$topic = "tele/my_full_topic/#";
$logFile = "myLogFile.txt";
$logMessage = {};
date_default_timezone_set("UTC");
$cmd = "mosquitto_sub -u \"%s\" -P \"%s\" -p %d -h %s -t %s";
$command = sprintf($cmd,$user,$password,$port,$host,$topic);
$a = popen($command, "r");
file_put_contents(
$logFile,
date(DATE_ATOM) . "\t started listening\n",
FILE_APPEND | LOCK_EX
);
while($line = fgets($a, 2048)){
if(strtolower($line) == "offline"){
file_put_contents(
$logFile,
date(DATE_ATOM) . "\t device offline\n",
FILE_APPEND | LOCK_EX
);
} else {
file_put_contents(
$logFile,
date(DATE_ATOM) . "\t device online\n",
FILE_APPEND | LOCK_EX
);
}
}
file_put_contents(
$logFile,
date(DATE_ATOM) . "\t stopped listening\n",
FILE_APPEND | LOCK_EX
);
?>
Set the script to executable and run it. Or load it as a service.
There is no cogent method of doing much with a tasmotised device when it has no power. To reset a counter on boot, use a rule that triggers at system boot
>>> on system#boot do mem1 0 endon
There are things you can do if there are hangs every now and then on the tasmota. The most reliable is to use a second device to switch off the power to the first device for a short period of time. For these type of scenarios I use a tasmotised plug. Using rules like your ping would work as a monitor. You would need to use static IP addresses or hostnames.
On your rule specifically, I am not convinced that trying to do maths on mem1 will work unless you have USE_EXPRESSIONS set at compile time. Do you?
The point of using memX is so that the variable persists through power loss. Is this something that you want? If not then use var. with varX the value is reset on reboot so you do not need to be concerned about resetting the value.
To increment a variable use addX. Eg.
On system#boot do var1 0 endon on time#minute|5 do add1 endon