diff --git a/FHEM/15_CUL_EM.pm b/FHEM/15_CUL_EM.pm index dcea6599e..166fa2d3c 100755 --- a/FHEM/15_CUL_EM.pm +++ b/FHEM/15_CUL_EM.pm @@ -23,6 +23,7 @@ CUL_EM_Initialize($) $hash->{ParseFn} = "CUL_EM_Parse"; $hash->{AttrList} = "IODev do_not_notify:0,1 showtime:0,1 " . "model:EMEM,EMWZ,EMGZ ignore:0,1 ". + "maxPeak ". $readingFnAttributes; $hash->{AutoCreate}= { "CUL_EM.*" => { GPLOT => "power8:Power,", FILTER => "%NAME:CNT.*" } }; @@ -138,12 +139,6 @@ CUL_EM_Parse($$) $basis_cnt = $hash->{READINGS}{basis}{VAL}; } - # correct counter wraparound - if($total_cnt < $total_cnt_last) { - # check: real wraparound or reset only - $basis_cnt += ($total_cnt_last > 65000 ? 65536 : $total_cnt_last); - $readings{basis} = $basis_cnt; - } # # translate into device units @@ -151,18 +146,44 @@ CUL_EM_Parse($$) my $corr1 = $hash->{corr1}; # EMEM power correction factor my $corr2 = $hash->{corr2}; # EMEM energy correction factor - my $total = ($basis_cnt+$total_cnt)*$corr2; - my $current = $current_cnt*$corr1; - my $peak = $peak_cnt*$corr1; + my $peak; if($tpe ne 2) { - $peak = 3000/($peak_cnt > 1 ? $peak_cnt : 1)*$corr1; - $peak = ($current > 0 ? $peak : 0); + $peak = $current_cnt && $peak_cnt ? 3000/$peak_cnt*$corr1 : 0; + # when EM detection toggles/glitches somewhere the internal + # EM-Counter increments by one and the device registers a + # very hi peak value + # Here we fix this by checking against a maximum peak + # level, removing the wrong counter increment and + # setting peak to the current value. + my $maxpeak = $attr{$n}{"maxPeak"}; + if(defined $maxpeak and $peak > $maxpeak){ + Log3 $n, 2, + "CUL_EM $n: max peak detected: $peak kW > $maxpeak kW"; + $current_cnt--; + # as total_cnt is "owned" by EM we decrement our basis_cnt + $basis_cnt--; + $readings{basis} = $basis_cnt; + $peak = $current_cnt*$corr1; + $peak_cnt = $peak ? int(3000*$corr1/$peak) : 0; + } + } else { + $peak = $peak_cnt*$corr1; } + # correct counter wraparound + if($total_cnt < $total_cnt_last) { + # check: real wraparound or reset only + $basis_cnt += ($total_cnt_last > 65000 ? 65536 : $total_cnt_last); + $readings{basis} = $basis_cnt; + } + + my $total = ($basis_cnt+$total_cnt)*$corr2; + my $current = $current_cnt*$corr1; $val = sprintf("CNT: %d CUM: %0.3f 5MIN: %0.3f TOP: %0.3f", - $seqno, $total, $current, $peak); + $seqno, $total, $current, $peak); + readingsBeginUpdate($hash); readingsBulkUpdate($hash, "state", $val); @@ -321,7 +342,15 @@ CUL_EM_Parse($$)