diff --git a/FHEM/73_ElectricityCalculator.pm b/FHEM/73_ElectricityCalculator.pm index cd9f50dfd..9bf3ce7b4 100644 --- a/FHEM/73_ElectricityCalculator.pm +++ b/FHEM/73_ElectricityCalculator.pm @@ -265,22 +265,33 @@ sub ElectricityCalculator_Get($@) ### If not enough arguments have been provided if ( @a < 2 ) { - return "\"get ElectricityCalculator\" needs at least one argument"; + return "\"set ElectricityCalculator\" needs at least one argument"; } my $ElectricityCalcName = shift @a; my $reading = shift @a; - my $value; + my $value = join(" ", @a); my $ReturnMessage; + my @cList; - if(!defined($hash->{helper}{gets}{$reading})) - { - my @cList = keys %{$hash->{helper}{gets}}; - return "Unknown argument $reading, choose one of " . join(" ", @cList); + ### Create Log entries for debugging + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. "_Get - reading : " . $reading; + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. "_Get - value : " . $value; - ### Create Log entries for debugging - Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " : ElectricityCalculator - get list: " . join(" ", @cList); + + if(defined($hash->{READINGS})) { + push(@cList, " "); + push(@cList, keys(%{$hash->{READINGS}})); } + else { + push(@cList, " "); + } + + ### Create Log entries for debugging + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " : ElectricityCalculator - set list: " . join(" ", @cList); + + return "Unknown argument $reading, choose one of " . join(" ", @cList) if $reading eq '?'; + if ( $reading ne "?") { @@ -313,17 +324,65 @@ sub ElectricityCalculator_Set($@) my $reading = shift @a; my $value = join(" ", @a); my $ReturnMessage; + my @cList; - if(!defined($hash->{helper}{gets}{$reading})) - { - my @cList = keys %{$hash->{helper}{sets}}; - return "Unknown argument $reading, choose one of " . join(" ", @cList); + ### Create Log entries for debugging + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. "_Set - reading : " . $reading; + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. "_Set - value : " . $value; - ### Create Log entries for debugging - Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " : ElectricityCalculator - set list: " . join(" ", @cList); - } - if ( $reading ne "?") + if(defined($hash->{READINGS})) { + push(@cList, "SyncCounter"); + push(@cList, keys(%{$hash->{READINGS}})); + } + else { + push(@cList, "SyncCounter"); + } + + ### Create Log entries for debugging + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " : ElectricityCalculator - set list: " . join(" ", @cList); + + return "Unknown argument $reading, choose one of " . join(" ", @cList) if $reading eq '?'; + + ### If the command supposed to synchronize the CounterValues between CounterModule and CalculatorModule + if ($reading eq "SyncCounter") { + ### Create Log entries for debugging + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " - Syncing Counter with :" . $value; + + ### Sreach for the ReadingsName of the Current CounterValue + my @SearchResult = grep(/_CounterCurrent/, @cList); + + ### Get current CalculatorValue + my $CalculaterValueCurrent = ReadingsVal($ElectricityCalcName, $SearchResult[0], 0); + + ### Get current Offset from Attribute + my $CounterOffsetCurrent = AttrVal($ElectricityCalcName, "ElectricityCounterOffset", 0); + + ### Calculate CounterValue + my $CounterValueCurrent = $CalculaterValueCurrent - $CounterOffsetCurrent; + + ### Calculate new Offset + my $CounterOffsetNew = $value - $CounterValueCurrent; + + ### Calculate Ceck +# my $CounterValueNew = $CounterValueCurrent + $CounterOffsetNew; + + ### Create Log entries for debugging + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " - Search Result : " . Dumper(@SearchResult); + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " - CounterValueNew Given : " . $value; + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " - CounterValueCurrent Result : " . $CalculaterValueCurrent; + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " - CounterOffsetCurrent Result : " . $CounterOffsetCurrent; + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " - CounterValueCurrent Result : " . $CounterValueCurrent; + Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " - CounterOffsetNew Result : " . $CounterOffsetNew; +# Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " - CounterValueNew Check : " . $CounterValueNew; + + ### Set new Offset in Attributes + $attr{$ElectricityCalcName}{ElectricityCounterOffset} = $CounterOffsetNew; + + ### Create ReturnMessage + $ReturnMessage = $ElectricityCalcName . " - Successfully synchromized Counter and Calculator with : " . $value . " kWh"; + } + elsif ($reading ne "?") { ### Create Log entries for debugging Log3 $ElectricityCalcName, 5, $ElectricityCalcName. " : ElectricityCalculator - set " . $reading . " with value: " . $value; @@ -962,8 +1021,9 @@ sub ElectricityCalculator_Notify($$) @@ -1547,8 +1607,9 @@ sub ElectricityCalculator_Notify($$)