SENSOR_MULTILEVEL and negative readings

Begonnen von simli, 08 Februar 2014, 20:18:39

Vorheriges Thema - Nächstes Thema


Hi everyone,

I have been running my own version of for quite a while because the components I have did not work straight out of the box with Fhem. But I just noticed that the code has been updated a bit since last time I checked. 

Upgrade looks good, but there is still a small problem with my temperature readings. Negative values ( below zero ) are not correct. I know that the sensors I have use "2's complement"-encoding of the value in the ZWave-message. I guess it will be so for all temperature sensors, and it would surprise me if this isn't true for all values in the SENSOR_MULTILEVEL -message.
Has anyone else seen the problem in other sensors with this messagetype?  I guess temperature is the one type that you will actually see negative values for. Readings like power and humidity etc will all be positive or zero.

Anyway, I have patched the current version of so that it should work for negative readings, ig anyone wants to try it. At least it works for me.  :)

--- 2014-02-08 18:08:55.309988918 +0100
+++ 2014-02-08 18:11:49.721212671 +0100
@@ -484,9 +484,12 @@ ZWave_ParseMultilevel($$$)

   my $pr = (hex($fl)>>5)&0x07; # precision
   my $sc = (hex($fl)>>3)&0x03; # scale
+  my $bc = (hex($fl)>>0)&0x07; # bytecount
+  my $msb = (hex($arg)>>8*$bc-1); # most significant bit  ( 0 = pos, 1 = neg )
+  my $val = $msb ? -( 2 ** (8 * $bc) - hex($arg) ) : hex($arg); # 2's complement   
   my $ml = $ml_tbl{$type};
   return "UNKNOWN multilevel type: $type fl: $fl arg: $arg" if(!$ml);
-  return sprintf("%s:%.*f %s", $ml->{n}, $pr, hex($arg)/(10**$pr),
+  return sprintf("%s:%.*f %s", $ml->{n}, $pr, $val/(10**$pr),
        int(@{$ml->{st}}) > $sc ? $ml->{st}->[$sc] : "");