From a7e1ba6ffe3e9c6319e516eedf1a3bc203db9b2f Mon Sep 17 00:00:00 2001 From: pahenning Date: Sat, 11 Jan 2014 06:16:17 +0000 Subject: [PATCH] git-svn-id: svn://svn.code.sf.net/p/fhem/code/trunk@4616 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/21_OWCOUNT.pm | 218 ++++++++++++++++++++++++++++++++++------ 1 file changed, 186 insertions(+), 32 deletions(-) diff --git a/fhem/FHEM/21_OWCOUNT.pm b/fhem/FHEM/21_OWCOUNT.pm index ba530a59b..bc4849897 100644 --- a/fhem/FHEM/21_OWCOUNT.pm +++ b/fhem/FHEM/21_OWCOUNT.pm @@ -25,9 +25,11 @@ # get present => 1 if device present, 0 if not # get interval => query interval # get memory => 32 byte string from page 0..13 -# get midnight => todays starting value for counter -# get counter => value for counter -# get counters => values for both counters +# get midnight => todays starting value (formatted) for counter +# get month => summary and average for month +# get year => summary and average for year +# get raw => raw value for counter +# get counters => formatted values for both counters # get version => OWX version number # # set interval => set query interval for measurement @@ -80,7 +82,7 @@ use strict; use warnings; sub Log($$); -my $owx_version="3.31"; +my $owx_version="3.32"; #-- fixed raw channel name, flexible channel name my @owg_fixed = ("A","B"); my @owg_channel = ("A","B"); @@ -97,9 +99,10 @@ my %gets = ( "interval" => "", "memory" => "", "midnight" => "", - "counter" => "", + "raw" => "", "counters" => "", "month" => "", + "year" => "", "version" => "" ); @@ -372,7 +375,7 @@ sub OWCOUNT_FormatValues($) { my ($sec, $min, $hour, $day, $month, $year, $wday,$yday,$isdst) = localtime(time); my ($seco,$mino,$houro,$dayo,$montho,$yearo,$dayrest); my ($daily, $dt,$dval,$dval2,$deltim,$delt,$delf); - my ($total,$total0,$total1,@monthv); + my ($total,$total0,$total1,$total2,$total3,@monthv,@yearv); my $daybreak = 0; my $monthbreak = 0; @@ -454,7 +457,7 @@ sub OWCOUNT_FormatValues($) { $dayo = substr($dayrest,0,2); ($houro,$mino,$seco) = split(/:/,substr($dayrest,3)); - #-- time dfifference to previous measurement and to midnight + #-- time difference to previous measurement and to midnight $delt = ($hour-$houro)*3600 + ($min-$mino)*60 + ($sec-$seco); $delf = $hour *3600 + $min *60 + $sec - 86400; @@ -488,8 +491,8 @@ sub OWCOUNT_FormatValues($) { OWCOUNT_SetPage($hash,14+$i,sprintf("%f",$dval2)); #-- string buildup for monthly and yearly logging - $dvalue .= sprintf( " %s: %5.1f %s %sm: %%5.1f %s", $owg_channel[$i],$dval,$unit,$owg_channel[$i],$unit); - $mvalue .= sprintf( " %s: %%5.1f %s", $owg_channel[$i],$unit); + $dvalue .= sprintf( " %s: %5.1f %s %sm: %%5.1f %s", $owg_channel[$i],$dval,$unit,$owg_channel[$i],$unit); + $mvalue .= sprintf( " %s: %%5.1f %s %sy: %%5.1f %s", $owg_channel[$i],$unit,$owg_channel[$i],$unit); } #-- end daybreak #-- string buildup for return value and STATE @@ -515,6 +518,7 @@ sub OWCOUNT_FormatValues($) { if( $daybreak == 1 ){ #-- daily/monthly accumulated value @monthv = OWCOUNT_GetMonth($hash); + @yearv = OWCOUNT_GetYear($hash); #-- error check if( int(@monthv) == 2 ){ $total0 = $monthv[0]->[1]; @@ -524,13 +528,21 @@ sub OWCOUNT_FormatValues($) { $total0 = ""; $total1 = ""; }; - #-- put in monthly sums + if( int(@yearv) == 2 ){ + $total2 = $yearv[0]->[1]; + $total3 = $yearv[1]->[1]; + }else{ + Log 3,"OWCOUNT: No yearly summary possible, ".$yearv[0]; + $total2 = ""; + $total3 = ""; + }; + #-- put in monthly and yearly sums $dvalue = sprintf("D%02d ",$day).$dvalue; $dvalue = sprintf($dvalue,$total0,$total1); readingsBulkUpdate($hash,"day",$dvalue); if ( $monthbreak == 1){ $mvalue = sprintf("M%02d ",$month+1).$mvalue; - $mvalue = sprintf($mvalue,$total0,$total1); + $mvalue = sprintf($mvalue,$total2,$total3); readingsBulkUpdate($hash,"month",$mvalue); } } @@ -622,7 +634,7 @@ sub OWCOUNT_Get($@) { } if( $daily==1){ $value .= $owg_channel[$i]."m: ".$month2[$i]->[1]." ".$unit. - " (monthly sum, average ".$month2[$i]->[2]." ".$unit."/d)\n"; + " (monthly sum until now, average ".$month2[$i]->[2]." ".$unit."/d)\n"; }else{ $value .= $owg_channel[$i]."m: ".$month2[$i]->[1]." ".$unit." (last midnight)\n"; } @@ -630,6 +642,34 @@ sub OWCOUNT_Get($@) { return $value; } + #-- get year + if($a[1] eq "year") { + $value="$name.year =>\n"; + my @year2 = OWCOUNT_GetYear($hash); + #-- error case + if( int(@year2) != 2 ){ + return $value." no yearly summary possible, ".$year2[0]; + } + #-- 3 entries for each month + for(my $i=0;$i{READINGS}{$owg_channel[$i]}{UNITABBR}; + #-- mode = daily ? + $daily = 0; + if( defined($attr{$name}{$owg_fixed[$i]."Mode"} )){ + if( $attr{$name}{$owg_fixed[$i]."Mode"} eq "daily"){ + $daily = 1; + } + } + if( $daily==1){ + $value .= $owg_channel[$i]."y: ".$year2[$i]->[1]." ".$unit. + " (yearly sum until now, average ".$year2[$i]->[2]." ".$unit."/d)\n"; + }else{ + $value .= $owg_channel[$i]."y: ".$year2[$i]->[1]." ".$unit." (last month)\n"; + } + } + return $value; + } + #-- get memory page/counter according to interface type my $interface= $hash->{IODev}->{TYPE}; @@ -673,8 +713,8 @@ sub OWCOUNT_Get($@) { } #-- check syntax for getting counter - if( $reading eq "counter" ){ - return "OWCOUNT: Get needs parameter when reading counter: " + if( $reading eq "raw" ){ + return "OWCOUNT: Get needs parameter when reading raw counter: " if( int(@a)<2 ); #-- find out which channel we have if( ($a[2] eq $owg_channel[0]) || ($a[2] eq "A") ){ @@ -695,6 +735,13 @@ sub OWCOUNT_Get($@) { }else{ return "OWCOUNT: Get with wrong IODev type $interface"; } + if( defined($ret) ){ + return "OWCOUNT: Could not get values from device $name, reason: ".$ret; + } + $hash->{PRESENT} = 1; + #-- only one counter will be returned + OWCOUNT_FormatValues($hash); + return "OWCOUNT: $name.raw $a[2] => ".$owg_val[$page-14]; #-- check syntax for getting counters }elsif( $reading eq "counters" ){ return "OWCOUNT: Get needs no parameter when reading counters" @@ -709,17 +756,18 @@ sub OWCOUNT_Get($@) { }else{ return "OWCOUNT: GetValues with wrong IODev type $interface"; } + #-- process results + $ret .= $ret1 + if( defined($ret1) ); + $ret .= $ret2 + if( defined($ret2) ); + if( defined($ret) ){ + return "OWCOUNT: Could not get values from device $name, reason: ".$ret; + } + $hash->{PRESENT} = 1; + #-- both counters will be returned + return "OWCOUNT: $name.counters => ".OWCOUNT_FormatValues($hash); } - #-- process results - $ret .= $ret1 - if( defined($ret1) ); - $ret .= $ret2 - if( defined($ret2) ); - if( $ret ne "" ){ - return "OWCOUNT: Could not get values from device $name, reason: ".$ret; - } - $hash->{PRESENT} = 1; - return "OWCOUNT: $name.$reading => ".OWCOUNT_FormatValues($hash); } ######################################################################################## @@ -764,7 +812,7 @@ sub OWCOUNT_GetMonth($) { if( $ret) { while( ){ #-- line looks as - # 2013-02-09_23:59:31 day: D09 : 180.0 : 180.0 : 180.0 : 180.0 + # 2013-06-25_23:57:57 DG.CT1 day: D25 W: 42.2 kWh Wm: 84.4 kWh B: 2287295.7 cts Bm: 2270341.0 cts $line = $_; chomp($line); if ( $line =~ m/$regexp/i){ @@ -792,7 +840,7 @@ sub OWCOUNT_GetMonth($) { #-- sum and average for (my $i=0;$i{NAME}; + my $regexp = ".*$name.*"; + my $val; + my @year = (); + my @year2 = (); + my @mchannel; + my @linarr; + my $month; + my $line; + my ($total,$total2,$daily,$deltim,$av); + + #-- Check current logfile + my $ln = $attr{$name}{"LogY"}; + if( !(defined($ln))){ + return "attribute LogY is missing"; + } + + #-- get channel names + OWCOUNT_ChannelNames($hash); + + my $lf = $defs{$ln}{currentlogfile}; + if( !(defined($lf))){ + return "logfile of LogY is missing"; + } + + my $ret = open(OWXFILE, "< $lf" ); + if( $ret) { + while( ){ + #-- line looks as + # 2013-05-31_23:57:57 DG.CT1 month: M05 W: 42.2 kWh Wy: 84.4 kWh B: 2287295.7 cts By: 2270341.0 cts + $line = $_; + chomp($line); + if ( $line =~ m/$regexp/i){ + @linarr = split(' ',$line); + if( int(@linarr)==4+6*int(@owg_fixed) ){ + $month = $linarr[3]; + $month =~ s/M_0+//; + @mchannel = (); + for (my $i=0;$iset <name> interval <int>
Measurement interval in seconds. The default is 300 seconds.
  • - set <name> memory <page>
    Write 32 bytes to + set <name> memory <page> <string>
    Write 32 bytes to memory page 0..13
  • - set <name> midnight <channel-name>
    Write the + set <name> midnight <channel-name> <int>
    Write the day's starting value for counter <channel> (A, B or named channel, see below)
  • @@ -1429,6 +1576,10 @@ sub OWXCOUNT_SetPage($$$) { get <name> midnight <channel-name>
    Obtain the day's starting value for counter <channel> (A, B or named channel, see below) +
  • + get <name> month
    Returns cumulated and averaged monthly value if mode=daily, otherwise last day's and averaged value
  • +
  • + get <name> year
    Returns cumulated and averaged yearly value if mode=daily, otherwise last months's and averaged value
  • get <name> counter <channel-name>
    Obtain the current value for counter <channel> (A, B or named channel, see below)
  • @@ -1441,10 +1592,10 @@ sub OWXCOUNT_SetPage($$$) {

    Attributes

    For each of the following attributes, the channel identification A,B may be used.

    @@ -1466,7 +1617,10 @@ sub OWXCOUNT_SetPage($$$) {
    factor multiplied to (reading+offset) in this channel.
  • attr <name> <channel>Mode daily | normal -
    factor multiplied to (reading+offset) in this channel.
  • +
    determines whether counter is nulled at start of day or running continuously +
  • attr <name> <channel>Period hour(default) | minute | + second +
    period for rate calculation
  • Standard attributes alias, comment, event-on-update-reading, event-on-change-reading, =end html -=cut +=cut \ No newline at end of file