Patch 91_watchdog: Allow readings to emit events

Begonnen von Nestor, 18 Dezember 2020, 19:59:52

Vorheriges Thema - Nächstes Thema

Nestor

I tried setting up userReadings for the watchdog device but noticed that the watchdog built-in readings do not emit events so the userReadings did not update.
Attached patch fixes this (changed setReadingsVal to  readings*Update):

--- - 2020-12-18 19:53:09.000000000 +0100
+++ fhem/FHEM/91_watchdog.pm 2020-12-18 19:33:25.000000000 +0100
@@ -55,8 +55,10 @@
watchdog_reset($)
{
   my ($watchdog) = @_;
-  $watchdog->{STATE} = "defined";
-  setReadingsVal($watchdog, "Reset", "reset", TimeNow());
+  readingsBeginUpdate($watchdog);
+  readingsBulkUpdate($watchdog, "state", "defined");
+  readingsBulkUpdate($watchdog, "Reset", "reset");
+  readingsEndUpdate($watchdog, 1);
}

#####################################
@@ -101,8 +103,7 @@
     watchdog_Activate($watchdog)

   } else {
-    $watchdog->{STATE} = "defined"; # do not set the reading
-
+    readingsSingleUpdate($watchdog, "state", "defined", 1);
   }

   InternalTimer(1, sub($){
@@ -193,9 +194,10 @@

   Log3 $name, 3, "Watchdog $name triggered";
   my $exec = SemicolonEscape($watchdog->{CMD});
-  $watchdog->{STATE} = "triggered";

-  setReadingsVal($watchdog, "Triggered", "triggered", TimeNow());
+  readingsBeginUpdate($watchdog);
+  readingsBulkUpdate($watchdog, "state", "triggered");
+  readingsBulkUpdate($watchdog, "Triggered", "triggered");
+  readingsEndUpdate($watchdog, 1); 
   
   my $ret = AnalyzeCommandChain(undef, $exec);
   Log3 $name, 3, $ret if($ret);
@@ -210,7 +212,7 @@
{
   my ($watchdog, $remaining) = @_;
   my $nt = ($remaining ? $remaining : gettimeofday() + $watchdog->{TO});
-  $watchdog->{STATE} = "Next: " . FmtTime($nt);
+  readingsSingleUpdate($watchdog, "state", "Next: " . FmtTime($nt), 1);
   RemoveInternalTimer($watchdog);
   InternalTimer($nt, "watchdog_Trigger", $watchdog, 0);

@@ -221,7 +223,7 @@
     my $tTime = ReadingsTimestamp($wName, "Triggered", "");
     $eor = undef if(!$aTime || !$tTime || $aTime ge $tTime)
   }
-  setReadingsVal($watchdog, "Activated","activated", TimeNow()) if(!$remaining);
+  readingsSingleUpdate($watchdog, "Activated", "activated", 1) if(!$remaining);

   AnalyzeCommandChain(undef, SemicolonEscape($eor)) if($eor);
}
@@ -247,7 +249,7 @@
   }
   $do = 2 if($cmd eq "del" && (!$attrName || $attrName eq "disable"));
   return if(!$do);
-  $hash->{STATE} = ($do == 1 ?  "disabled" : "defined");
+  readingsSingleUpdate($hash, "state", ($do == 1 ?  "disabled" : "defined"), 1);
   return undef;
}

rudolfkoenig

Can you tell me the usage pattern for this change?
Generating a new event for each state change comes with a performance penalty, and I'd like to know, if its worth.
Is there anybody else, who is missing this feature?

Are you aware of the sequence module? In combination with some of its attributes can be viewed as a generalized version of watchdog, creating only events instead of executing code.

betateilchen

I tested Nestor's patch in my installation and I received a bunch of additional events which I will never need or use for anything making sense.

@Rudi: If you really think about adding the patch, please add the readingFnAttributes, too. This will give the possibility to prevent watchdogs from generating events. Even better: "watchdog devices will not create events" should be the preferred default behavior.

@Nestor: what is the use case for userReadings in a watchdog device?
-----------------------
Formuliere die Aufgabe möglichst einfach und
setze die Lösung richtig um - dann wird es auch funktionieren.
-----------------------
Lesen gefährdet die Unwissenheit!

Nestor

I want to gather some statistics from the Triggered reading. Since no events are emitted statistics module or userreadings do not work. Maybe remove userreadings attribute from the module because if you can't read the code you wouldn't know why its not working.

I wouldn't add $readingfnattributes because setting the stateformat will break the module.

For me the whole philosophy of home automation is reacting on events. All my devices are generating events, not sure why watchdog can not generate events and let the user decide what to do with it.

rudolfkoenig

I suggest to take a look at the sequence module.
It is all about events, and with certain attributes set it emits a lot of them.

Nestor

Nay, I'll keep it in my private branch but maybe update the docs with notice that watchdog devices don't generate events so others don't loose time with all the peculiarities.