From 04733923c911d932834424ae531a7522bbd73aa7 Mon Sep 17 00:00:00 2001 From: nasseeder1 Date: Wed, 17 Apr 2024 17:44:02 +0000 Subject: [PATCH] 76_SolarForecast: possible Illegal division by zero if Battery SOC management is activated git-svn-id: https://svn.fhem.de/fhem/trunk@28801 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 2 ++ fhem/FHEM/76_SolarForecast.pm | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index c7bfc906c..0d93c594c 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,7 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it + - bugfix: 76_SolarForecast: possible Illegal division by zero if Battery SOC + management is activated - change: 76_SolarForecast: minor change calculation Today PV deviation - change: 49_SSCam: change internal logic for sending data and their Saving - change: 10_SOMFY.pm: Signalduino modif and doc cleanup - msg1298875 diff --git a/fhem/FHEM/76_SolarForecast.pm b/fhem/FHEM/76_SolarForecast.pm index fc0968a6c..929327681 100644 --- a/fhem/FHEM/76_SolarForecast.pm +++ b/fhem/FHEM/76_SolarForecast.pm @@ -158,6 +158,7 @@ BEGIN { # Versions History intern my %vNotesIntern = ( + "1.17.9" => "17.04.2024 _batSocTarget: fix Illegal division by zero, Forum: https://forum.fhem.de/index.php?msg=1310930 ", "1.17.8" => "16.04.2024 calcTodayPVdeviation: change of calculation ", "1.17.7" => "09.04.2024 export pvHistory to CSV, making attr affectMaxDayVariance obsolete ", "1.17.6" => "07.04.2024 new sub writeToHistory with many internal changes in pvHistory write process ". @@ -5332,8 +5333,8 @@ sub Attr { if ($aName eq 'ctrlBatSocManagement' && $init_done) { if ($cmd eq 'set') { - return qq{Define the key "cap" with "set $name currentBatteryDev" before this attribute.} - if(ReadingsVal ($name, 'currentBatteryDev', '') !~ /\s+cap=/xs); + return qq{Define the key "cap" with "set $name currentBatteryDev" before this attribute in the correct form.} + if(!CurrentVal($hash, 'batinstcap', 0)); # https://forum.fhem.de/index.php?msg=1310930 my ($lowSoc, $upSoc, $maxsoc, $careCycle) = __parseAttrBatSoc ($name, $aVal); @@ -8140,13 +8141,19 @@ sub _batSocTarget { my $hash = $paref->{hash}; my $name = $paref->{name}; my $type = $paref->{type}; - my $t = $paref->{t}; # aktuelle Zeit + my $t = $paref->{t}; # aktuelle Zeit return if(!isBatteryUsed ($name)); - my $oldd2care = CircularVal ($hash, 99, 'days2care', 0); - my $ltsmsr = CircularVal ($hash, 99, 'lastTsMaxSocRchd', undef); - my $batcharge = CurrentVal ($hash, 'batcharge', 0); # aktuelle Ladung in % + my $oldd2care = CircularVal ($hash, 99, 'days2care', 0); + my $ltsmsr = CircularVal ($hash, 99, 'lastTsMaxSocRchd', undef); + my $batcharge = CurrentVal ($hash, 'batcharge', 0); # aktuelle Ladung in % + my $batinstcap = CurrentVal ($hash, 'batinstcap', 0); # installierte Batteriekapazität Wh + + if (!$batinstcap) { + Log3 ($name, 1, "$name - WARNING - Attribute ctrlBatSocManagement is active, but the required key 'cap' is not setup in currentBatteryDev. Exit."); + return; + } __batSaveSocKeyFigures ($paref) if(!$ltsmsr || $batcharge >= $maxSoCdef || $oldd2care < 0); @@ -8203,7 +8210,6 @@ sub _batSocTarget { my $csopt = ReadingsNum ($name, 'Battery_OptimumTargetSoC', $lowSoc); # aktuelles SoC Optimum my $pvexpect = $pvfctm > $pvfctd ? $pvfctm : $pvfctd; - my $batinstcap = CurrentVal ($hash, 'batinstcap', 0); # installierte Batteriekapazität Wh my $cantarget = 100 - (100 / $batinstcap) * $pvexpect; # berechneter möglicher Min SOC nach Berücksichtigung Ladewahrscheinlichkeit my $newtarget = sprintf "%.0f", ($cantarget < $target ? $cantarget : $target); # Abgleich möglicher Min SOC gg. berechneten Min SOC