From d365ac6a093903f33de38a9c49e9adc81553d0d8 Mon Sep 17 00:00:00 2001 From: tpoitzsch Date: Sun, 19 Oct 2014 16:38:33 +0000 Subject: [PATCH] statistics: decimal places for delta values now equally to source values git-svn-id: https://svn.fhem.de/fhem/trunk@6793 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/98_statistics.pm | 86 ++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 31 deletions(-) diff --git a/fhem/FHEM/98_statistics.pm b/fhem/FHEM/98_statistics.pm index 77efaf4ff..5c669a18e 100644 --- a/fhem/FHEM/98_statistics.pm +++ b/fhem/FHEM/98_statistics.pm @@ -43,12 +43,13 @@ sub statistics_DoStatistics ($$$); sub statistics_doStatisticMinMax ($$$$$$); sub statistics_doStatisticMinMaxSingle ($$$$$$$); sub statistics_doStatisticTendency ($$$$); -sub statistics_doStatisticDelta ($$$$$); +sub statistics_doStatisticDelta ($$$$); sub statistics_doStatisticDuration ($$$$); sub statistics_doStatisticDurationSingle ($$$$$$); sub statistics_storeSingularReadings ($$$$$$$$$$); sub statistics_getStoredDevices($); sub statistics_FormatDuration($); +sub statistics_maxDecPlaces($$); sub statistics_UpdateDevReading($$$$); # Modul Version for remote debugging @@ -59,18 +60,18 @@ sub statistics_UpdateDevReading($$$$); # statisticType: 0=noStatistic | 1=minMaxAvg(daily) | 2=delta | 3=stateDuration | 4=tendency | 5=minMaxAvg(hourly) ############################################################## my @knownReadings = ( ["brightness", 1, 0] - ,["count", 2, 0] + ,["count", 2] ,["current", 1, 3] - ,["energy", 2, 0] + ,["energy", 2] ,["energy_current", 1, 1] - ,["energy_total", 2, 3] + ,["energy_total", 2] ,["humidity", 1, 0] ,["lightsensor", 3] ,["lock", 3] ,["motion", 3] ,["power", 1, 1] ,["pressure", 4, 1] - ,["rain", 2, 1] + ,["rain", 2] ,["rain_rate", 1, 1] ,["rain_total", 2, 1] ,["temperature", 1, 1] @@ -217,7 +218,7 @@ statistics_Notify($$) return; } - # ignore my own notifications + # Ignore my own notifications if($devName eq $name) { Log3 $name,5,"$name: Notifications of myself received."; return "" ; @@ -387,7 +388,7 @@ statistics_DoStatistics($$$) next if not exists ($dev->{READINGS}{$readingName}); $statisticDone = 1; if ($$f[1] == 1) { statistics_doStatisticMinMax ($hash, $dev, $readingName, $$f[2], $periodSwitch, 0);} - if ($$f[1] == 2) { statistics_doStatisticDelta ($hash, $dev, $readingName, $$f[2], $periodSwitch );} + if ($$f[1] == 2) { statistics_doStatisticDelta ($hash, $dev, $readingName, $periodSwitch );} if ($$f[1] == 3) { statistics_doStatisticDuration ($hash, $dev, $readingName, $periodSwitch ); } if ($$f[1] == 4 && $periodSwitch>=1) { statistics_doStatisticTendency ($hash, $dev, $readingName, $$f[2]);} if ($$f[1] == 5) { statistics_doStatisticMinMax ($hash, $dev, $readingName, $$f[2], $periodSwitch, 1);} @@ -400,7 +401,7 @@ statistics_DoStatistics($$$) next if ($completeReadingName =~ m/^($exclReadings)$/ ); next if not exists ($dev->{READINGS}{$readingName}); $statisticDone = 1; - statistics_doStatisticDelta ($hash, $dev, $readingName, 3, $periodSwitch); + statistics_doStatisticDelta ($hash, $dev, $readingName, $periodSwitch); } @specialReadings = split /,/, AttrVal($hashName, "durationReadings", ""); @@ -642,17 +643,18 @@ statistics_doStatisticTendency ($$$$) # Calculates deltas for day, month and year sub ######################################## -statistics_doStatisticDelta ($$$$$) +statistics_doStatisticDelta ($$$$) { - my ($hash, $dev, $readingName, $decPlaces, $periodSwitch) = @_; + my ($hash, $dev, $readingName, $periodSwitch) = @_; my $dummy; my $result; my $showDate; my $name = $hash->{NAME}; - + my $decPlaces = 0; return if not exists ($dev->{READINGS}{$readingName}); + - # Get reading, cut out first number without units + # Get reading, extract first number without units my $value = $dev->{READINGS}{$readingName}{VAL}; $value =~ s/^[\D]*([\d.]*).*/$1/eg; Log3 $name, 4, "Calculating delta statistics for '".$dev->{NAME}.":$readingName = $value'"; @@ -677,6 +679,7 @@ statistics_doStatisticDelta ($$$$$) @stat = split / /, $dev->{READINGS}{$statReadingName}{VAL}; @hidden = split / /, $hash->{READINGS}{$hiddenReadingName}{VAL}; # Internal values $showDate = $hidden[3]; + $decPlaces = statistics_maxDecPlaces($value, $hidden[5]); if (exists ($dev->{READINGS}{$statReadingName."Last"})) { @last = split / /, $dev->{READINGS}{$statReadingName."Last"}{VAL}; } else { @@ -738,7 +741,7 @@ statistics_doStatisticDelta ($$$$$) } # Store hidden reading - $result = "LastValue: $value ShowDate: $showDate "; + $result = "LastValue: $value ShowDate: $showDate DecPlaces: $decPlaces"; readingsSingleUpdate($hash, $hiddenReadingName, $result, 0); Log3 $name,5,"$name: Set '$hiddenReadingName'='$result'"; @@ -963,6 +966,18 @@ statistics_FormatDuration($) return $returnstr; } +sub ######################################## +statistics_maxDecPlaces($$) +{ + my ($value,$decMax) = @_; + $decMax = 0 if $decMax == undef; + if ( $value =~ /.*\.(.*)/ ) { + my $decPlaces = length($1); + $decMax = $decPlaces >= $decMax ? $decPlaces : $decMax; + } + return $decMax; +} + sub ######################################## statistics_UpdateDevReading($$$$) { @@ -992,11 +1007,19 @@ statistics_UpdateDevReading($$$$)
Until now statistics for the following readings are automatically built: +
  • Min|Avg|Max Minimum, average and maximum of instantaneous values: +
    + over a period of day, month, year: brightness, current, energy_current, humidity, temperature, voltage +
    + over a period of hour, day, month, year: wind, wind_speed, windSpeed
  • +
  • Tendency over 1h, 2h, 3h und 6h: pressure
  • +
  • Delta between start and end values over a period of hour, day, month, year: +
    + count, energy, energy_total, power, total, rain, rain_rate, rain_total
  • +
  • Duration of the states over a period of day, month, year: +
    + lightsensor, lock, motion, Window, window, state (if no other reading is recognized)
  • + Further readings can be added via the correspondent attribute.
     
    @@ -1071,8 +1094,8 @@ statistics_UpdateDevReading($$$$)
  • singularReadings <DeviceRegExp:ReadingRegExp>:statTypes:period
    Regulare expression of statistic values, which shall not be shown in summary but also in singular readings. Eases the creation of plots. @@ -1099,23 +1122,24 @@ statistics_UpdateDevReading($$$$)

    statistics