From 10781f0f64063d88d1cfe6ee7249c0c72dbf917f Mon Sep 17 00:00:00 2001 From: HCS <> Date: Sat, 25 May 2019 19:38:31 +0000 Subject: [PATCH] 36_Level.pm: added RSSI and round liters git-svn-id: https://svn.fhem.de/fhem/trunk@19465 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/36_Level.pm | 50 ++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/fhem/FHEM/36_Level.pm b/fhem/FHEM/36_Level.pm index 0e6e197db..9336da3cf 100644 --- a/fhem/FHEM/36_Level.pm +++ b/fhem/FHEM/36_Level.pm @@ -1,7 +1,4 @@ - # $Id$ -# -# TODO: package main; @@ -27,6 +24,7 @@ Level_Initialize($) ." filterThreshold" ." litersPerCm" ." distanceToBottom" + ." formula:textField-long" ." $readingFnAttributes"; } @@ -123,10 +121,7 @@ Level_Parse($$) my ($hash, $msg) = @_; my $name = $hash->{NAME}; - ###$DB::single = 1; - - - my( @bytes, $addr, $type, $distance, $temperature, $voltage ); + my( @bytes, $addr, $type, $distance, $temperature, $voltage, $rssi ); if( $msg =~ m/^OK LS/ ) { @bytes = split( ' ', substr($msg, 5) ); @@ -135,6 +130,9 @@ Level_Parse($$) $distance = ($bytes[2]*256 + $bytes[3] - 1000)/10; $temperature = ($bytes[4]*256 + $bytes[5] - 1000)/10; $voltage = $bytes[6] / 10; + if(@bytes > 7) { + $rssi = $bytes[7] + } } else { DoTrigger($name, "UNKNOWNCODE $msg"); Log3 $name, 3, "$name: Unknown code $msg, help me!"; @@ -157,33 +155,46 @@ Level_Parse($$) readingsBeginUpdate($rhash); - my $litersPerCm = AttrVal( $rname, "litersPerCm", 1); + my $litresPerCm = AttrVal( $rname, "litersPerCm", 1); my $distanceToBottom = AttrVal( $rname, "distanceToBottom", 100); my $level = $distanceToBottom - $distance; - my $liters = $litersPerCm * $level; + + my $litres = 0; + my $formula = AttrVal( $rname, "formula", undef); + if($formula){ + eval($formula); + } + else { + $litres = $litresPerCm * $level; + } if( AttrVal( $rname, "doAverage", 0 ) && defined($rhash->{"previousLiters"}) ) { - $liters = int(($rhash->{"previousLiters"}*3+$liters)/4); + $litres = int(($rhash->{"previousLiters"}*3+$litres)/4); } if( AttrVal( $rname, "doAverage", 0 ) && defined($rhash->{"previousTemeprature"}) ) { $temperature = int(($rhash->{"previousTemeprature"}*3+$temperature)/4); } - + + $litres = int($litres); + readingsBulkUpdate($rhash, "distance", $distance); readingsBulkUpdate($rhash, "level", $level); - readingsBulkUpdate($rhash, "liters", $liters); + readingsBulkUpdate($rhash, "liters", $litres); readingsBulkUpdate($rhash, "temperature", $temperature); readingsBulkUpdate($rhash, "voltage", $voltage); + if($rssi) { + readingsBulkUpdate($rhash, "rssi", $rssi); + } - my $state = "L: $liters"; + my $state = "L: $litres"; $state .= " T: $temperature"; $state .= " V: $voltage"; readingsBulkUpdate($rhash, "state", $state) if( Value($rname) ne $state ); readingsEndUpdate($rhash,1); - $rhash->{"previousLiters"} = $liters; + $rhash->{"previousLiters"} = $litres; $rhash->{"previousTemperature"} = $temperature; return @list; @@ -245,7 +256,16 @@ Level_Attr(@)
  • distanceToBottom
    Distance from the ultra sonic sensor to the bottom of the tank
  • litersPerCm
    - Liters for each cm level
  • + Liters for each cm level. Only used if the attribute formula is not set +
  • formula
    + Own calculation of the content, e.g for a lying ton
    + Get the variabl $level for the current level and must provide the result in $litres
    + Example:
    + my $tankRadius = 0.6;
    + my $tankLength = 2.35;
    + my $levelMtr = $level / 100;
    + $litres = int((($tankRadius ** 2) * acos(($tankRadius - $levelMtr) / $tankRadius) - ($tankRadius - $levelMtr)
    + * sqrt(($tankRadius ** 2) - (($tankRadius - $levelMtr) * ($tankRadius - $levelMtr)))) * $tankLength * 1000 + 0.5);