2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-13 05:06:35 +00:00

added counter differential per time in 81_M232Counter.pm

git-svn-id: https://svn.fhem.de/fhem/trunk@352 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
borisneubert 2009-02-14 20:54:45 +00:00
parent 8b31e2a7ce
commit 52cd8fcf23
4 changed files with 126 additions and 39 deletions

View File

@ -491,3 +491,4 @@
- feature: avoid the "unknown/help me" message for unloaded devices - feature: avoid the "unknown/help me" message for unloaded devices
- feature: structure module for big installations - feature: structure module for big installations
- feature: Cost Control in 15_CUL_EM (CostPerUnit, BasisFeePerMonth) - feature: Cost Control in 15_CUL_EM (CostPerUnit, BasisFeePerMonth)
- feature: add counter differential per time in 81_M232Counter.pm

View File

@ -1,7 +1,7 @@
# #
# #
# 81_M232Counter.pm # 81_M232Counter.pm
# written by Dr. Boris Neubert 2007-11-26 # written by Dr. Boris Neubert 2007-11-26
# e-mail: omega at online dot de # e-mail: omega at online dot de
# #
############################################## ##############################################
@ -37,10 +37,11 @@ M232Counter_GetStatus($)
my ($hash) = @_; my ($hash) = @_;
if(!$hash->{LOCAL}) { if(!$hash->{LOCAL}) {
InternalTimer(gettimeofday()+60, "M232Counter_GetStatus", $hash, 1); InternalTimer(gettimeofday()+$hash->{INTERVAL}, "M232Counter_GetStatus", $hash, 1);
} }
my $name = $hash->{NAME}; my $name = $hash->{NAME};
my $r= $hash->{READINGS};
my $d = IOWrite($hash, "z"); my $d = IOWrite($hash, "z");
if(!defined($d)) { if(!defined($d)) {
@ -49,29 +50,81 @@ M232Counter_GetStatus($)
return $msg; return $msg;
} }
# time
my $tn = TimeNow(); my $tn = TimeNow();
if(!defined($hash->{READINGS}{basis})) {
$hash->{READINGS}{basis}{VAL}= 0;
$hash->{READINGS}{basis}{TIME}= $tn;
}
if(!defined($hash->{READINGS}{count})) {
$hash->{READINGS}{count}{VAL}= 0;
$hash->{READINGS}{count}{TIME}= $tn;
}
my $count= hex $d;
if($count< $hash->{READINGS}{count}{VAL}) {
$hash->{READINGS}{basis}{VAL}+= 65536;
$hash->{READINGS}{basis}{TIME}= $tn;
}
my $value= ($hash->{READINGS}{basis}{VAL}+$count) * $hash->{FACTOR};
$hash->{READINGS}{count}{TIME} = $tn; #tsecs
$hash->{READINGS}{count}{VAL} = $count; my $tsecs= time(); # number of non-leap seconds since January 1, 1970, UTC
$hash->{READINGS}{value}{TIME} = $tn;
$hash->{READINGS}{value}{VAL} = $value; # previous tsecs
my $tsecs_prev;
if(defined($r->{tsecs})) {
$tsecs_prev= $r->{tsecs}{VAL};
} else{
$tsecs_prev= $tsecs; # 1970-01-01
}
# basis
my $basis;
if(defined($r->{basis})) {
$basis= $r->{basis}{VAL};
} else {
$basis= 0;
}
my $basis_prev= $basis;
# previous count
my $count_prev;
if(defined($r->{count})) {
$count_prev= $r->{count}{VAL};
} else {
$count_prev= 0;
}
# current count
my $count= hex $d;
if($count< $count_prev) {
$basis+= 65536;
$r->{basis}{VAL} = $basis;
$r->{basis}{TIME}= $tn;
}
# previous value
my $value_prev;
if(defined($r->{value})) {
$value_prev= $r->{value}{VAL};
} else {
$value_prev= 0;
}
# current value
my $value= ($basis+$count) * $hash->{FACTOR};
# round to 3 digits
$value= int($value*1000.0+0.5)/1000.0;
# set new values
$r->{count}{TIME} = $tn;
$r->{count}{VAL} = $count;
$r->{value}{TIME} = $tn;
$r->{value}{VAL} = $value;
$r->{tsecs}{TIME} = $tn;
$r->{tsecs}{VAL} = $tsecs;
$hash->{CHANGED}[0]= "value: $value"; $hash->{CHANGED}[0]= "value: $value";
# delta
my $tsecs_delta= $tsecs-$tsecs_prev;
my $count_delta= ($count+$basis)-($count_prev+$basis_prev);
if($tsecs_delta>0) {
my $delta= ($count_delta/$tsecs_delta)*$hash->{DELTAFACTOR};
# round to 3 digits
$delta= int($delta*1000.0+0.5)/1000.0;
$r->{delta}{TIME} = $tn;
$r->{delta}{VAL} = $delta;
$hash->{CHANGED}[1]= "delta: $delta";
}
if(!$hash->{LOCAL}) { if(!$hash->{LOCAL}) {
DoTrigger($name, undef) if($init_done); DoTrigger($name, undef) if($init_done);
} }
@ -121,7 +174,7 @@ M232Counter_Calibrate($@)
# recalculate value # recalculate value
$hash->{READINGS}{value}{VAL} = $value; $hash->{READINGS}{value}{VAL} = $value;
$hash->{READINGS}{value}{TIME} = $tn; $hash->{READINGS}{value}{TIME} = $tn;
# reset counter # reset counter
my $ret = IOWrite($hash, "Z1"); my $ret = IOWrite($hash, "Z1");
if(!defined($ret)) { if(!defined($ret)) {
@ -137,14 +190,21 @@ sub
M232Counter_Set($@) M232Counter_Set($@)
{ {
my ($hash, @a) = @_; my ($hash, @a) = @_;
my $u = "Usage: set <name> value <value>"; my $u = "Usage: set <name> value <value>\n" .
"set <name> interval <seconds>\n" ;
return $u if(int(@a) != 3); return $u if(int(@a) != 3);
my $reading= $a[1]; my $reading= $a[1];
my $value = $a[2];
return $u unless($reading eq "value");
my $rm= M232Counter_Calibrate($hash, $value); if($a[1] eq "value") {
my $value= $a[2];
my $rm= M232Counter_Calibrate($hash, $value);
} elsif($a[1] eq "interval") {
my $interval= $a[2];
$hash->{INTERVAL}= $interval;
} else {
return $u;
}
return undef; return undef;
} }
@ -157,13 +217,18 @@ M232Counter_Define($$)
my ($hash, $def) = @_; my ($hash, $def) = @_;
my @a = split("[ \t][ \t]*", $def); my @a = split("[ \t][ \t]*", $def);
return "syntax: define <name> M232Counter [unit] [multiplicator]" return "syntax: define <name> M232Counter [unit] [factor] [deltaunit] [deltafactor]"
if(int(@a) < 2 && int(@a) > 4); if(int(@a) < 2 && int(@a) > 6);
my $unit= ((int(@a) > 2) ? $a[2] : "ticks"); my $unit= ((int(@a) > 2) ? $a[2] : "ticks");
my $factor= ((int(@a) > 3) ? $a[3] : 1.0); my $factor= ((int(@a) > 3) ? $a[3] : 1.0);
my $deltaunit= ((int(@a) > 4) ? $a[4] : "ticks per second");
my $deltafactor= ((int(@a) > 5) ? $a[5] : 1.0);
$hash->{UNIT}= $unit; $hash->{UNIT}= $unit;
$hash->{FACTOR}= $factor; $hash->{FACTOR}= $factor;
$hash->{DELTAUNIT}= $deltaunit;
$hash->{DELTAFACTOR}= $deltafactor;
$hash->{INTERVAL}= 60; # poll every minute per default
AssignIoPort($hash); AssignIoPort($hash);

View File

@ -388,3 +388,8 @@
e.g.: attr global logdir /var/tmp e.g.: attr global logdir /var/tmp
define emGaslog FileLog %ld/emGas.log emGas:.*CNT.* define emGaslog FileLog %ld/emGas.log emGas:.*CNT.*
- Sat Feb 15 2009 (Boris)
- added counter differential per time in 81_M232Counter.pm, commandref.html
updated

View File

@ -2302,25 +2302,36 @@ A line ending with \ will be concatenated with the next one, so long lines
<a name="M232Counterdefine"></a> <a name="M232Counterdefine"></a>
<b>Define</b> <b>Define</b>
<ul> <ul>
<code>define &lt;name&gt; M232Counter [unit [factor]]</code> <code>define &lt;name&gt; M232Counter [unit [factor [deltaunit [deltafactor]]]]</code>
<br><br> <br><br>
Define at most one M232Counter for a M232 device. Defining a M232Counter Define at most one M232Counter for a M232 device. Defining a M232Counter
will schedule an internal task, which reads the status of the counter every will schedule an internal task, which periodically reads the status of the
minute, and triggers notify/filelog commands. <code>unit</code> is the unit counter, and triggers notify/filelog commands. <code>unit</code> is the unit
name, <code>factor</code> is used to calculate the reading of the counter name, <code>factor</code> is used to calculate the reading of the counter
from number of ticks. <br><br> <br>Note: the unit defaults to the string from the number of ticks. <code>deltaunit</code> is the unit name of the counter
"ticks", but it must be specified if you wish to set the factor, which differential per second, <code>deltafactor</code> is used to calculate the
defaults to 1.0. In the second example below one tick equals 1/1250th kWh. counter differential per second from the number of ticks per second.<br><br>
Default values:
<ul>
<li>unit: ticks</li>
<li>factor: 1.0</li>
<li>deltaunit: ticks per second</li>
<li>deltafactor: 1.0</li>
</ul>
<br>Note: the parameters in square brackets are optional. If you wish to
specify an optional parameter, all preceding parameters must be specified
as well.
<br><br>Examples:
<ul>
<code>define counter M232Counter turns</code><br>
<code>define counter M232Counter kWh 0.0008 kW 2.88</code>
(one tick equals 1/1250th kWh)<br>
</ul>
<br>
Do not forget to start the counter (with <code>set .. start</code> for Do not forget to start the counter (with <code>set .. start</code> for
M232) or to start the counter and set the reading to a specified value M232) or to start the counter and set the reading to a specified value
(with <code>set ... value</code> for M232Counter). <br><br> (with <code>set ... value</code> for M232Counter). <br><br>
Example:
<ul>
<code>define counter M232Counter turns</code><br>
<code>define counter M232Counter kWh 0.0008</code><br>
</ul>
<br>
</ul> </ul>
<a name="M232Counterset"></a> <a name="M232Counterset"></a>
@ -2331,6 +2342,11 @@ A line ending with \ will be concatenated with the next one, so long lines
Sets the reading of the counter to the given value. The counter is reset Sets the reading of the counter to the given value. The counter is reset
and started and the offset is adjusted to value/unit. and started and the offset is adjusted to value/unit.
<br><br> <br><br>
<code>set &lt;name&gt; interval &lt;interval&gt;</code>
<br><br>
Sets the status polling interval in seconds to the given value. The default
is 60 seconds.
<br><br>
</ul> </ul>