From d7cc9c453221dc0a7e254e07888c0b6e798a9fc0 Mon Sep 17 00:00:00 2001 From: rudolfkoenig <> Date: Sun, 25 Jan 2015 15:24:49 +0000 Subject: [PATCH] fhem.pl: event-aggregator attribute from Boris (Forum #31856) git-svn-id: https://svn.fhem.de/fhem/trunk@7714 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/fhem.pl | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/fhem/fhem.pl b/fhem/fhem.pl index 241c3569c..50f3e6298 100755 --- a/fhem/fhem.pl +++ b/fhem/fhem.pl @@ -36,7 +36,6 @@ use IO::Socket; use Time::HiRes qw(gettimeofday); use Errno qw(:POSIX); - ################################################## # Forward declarations # @@ -260,7 +259,7 @@ $modules{Global}{AttrFn} = "GlobalAttr"; use vars qw($readingFnAttributes); $readingFnAttributes = "event-on-change-reading event-on-update-reading ". - "event-min-interval stateFormat"; + "event-aggregator event-min-interval stateFormat"; %cmds = ( @@ -3506,6 +3505,12 @@ readingsBeginUpdate($) my @a = split(/,/,$attrminint); $hash->{".attrminint"} = \@a; } + + my $attraggr = AttrVal($name, "event-aggregator", undef); + if($attraggr) { + my @a = split(/,/,$attraggr); + $hash->{".attraggr"} = \@a; + } my $attreocr= AttrVal($name, "event-on-change-reading", undef); if($attreocr) { @@ -3633,6 +3638,7 @@ readingsEndUpdate($$) delete $hash->{".updateTime"}; delete $hash->{".attreour"}; delete $hash->{".attreocr"}; + delete $hash->{".attraggr"}; delete $hash->{".attrminint"}; @@ -3733,8 +3739,39 @@ readingsBulkUpdate($$$@) $changed = 1 if($eocr); } } + } - + + if($changed) { + #Debug "Processing $reading: $value"; + my @v = grep { my $l = $_; + $l =~ s/:.*//; + ($reading=~ m/^$l$/) ? $_ : undef} @{$hash->{".attraggr"}}; + if(@v) { + # e.g. power:20:linear:avg + my (undef, $duration, $method, $function) = split(":", $v[0], 4); + my $ts; + if(defined($readings->{".ts"})) { + $ts= $readings->{".ts"}; + } else { + require "TimeSeries.pm"; + $ts= TimeSeries->new( { method => $method, autoreset => $duration } ); + $readings->{".ts"}= $ts; + # access from command line: + # { $defs{"myClient"}{READINGS}{"myValue"}{".ts"}{max} } + #Debug "TimeSeries created."; + } + my $now = $hash->{".updateTime"}; + $changed= $ts->elapsed($now); + $value= $ts->{$function} if($changed); + $ts->add($now, $value); + } else { + # If no event-aggregator attribute, then remove stale series if any. + delete $readings->{".ts"}; + } + } + + setReadingsVal($hash, $reading, $value, $hash->{".updateTimestamp"}); my $rv = "$reading: $value";