From 8ed7397c9843dad1ac451a618f5c20a13bdf5e25 Mon Sep 17 00:00:00 2001 From: Wzut <> Date: Mon, 15 Feb 2021 18:30:28 +0000 Subject: [PATCH] 00_OW2S0SMSGUARD:add extra readings for both counters git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@23749 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- FHEM/00_OW2S0SMSGUARD.pm | 180 +++++++++++++++++++++++++++++---------- 1 file changed, 134 insertions(+), 46 deletions(-) diff --git a/FHEM/00_OW2S0SMSGUARD.pm b/FHEM/00_OW2S0SMSGUARD.pm index d77291069..6c56cca6c 100644 --- a/FHEM/00_OW2S0SMSGUARD.pm +++ b/FHEM/00_OW2S0SMSGUARD.pm @@ -125,7 +125,9 @@ sub DefineFn { $hash->{OWVals} = 0; $hash->{addr} = 'master'; $hash->{TIMEOUT} = 0.5; - setDevAttrList($name,'interval disable:0,1 DS2401_Timeout mapOWIDs useSubDevices:0,1 delay:0.01,0.05,0.1,0.5,1 model:master,unknown,DS2401,DS1820,DS18B20,DS1822 '.$readingFnAttributes); + setDevAttrList($name,'interval disable:0,1 DS2401_Timeout A_offset A_calc:after,before,never B_calc:after,before,never B_offset ' + .'mapOWIDs useSubDevices:0,1 delay:0.01,0.05,0.1,0.5,1 model:master,unknown,DS2401,DS1820,DS18B20,DS1822 ' + .$readingFnAttributes); CommandAttr(undef, "$name model master") if (!exists($attr{$name}{model})); } else { @@ -590,7 +592,8 @@ sub decodeVal { sub decodeTemperature { my ($model, @data) = @_; - my $temp = (( hex($data[1]) << 8) | hex($data[0])); + + my $temp = hex($data[1].$data[0]); if ($model eq 'DS1820') { @@ -598,20 +601,19 @@ sub decodeTemperature { $temp = $temp * 0.5; #$temp = ($temp & 0xFFF0) +12 - hex($data[6]) if ($data[7] eq '10'); # Alternative Berechnung : - $temp = ($temp - 0.25) + ((hex($data[7]) - hex($data[6])) / hex($data[7])) if ($data[7] ne '00'); } if (($model eq 'DS18B20') || ($model eq 'DS1822')) { - my $cfg = (hex($data[4]) & 0x60); - $temp = $temp << 3 if ($cfg == 0); - $temp = $temp << 2 if ($cfg == 0x20); - $temp = $temp << 1 if ($cfg == 0x40); + my $cfg = (hex($data[4]) & 0x60); # volle 12 Bit Auflösung + $temp = $temp << 3 if ($cfg == 0); # / 1000 ? 9 Bit Auflösung & -7 + $temp = $temp << 2 if ($cfg == 0x20); # / 100 ? 10 Bit Auflösung & -3 + $temp = $temp << 1 if ($cfg == 0x40); # / 10 ? 11 Bit Auflösung & -1 $temp = $temp/16.0; } - $temp -= 4096 if (hex($data[1]) > 127); # ToDo : testen ! + $temp -= 4096 if (hex($data[1]) > 127); return sprintf('%.1f', $temp); } @@ -685,20 +687,22 @@ sub ParseFn { sub UpdateReadings { my $hash = shift; + my $name = $hash->{NAME}; my @data = split(';', shift); + Log3($hash, 5, "$name, UpdateReadings data : ".join(' ',@data)); my @arr; - my $S0A = (looks_like_number($data[1])) ? int($data[1]) : ''; - my $S0B = (looks_like_number($data[2])) ? int($data[2]) : ''; - + my $S0A = (looks_like_number($data[1])) ? int($data[1]) : undef; + my $S0B = (looks_like_number($data[2])) ? int($data[2]) : undef; - Log3($hash, 5, "$hash->{NAME}, UpdateReadings data : ".join(' ',@data)); + $S0A += AttrNum($name, 'A_offset', 0) if (defined($S0A)); + $S0B += AttrNum($name, 'B_offset', 0) if (defined($S0B)); readingsBeginUpdate($hash); - readingsBulkUpdate($hash, 'A', $S0A) if ($S0A ne ''); - readingsBulkUpdate($hash, 'B', $S0B) if ($S0B ne ''); - readingsBulkUpdate($hash, 'state', "A: $S0A - B: $S0B") if (($S0A ne '') && ($S0B ne '')); + calcS0($hash, 'A', $S0A); + calcS0($hash, 'B', $S0B); + readingsBulkUpdate($hash, 'state', "A: $S0A - B: $S0B") if ((defined($S0A)) && (defined($S0B))); readingsEndUpdate($hash, 1); if (exists($hash->{helper}{OW}{100})) { @@ -706,7 +710,7 @@ sub UpdateReadings { readingsBeginUpdate($hash); foreach my $dev ( keys %{$hash->{helper}{OW}{100}} ) { - my $timeout = AttrNum($hash->{NAME}, 'DS2401_Timeout', ($hash->{INTERVAL} * 1.5)); + my $timeout = AttrNum($name, 'DS2401_Timeout', ($hash->{INTERVAL} * 1.5)); my $state = (index($hash->{DS2401}, $dev) != -1) ? 'present' : ''; my $t = 0; @@ -731,7 +735,7 @@ sub UpdateReadings { readingsBulkUpdate($hash, '.ds2401', OW_uniq($hash->{NAME}, '.ds2401', $hash->{DS2401})) if ($hash->{DS2401}); delete $hash->{DS2401}; - if (!AttrNum($hash->{NAME}, 'useSubDevices', 0)) { + if (!AttrNum($name, 'useSubDevices', 0)) { readingsEndUpdate($hash, 1); return; } @@ -870,6 +874,82 @@ sub crc8 { ##################################### +sub calcS0 { + + my $hash = shift; + my $c = shift // return; + my $v = shift // return; + + my $ti = time(); + + my ($Min, $Hour, $Month, $Year, $Wday); + my ($nMin, $nHour, $nMonth, $nYear, $nWday); + + readingsBulkUpdate($hash, $c, $v); + + if (AttrVal($hash->{NAME}, $c.'_calc', '') eq 'before') { + (undef, $Min, $Hour, undef, $Month, $Year, $Wday) = localtime($ti); + # Wann wäre der nächste Duchlauf ? + (undef, $nMin, $nHour, undef, $nMonth, $nYear, $nWday) = localtime($ti+$hash->{INTERVAL}); + Log3($hash, 4, "$hash->{NAME}, calcS0 $c before -> $Min:$nMin, $Hour:$nHour, $Month:$nMonth, $Year:$nYear, $Wday:$nWday"); + } + elsif (AttrVal($hash->{NAME}, $c.'_calc', '') eq 'after') { + # Wann war der letzte Durchlauf ? + $hash->{lastrun} = $ti if (!defined($hash->{lastrun})); # erster Durchlauf nach FHEM Neustart + (undef, $Min, $Hour, undef, $Month, $Year, $Wday) = localtime($hash->{lastrun}); + (undef, $nMin, $nHour, undef, $nMonth, $nYear, $nWday) = localtime($ti); + $hash->{lastrun} = $ti; + Log3($hash, 4, "$hash->{NAME}, calcS0 $c after -> $Min:$nMin, $Hour:$nHour, $Month:$nMonth, $Year:$nYear, $Wday:$nWday"); + } + else { + Log3($hash, 5, "$hash->{NAME}, calcS0 $c never"); + return; + } + + return if ($nMin == $Min); + + my $o = ReadingsNum($hash->{NAME}, $c.'_start_min', undef); + + readingsBulkUpdate($hash, $c.'_last_min' , ($v-$o)) if (defined($o)); + readingsBulkUpdate($hash, $c.'_start_min', $v); + + return if ($nHour == $Hour); + + $o = ReadingsNum($hash->{NAME}, $c.'_start_hour', undef); + + readingsBulkUpdate($hash, $c.'_last_hour' , ($v-$o)) if (defined($o)); + readingsBulkUpdate($hash, $c.'_start_hour', $v); + + return if ($nWday == $Wday); + + $o = ReadingsNum($hash->{NAME}, $c.'_start_day', undef); + + readingsBulkUpdate($hash, $c.'_last_day' , ($v-$o)) if (defined($o)); + readingsBulkUpdate($hash, $c.'_start_day', $v); + + if ($nWday == 1) { + $o = ReadingsNum($hash->{NAME}, $c.'_start_week', undef); + + readingsBulkUpdate($hash, $c.'_last_week' , ($v-$o)) if (defined($o)); + readingsBulkUpdate($hash, $c.'_start_week', $v); + } + + return if ($nMonth == $Month); + + $o = ReadingsNum($hash->{NAME}, $c.'_start_month', undef); + + readingsBulkUpdate($hash, $c.'_last_month', ($v-$o)) if (defined($o)); + readingsBulkUpdate($hash, $c.'_start_month', $v); + + return if ($nYear == $Year); + + $o = ReadingsNum($hash->{NAME}, $c.'_start_year', undef); + readingsBulkUpdate($hash, $c.'_last_year', ($v-$o)) if (defined($o)); + readingsBulkUpdate($hash, $c.'_start_year', $v); + + return; +} + 1; __END__ @@ -900,34 +980,37 @@ FHEM Forum : 1Wire<
Set + Get + Attributes - - - - @@ -950,47 +1033,52 @@ FHEM Forum : 1Wire< define myOW2S0 OW2S0SMSGUARD 192.168.0.100:2000 (socat, ser2net)
-
Set + +
+ Get + +
Attribute - - - - + =end html_DE