98_statistics.pm 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. ##############################################
  2. # $Id: 98_statistics.pm 12218 2016-09-27 19:25:42Z grompo $
  3. #
  4. # 98_statistic.pm
  5. #
  6. # (c) 2014 Torsten Poitzsch
  7. # (c) 2014-2016 tupol http://forum.fhem.de/index.php?action=profile;u=5432
  8. #
  9. # This module computes statistic data of and for readings of other modules
  10. #
  11. # Copyright notice
  12. #
  13. # This script is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation; either version 2 of the License, or
  16. # (at your option) any later version.
  17. #
  18. # The GNU General Public License can be found at
  19. # http://www.gnu.org/copyleft/gpl.html.
  20. # A copy is found in the text file GPL.txt and important notices to the license
  21. # from the author is found in LICENSE.txt distributed with these scripts.
  22. #
  23. # This script is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU General Public License for more details.
  27. #
  28. # This copyright notice MUST APPEAR in all copies of the script!
  29. #
  30. ##############################################################################
  31. #
  32. # define <name> statistics <regexp>
  33. #
  34. ##############################################################################
  35. package main;
  36. use strict;
  37. use warnings;
  38. use Time::Local;
  39. sub statistics_PeriodChange($);
  40. sub statistics_DoStatisticsAll($$);
  41. sub statistics_DoStatistics ($$$);
  42. sub statistics_doStatisticMinMax ($$$$$);
  43. sub statistics_doStatisticMinMaxSingle ($$$$$$);
  44. sub statistics_doStatisticTendency ($$$);
  45. sub statistics_doStatisticDelta ($$$$);
  46. sub statistics_doStatisticDuration ($$$$);
  47. sub statistics_doStatisticDurationSingle ($$$$$$);
  48. sub statistics_storeSingularReadings ($$$$$$$$$$);
  49. sub statistics_getStoredDevices($);
  50. sub statistics_FormatDuration($);
  51. sub statistics_maxDecPlaces($$);
  52. sub statistics_UpdateDevReading($$$$);
  53. # Modul Version for remote debugging
  54. my $MODUL = "statistics";
  55. ##############################################################
  56. # Syntax: readingName => statisticType
  57. # statisticType: 0=noStatistic | 1=minMaxAvg(daily) | 2=delta | 3=stateDuration | 4=tendency | 5=minMaxAvg(hourly)
  58. ##############################################################
  59. my %knownReadings = (
  60. "brightness" => 1
  61. ,"count" => 2
  62. ,"current" => 1
  63. ,"energy" => 2
  64. ,"energy_current" => 1
  65. ,"energy_total" => 2
  66. ,"energyCalc" => 2
  67. ,"Total.Energy" => 2
  68. ,"humidity" => 1
  69. ,"lightsensor" => 3
  70. ,"lock" => 3
  71. ,"luminosity" => 1
  72. ,"motion" => 3
  73. ,"power" => 1
  74. ,"pressure" => 4
  75. ,"rain" => 2
  76. ,"rain_rate" => 1
  77. ,"rain_total" => 2
  78. ,"temperature" => 1
  79. ,"total" => 2
  80. ,"voltage" => 1
  81. ,"wind" => 5
  82. ,"wind_speed" => 5
  83. ,"windSpeed" => 5
  84. ,"Window" => 3
  85. ,"window" => 3
  86. );
  87. ##############################################################
  88. # Syntax: attributeName => statisticType
  89. # statisticType: 0=noStatistic | 1=minMaxAvg(daily) | 2=delta | 3=stateDuration | 4=tendency | 5=minMaxAvg(hourly)
  90. ##############################################################
  91. my %addedReadingsAttr = (
  92. "deltaReadings" => 2
  93. ,"durationReadings" => 3
  94. ,"minAvgMaxReadings" => 5
  95. ,"tendencyReadings" => 4
  96. );
  97. ##############################################################
  98. ##########################################
  99. sub statistics_Log($$$)
  100. {
  101. my ( $hash, $loglevel, $text ) = @_;
  102. my $xline = ( caller(0) )[2];
  103. my $xsubroutine = ( caller(1) )[3];
  104. my $sub = ( split( ':', $xsubroutine ) )[2];
  105. $sub =~ s/statistics_//;
  106. my $instName = ( ref($hash) eq "HASH" ) ? $hash->{NAME} : $hash;
  107. Log3 $instName, $loglevel, "$MODUL $instName: $sub.$xline " . $text;
  108. }
  109. ##########################################
  110. sub statistics_Initialize($)
  111. {
  112. my ($hash) = @_;
  113. $hash->{DefFn} = "statistics_Define";
  114. $hash->{UndefFn} = "statistics_Undefine";
  115. $hash->{NotifyFn} = "statistics_Notify";
  116. $hash->{NOTIFYDEV} = "global";
  117. $hash->{SetFn} = "statistics_Set";
  118. $hash->{NotifyOrderPrefix} = "10-"; # Want to be called before the rest
  119. $hash->{AttrList} = "disable:0,1 "
  120. ."dayChangeTime "
  121. ."deltaReadings "
  122. ."durationReadings "
  123. ."durationPeriodHour:0,1 "
  124. ."excludedReadings "
  125. ."ignoreDefaultAssignments:0,1 "
  126. ."minAvgMaxReadings "
  127. ."periodChangePreset "
  128. ."specialDeltaPeriodHours "
  129. ."specialDeltaPeriods "
  130. ."singularReadings "
  131. ."tendencyReadings "
  132. .$readingFnAttributes;
  133. }
  134. ##########################
  135. sub statistics_Define($$)
  136. {
  137. my ($hash, $def) = @_;
  138. my @a = split("[ \t][ \t]*", $def);
  139. return "Usage: define <name> statistics <devicename-regexp> [prefix]"
  140. if(@a<3 || @a>4);
  141. my $name = $a[0];
  142. my $devName = $a[2];
  143. if (@a == 4) {$hash->{PREFIX} = $a[3];}
  144. else {$hash->{PREFIX} = "stat";}
  145. eval { "Hallo" =~ m/^$devName$/ };
  146. return "Bad regexp: $@" if($@);
  147. $hash->{DEV_REGEXP} = $devName;
  148. $hash->{STATE} = "Waiting for notifications";
  149. $hash->{fhem}{modulVersion} = '$Date: 2016-09-27 21:25:42 +0200 (Tue, 27 Sep 2016) $';
  150. RemoveInternalTimer($hash);
  151. InternalTimer( gettimeofday() + 11, "statistics_PeriodChange", $hash, 0);
  152. $hash->{fhem}{nextPeriodChangeTime} = gettimeofday() + 11;
  153. return undef;
  154. }
  155. ########################################
  156. sub statistics_Undefine($$)
  157. {
  158. my ($hash, $arg) = @_;
  159. RemoveInternalTimer($hash);
  160. return undef;
  161. }
  162. ########################################
  163. sub statistics_Set($$@)
  164. {
  165. my ($hash, $name, $cmd, $val) = @_;
  166. my $resultStr = "";
  167. if ($cmd eq 'resetStatistics') {
  168. if ($val ne "") {
  169. Log3 $name, 3, "statistics: set $name $cmd $val";
  170. my $regExp;
  171. if ($val eq "all") { $regExp = ""; }
  172. else { $regExp = $val.":.*"; }
  173. foreach (sort keys %{ $hash->{READINGS} }) {
  174. if ($_ =~ /^\.$regExp/ && $_ ne "state") {
  175. delete $hash->{READINGS}{$_};
  176. $resultStr .= "\n * " . substr $_, 1;
  177. }
  178. }
  179. }
  180. if ( $resultStr eq "" ) {
  181. $resultStr = "$name: No statistics to reset";
  182. } else {
  183. $resultStr = "$name: Statistic value(s) reset:" . $resultStr;
  184. readingsSingleUpdate($hash,"state","Statistic value(s) reset: $val",1);
  185. }
  186. # statistics_Log $hash, 3, $resultStr;
  187. return $resultStr;
  188. } elsif ($cmd eq 'doStatistics') {
  189. Log3 $name, 3, "statistics: set $name $cmd";
  190. statistics_DoStatisticsAll($hash,0);
  191. return undef;
  192. }
  193. my $list = "resetStatistics:all" . statistics_getStoredDevices($hash);
  194. $list .= " doStatistics:noArg";
  195. return "Unknown argument $cmd, choose one of $list";
  196. }
  197. ########################################
  198. sub statistics_Notify($$)
  199. {
  200. my ($hash, $dev) = @_;
  201. my $name = $hash->{NAME};
  202. my $devName = $dev->{NAME};
  203. # At startup: delete old Readings of monitored devices and rebuild from hidden readings
  204. if ($devName eq "global" && grep (m/^INITIALIZED|REREADCFG$/,@{$dev->{CHANGED}})) {
  205. foreach my $r (keys %{$hash->{READINGS}}) {
  206. if ($r =~ /^monitoredDevices.*/) {
  207. statistics_Log $hash,5,"Initialization - Delete old reading '$r'.";
  208. delete($hash->{READINGS}{$r});
  209. }
  210. }
  211. my %unknownDevices;
  212. foreach my $r (keys %{$hash->{READINGS}}) {
  213. if ($r =~ /^\.(.*):.*/) { $unknownDevices{$1}++; }
  214. }
  215. my $val="";
  216. foreach my $device (sort (keys(%unknownDevices))) {
  217. if (not exists ($defs{$device})) {
  218. if ($val ne "") { $val.=","; }
  219. $val .= $device;
  220. }
  221. }
  222. if ($val ne "") {
  223. statistics_Log $hash, 4, "Initialization - Found hidden readings for device(s) '$val'.";
  224. readingsSingleUpdate($hash,"monitoredDevicesUnknown",$val,1);
  225. }
  226. return;
  227. }
  228. # Ignore my own notifications
  229. if($devName eq $name) {
  230. statistics_Log $hash, 5, "Notifications of myself received.";
  231. return "" ;
  232. }
  233. # Return if the notifying device is not monitored
  234. return "" if(!defined($hash->{DEV_REGEXP}));
  235. my $regexp = $hash->{DEV_REGEXP};
  236. if($devName !~ m/^($regexp)$/) {
  237. statistics_Log $hash, 5, "Notification of '".$dev->{NAME}."' received. Device not monitored.";
  238. return "" ;
  239. }
  240. # Check if it notifies only for the statistic values
  241. my $prefix = $hash->{PREFIX};
  242. my $normalReadingFound = 0;
  243. my $max = int(@{$dev->{CHANGED}});
  244. for (my $i = 0; $i < $max; $i++) {
  245. my $s = $dev->{CHANGED}[$i];
  246. next if(!defined($s));
  247. if ($s !~ /^$prefix[A-Z]/) { $normalReadingFound = 1;}
  248. }
  249. if ($normalReadingFound==1) {
  250. statistics_DoStatistics $hash, $dev, 0;
  251. statistics_Log $hash, 5, "Notification of '".$dev->{NAME}."' received. Update statistics.";
  252. } else {
  253. statistics_Log $hash, 5, "Notification of '".$dev->{NAME}."' received but for my own readings only.";
  254. }
  255. return;
  256. }
  257. ########################################
  258. sub statistics_PeriodChange($)
  259. {
  260. my ($hash) = @_;
  261. my $name = $hash->{NAME};
  262. my $dummy;
  263. my $val;
  264. my $curPeriodChangeTime = $hash->{fhem}{nextPeriodChangeTime};
  265. $curPeriodChangeTime = gettimeofday() unless defined $curPeriodChangeTime;
  266. my $periodChangePreset = AttrVal($name, "periodChangePreset", 5);
  267. my $isDayChange = ( ReadingsVal($name, "nextPeriodChangeCalc", "") =~ /Day Change/ );
  268. # Determine the next day change time
  269. my @th=localtime();
  270. my $dayChangeDelay = 0;
  271. my $dayChangeTime = timelocal(0,0,0,$th[3],$th[4],$th[5]+1900);
  272. if (AttrVal($name, "dayChangeTime", "00:00") =~ /(\d+):(\d+)/ && $1<24 && $1 >=0 && $2<60 && $2>=0) {
  273. $dayChangeDelay = $1 * 3600 + $2 * 60;
  274. if ($dayChangeDelay == 0) { $dayChangeTime += 24*3600; } # Otherwise it would always lay in the past
  275. $dayChangeTime += $dayChangeDelay - $periodChangePreset;
  276. }
  277. RemoveInternalTimer($hash);
  278. # Run period change procedure each full hour ("periodChangePreset" second before).
  279. my $periodEndTime = 3600 * ( int((gettimeofday()+$periodChangePreset)/3600) + 1 ) - $periodChangePreset ;
  280. # Run procedure also for given dayChangeTime
  281. $val = "";
  282. if ( gettimeofday()<$dayChangeTime && $dayChangeTime<=$periodEndTime ) {
  283. $periodEndTime = $dayChangeTime;
  284. $val = " (Day Change)";
  285. }
  286. $val = strftime ("%Y-%m-%d %H:%M:%S", localtime($periodEndTime)) . $val;
  287. InternalTimer( $periodEndTime, "statistics_PeriodChange", $hash, 1);
  288. readingsSingleUpdate($hash, "nextPeriodChangeCalc", $val, 0);
  289. $hash->{fhem}{nextPeriodChangeTime} = $periodEndTime;
  290. statistics_Log $hash, 4, "Next period change will be calculated at $val";
  291. return if( AttrVal($name, "disable", 0 ) == 1 );
  292. # Determine if time period switched (day, month, year)
  293. my $periodSwitch = 0;
  294. my $yearLast;
  295. my $monthLast;
  296. my $dayLast;
  297. my $hourLast;
  298. my $hourNow;
  299. my $dayNow;
  300. my $monthNow;
  301. my $yearNow;
  302. if ($isDayChange) {
  303. statistics_Log $hash, 4, "Calculating day change";
  304. # get time values for 50 seconds before a new day
  305. ($dummy, $dummy, $hourLast, $dayLast, $monthLast, $yearLast) = localtime ($curPeriodChangeTime - $dayChangeDelay + $periodChangePreset - 59);
  306. # get time values for next day
  307. ($dummy, $dummy, $hourNow, $dayNow, $monthNow, $yearNow) = localtime (gettimeofday() + $periodChangePreset);
  308. $periodSwitch = -2;
  309. if ($yearNow != $yearLast) { $periodSwitch = -4; }
  310. elsif ($monthNow != $monthLast) { $periodSwitch = -3; }
  311. # Positiv Value if periode changes at full hour
  312. $periodSwitch = abs($periodSwitch) if $dayChangeDelay % 3600 == 0;
  313. } else {
  314. ($dummy, $dummy, $hourLast, $dummy, $dummy, $dummy) = localtime (gettimeofday());
  315. ($dummy, $dummy, $hourNow, $dummy, $dummy, $dummy) = localtime (gettimeofday() + $periodChangePreset);
  316. if ($hourNow != $hourLast) {
  317. $periodSwitch = 1;
  318. statistics_Log $hash,4,"Calculating hour change";
  319. } else {
  320. statistics_Log $hash,4,"Calculating statistics at startup";
  321. }
  322. }
  323. statistics_DoStatisticsAll $hash, $periodSwitch;
  324. return undef;
  325. }
  326. ##########################
  327. # Take each notified reading and perform the calculation
  328. sub statistics_DoStatisticsAll($$)
  329. {
  330. my ($hash,$periodSwitch) = @_;
  331. my $name = $hash->{NAME};
  332. return "" if(!defined($hash->{DEV_REGEXP}));
  333. my $regexp = $hash->{DEV_REGEXP};
  334. foreach my $devName (sort keys %defs) {
  335. if ($devName ne $name && $devName =~ m/^($regexp)$/) {
  336. statistics_Log $hash,4,"Doing statistics (period $periodSwitch) for device '$devName'";
  337. statistics_DoStatistics($hash, $defs{$devName}, $periodSwitch);
  338. }
  339. }
  340. # To avoid data loose, store all readings once a day in the state file
  341. if ($periodSwitch < -1 || $periodSwitch > 1 ) { WriteStatefile(); }
  342. }
  343. ##########################
  344. sub statistics_DoStatistics($$$)
  345. {
  346. my ($hash, $dev, $periodSwitch) = @_;
  347. my $hashName = $hash->{NAME};
  348. my $devName = $dev->{NAME};
  349. my $devType = $dev->{TYPE};
  350. my $statisticDone = 0;
  351. my %statReadings = ();
  352. return if( AttrVal($hashName, "disable", 0 ) == 1 );
  353. my $ignoreDefAssign = AttrVal($hashName, "ignoreDefaultAssignments", 0);
  354. my $exclReadings = AttrVal($hashName, "excludedReadings", "");
  355. my $regExp = '^'.$devName.'$|^'.$devName.',|,'.$devName.'$|,'.$devName.',';
  356. # Return if the notifying device is already served by another statistics instance with same prefix
  357. my $instanceMarker = "_98_statistics_".$hash->{PREFIX};
  358. if (exists ($dev->{helper}{$instanceMarker})) {
  359. my $servedBy = $dev->{helper}{$instanceMarker};
  360. if ($servedBy ne $hashName) {
  361. my $monReadingValue = ReadingsVal($hashName,"monitoredDevicesUnserved","");
  362. if ($monReadingValue !~ /$regExp/) {
  363. if($monReadingValue eq "") { $monReadingValue = $devName;}
  364. else {$monReadingValue .= ",".$devName;}
  365. readingsSingleUpdate($hash,"monitoredDevicesUnserved",$monReadingValue,1);
  366. statistics_Log $hash, 3, "Device '$devName' identified as supported but already servered by '$servedBy' with some reading prefix.";
  367. }
  368. return;
  369. }
  370. } else {
  371. $dev->{helper}{_98_statistics}=$hashName;
  372. }
  373. # Build up Statistic-Reading-Hash, add readings defined in attributes to Statistic-Reading-Hash
  374. %statReadings = %knownReadings
  375. unless $ignoreDefAssign == 1;
  376. while (my ($aName, $statType) = each (%addedReadingsAttr) )
  377. {
  378. my @addedReadings = split /,/, AttrVal($hashName, $aName, "");
  379. foreach( @addedReadings )
  380. {
  381. statistics_Log $hash, 5, "Assigned reading '$_' from attribute '$aName' to statistic type $statType.";
  382. $statReadings{$_} = $statType;
  383. }
  384. }
  385. readingsBeginUpdate($dev);
  386. # Loop through Statistic-Reading-Hash and start statistic calculation if the readings exists in the notifying device
  387. while ( my ($rName, $statType) = each (%statReadings) )
  388. {
  389. # notifing device has known reading, no statistic for excluded readings
  390. my $completeReadingName = $devName.":".$rName;
  391. next if ($completeReadingName =~ m/^($exclReadings)$/ );
  392. next if not exists ($dev->{READINGS}{$rName});
  393. if ($statType == 1) { statistics_doStatisticMinMax ( $hash, $dev, $rName, $periodSwitch, 0 );}
  394. elsif ($statType == 2) { statistics_doStatisticDelta ( $hash, $dev, $rName, $periodSwitch );}
  395. elsif ($statType == 3) { statistics_doStatisticDuration ( $hash, $dev, $rName, $periodSwitch ); }
  396. elsif ($statType == 4 && $periodSwitch>=1) { statistics_doStatisticTendency ( $hash, $dev, $rName );}
  397. elsif ($statType == 5) { statistics_doStatisticMinMax ( $hash, $dev, $rName, $periodSwitch, 1 );}
  398. $statisticDone = 1;
  399. }
  400. # If no statistic-reading has been found, do a duration stat for the device-state
  401. if ($statisticDone != 1 && $ignoreDefAssign != 1)
  402. {
  403. if ( exists ($dev->{READINGS}{state}) && $dev->{READINGS}{state}{VAL} ne "defined" ) {
  404. statistics_doStatisticDuration $hash, $dev, "state", $periodSwitch;
  405. $statisticDone = 1;
  406. }
  407. }
  408. if ($periodSwitch >0) {readingsEndUpdate($dev,1);}
  409. else {readingsEndUpdate($dev,0);}
  410. # Record device as monitored
  411. my $monReadingName;
  412. if ($statisticDone == 1) {
  413. $monReadingName = "monitoredDevices".$devType;
  414. readingsSingleUpdate($hash,"state","Updated stats for: $devName",1);
  415. } else {
  416. $monReadingName = "monitoredDevicesUnsupported";
  417. $devName .= "#$devType";
  418. $regExp = '^'.$devName.'$|^'.$devName.',|,'.$devName.'$|,'.$devName.',';
  419. }
  420. my $monReadingValue = ReadingsVal($hashName,$monReadingName,"");
  421. if ($monReadingValue !~ /$regExp/) {
  422. if($monReadingValue eq "") { $monReadingValue = $devName;}
  423. else {$monReadingValue .= ",".$devName;}
  424. readingsSingleUpdate($hash,$monReadingName,$monReadingValue,1);
  425. my $monReadingValue = ReadingsVal($hashName,"monitoredDevicesUnknownType","");
  426. if ($monReadingValue =~ /$regExp/) {
  427. $monReadingValue =~ s/$devName,?//;
  428. $monReadingValue =~ s/,$//;
  429. if ($monReadingValue ne "") {
  430. readingsSingleUpdate($hash,"monitoredDevicesUnknownType",$monReadingValue,1);
  431. } else {
  432. delete $hash->{READINGS}{monitoredDevicesUnknownType};
  433. }
  434. }
  435. }
  436. return undef;
  437. }
  438. # Calculates Min/Average/Max Values
  439. ########################################
  440. sub statistics_doStatisticMinMax ($$$$$)
  441. {
  442. my ($hash, $dev, $readingName, $periodSwitch, $doHourly) = @_;
  443. my $name = $hash->{NAME};
  444. my $devName = $dev->{NAME};
  445. return if not exists ($dev->{READINGS}{$readingName});
  446. # Get reading, cut out first number without units
  447. my $value = $dev->{READINGS}{$readingName}{VAL};
  448. $value =~ s/\s*(-?[\d.]*).*/$1/e;
  449. statistics_Log $hash, 4, "Calculating min/avg/max statistics for '".$dev->{NAME}.":$readingName = $value'";
  450. # statistics_doStatisticMinMaxSingle: $hash, $readingName, $value, $saveLast
  451. # Hourly statistic (if needed)
  452. if ($doHourly) { statistics_doStatisticMinMaxSingle $hash, $dev, $readingName, "Hour", $value, ($periodSwitch != 0); }
  453. # Daily statistic
  454. statistics_doStatisticMinMaxSingle $hash, $dev, $readingName, "Day", $value, ( $periodSwitch >= 2 || $periodSwitch <= -2 );
  455. # Monthly statistic
  456. statistics_doStatisticMinMaxSingle $hash, $dev, $readingName, "Month", $value, ( $periodSwitch >= 3 || $periodSwitch <= -3 );
  457. # Yearly statistic
  458. statistics_doStatisticMinMaxSingle $hash, $dev, $readingName, "Year", $value, ( $periodSwitch == 4 || $periodSwitch == -4 );
  459. return ;
  460. }
  461. # Calculates single MaxMin Values and informs about end of day and month
  462. ########################################
  463. sub statistics_doStatisticMinMaxSingle ($$$$$$)
  464. {
  465. my ($hash, $dev, $readingName, $period, $value, $saveLast) = @_;
  466. my $result;
  467. my $hiddenReadingName = ".".$dev->{NAME}.":".$readingName.$period;
  468. my $name=$hash->{NAME};
  469. my $devName = $dev->{NAME};
  470. my $statReadingName = $hash->{PREFIX};
  471. $statReadingName .= ucfirst($readingName).$period;
  472. my @hidden;
  473. my @stat;
  474. my $lastValue;
  475. my $firstRun = not exists($hash->{READINGS}{$hiddenReadingName});
  476. if ( $firstRun ) {
  477. # Show since-Value
  478. $hidden[1] = 0; $hidden[3] = 0; $hidden[9] = 1;
  479. $stat[1] = $value; $stat[3] = $value; $stat[5] = $value;
  480. $stat[7] = strftime ("%Y-%m-%d_%H:%M:%S",localtime() );
  481. }
  482. else {
  483. # Do calculations if hidden reading exists
  484. @hidden = split / /, $hash->{READINGS}{$hiddenReadingName}{VAL}; # Internal values
  485. @stat = split / /, $dev->{READINGS}{$statReadingName}{VAL};
  486. my $timeDiff = int(gettimeofday())-$hidden[7];
  487. $hidden[1] += $hidden[5] * $timeDiff; # sum
  488. $hidden[3] += $timeDiff; # time
  489. if ($value < $stat[1]) { $stat[1]=$value; } # Min
  490. if ($hidden[3]>0) {$stat[3] = $hidden[1] / $hidden[3];} # Avg
  491. if ($value > $stat[5]) { $stat[5]=$value; } # Max
  492. }
  493. my $decPlaces = statistics_maxDecPlaces($value, $hidden[11]);
  494. # Prepare new current reading
  495. $result = sprintf( "Min: %.".$decPlaces."f Avg: %.".$decPlaces."f Max: %.".$decPlaces."f", $stat[1], $stat[3], $stat[5]);
  496. if ($hidden[9] == 1) { $result .= " (since: $stat[7] )"; }
  497. # Store current reading as last reading, Reset current reading
  498. if ($saveLast) {
  499. readingsBulkUpdate($dev, $statReadingName . "Last", $result, 1);
  500. statistics_Log $hash, 4, "Set '".$statReadingName . "Last'='$result'";
  501. $hidden[1] = 0; $hidden[3] = 0; $hidden[9] = 0; # No since value anymore
  502. $result = "Min: $value Avg: $value Max: $value";
  503. }
  504. # Store current reading
  505. readingsBulkUpdate($dev, $statReadingName, $result, 0);
  506. statistics_Log $hash, 5, "Set '$statReadingName'='$result'";
  507. # Store single readings
  508. my $singularReadings = AttrVal($name, "singularReadings", "");
  509. if ($singularReadings ne "") {
  510. # statistics_storeSingularReadings $hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast
  511. my $statValue = sprintf "%.".$decPlaces."f", $stat[1];
  512. if ($saveLast) { $lastValue = $statValue; $statValue = $value; }
  513. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Min",$period,$statValue,$lastValue,$saveLast);
  514. $statValue = sprintf "%.".$decPlaces."f", $stat[3];
  515. if ($saveLast) { $lastValue = $statValue; $statValue = $value; }
  516. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Avg",$period,$statValue,$lastValue,$saveLast);
  517. $statValue = sprintf "%.".$decPlaces."f", $stat[5];
  518. if ($saveLast) { $lastValue = $statValue; $statValue = $value; }
  519. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Max",$period,$statValue,$lastValue,$saveLast);
  520. }
  521. # Store hidden reading
  522. $result = "Sum: $hidden[1] Time: $hidden[3] LastValue: ".$value." LastTime: ".int(gettimeofday())." ShowDate: $hidden[9] DecPlaces: $decPlaces";
  523. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  524. statistics_Log $hash, 5, "Set '$hiddenReadingName'='$result'";
  525. return;
  526. }
  527. # Calculates tendency values
  528. ########################################
  529. sub statistics_doStatisticTendency ($$$)
  530. {
  531. my ($hash, $dev, $readingName) = @_;
  532. my $name = $hash->{NAME};
  533. my $devName = $dev->{NAME};
  534. my $decPlaces = 0;
  535. return if not exists ($dev->{READINGS}{$readingName});
  536. # Get reading, cut out first number without units
  537. my $value = $dev->{READINGS}{$readingName}{VAL};
  538. $value =~ s/\s*(-?[\d.]*).*/$1/e;
  539. statistics_Log $hash, 4, "Calculating hourly tendency statistics for '".$dev->{NAME}.":$readingName = $value'";
  540. my $statReadingName = $hash->{PREFIX} . ucfirst($readingName) . "Tendency";
  541. my $hiddenReadingName = ".".$dev->{NAME}.":".$readingName."Tendency";
  542. my @hidden; my @stat;
  543. my $firstRun = not exists($hash->{READINGS}{$hiddenReadingName});
  544. if ( $firstRun ) {
  545. @stat = split / /, "1h: - 2h: - 3h: - 6h: -";
  546. statistics_Log $hash,4,"Initializing statistic of '$hiddenReadingName'.";
  547. $hash->{READINGS}{$hiddenReadingName}{VAL} = "";
  548. }
  549. else {
  550. @stat = split / /, $dev->{READINGS}{$statReadingName}{VAL};
  551. }
  552. statistics_Log $hash, 4, "Add $value to $hiddenReadingName";
  553. my $result = $value;
  554. if (exists ($hash->{READINGS}{$hiddenReadingName}{VAL})) { $result .= " " . $hash->{READINGS}{$hiddenReadingName}{VAL}; }
  555. @hidden = split / /, $result; # Internal values
  556. # determine decPlaces with stored values
  557. foreach (@hidden) {
  558. $decPlaces = statistics_maxDecPlaces($_, $decPlaces);
  559. }
  560. if ( exists($hidden[7]) ) {
  561. statistics_Log $hash, 4, "Remove last value ".$hidden[7]." from '$hiddenReadingName'";
  562. delete $hidden[7];
  563. }
  564. if ( exists($hidden[1]) ) {$stat[1] = sprintf "%+.".$decPlaces."f", $value-$hidden[1];}
  565. if ( exists($hidden[2]) ) {$stat[3] = sprintf "%+.".$decPlaces."f", $value-$hidden[2];}
  566. if ( exists($hidden[3]) ) {$stat[5] = sprintf "%+.".$decPlaces."f", $value-$hidden[3];}
  567. if ( exists($hidden[6]) ) {$stat[7] = sprintf "%+.".$decPlaces."f", $value-$hidden[6];}
  568. $result = "1h: " . $stat[1] ." 2h: ". $stat[3] ." 3h: ". $stat[5] ." 6h: ". $stat[7];
  569. readingsBulkUpdate($dev, $statReadingName, $result, 1);
  570. # Store single readings
  571. my $singularReadings = AttrVal($name, "singularReadings", "");
  572. if ($singularReadings ne "") {
  573. # statistics_storeSingularReadings $hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast
  574. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Tendency","1h",$stat[1],0,0);
  575. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Tendency","2h",$stat[3],0,0);
  576. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Tendency","3h",$stat[5],0,0);
  577. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Tendency","6h",$stat[7],0,0);
  578. }
  579. $result = join( " ", @hidden );
  580. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  581. statistics_Log $hash, 4, "Set '$hiddenReadingName = $result'";
  582. return ;
  583. }
  584. # Calculates deltas for day, month and year
  585. ########################################
  586. sub statistics_doStatisticDelta ($$$$)
  587. {
  588. my ($hash, $dev, $readingName, $periodSwitch) = @_;
  589. my $dummy;
  590. my $result;
  591. my $showDate;
  592. my $name = $hash->{NAME};
  593. my $decPlaces = 0;
  594. return if not exists ($dev->{READINGS}{$readingName});
  595. # Get reading, extract first number without units
  596. my $value = $dev->{READINGS}{$readingName}{VAL};
  597. $value =~ s/\s*(-?[\d.]*).*/$1/e;
  598. statistics_Log $hash, 4, "Calculating delta statistics for '".$dev->{NAME}.":$readingName = $value'";
  599. my $hiddenReadingName = ".".$dev->{NAME}.":".$readingName;
  600. my $statReadingName = $hash->{PREFIX};
  601. $statReadingName .= ucfirst($readingName);
  602. my @hidden; my @stat; my @last;
  603. my $firstRun = not exists($hash->{READINGS}{$hiddenReadingName});
  604. # Show since-Value and initialize all readings
  605. if ( $firstRun ) {
  606. $showDate = 8;
  607. @stat = split / /, "Hour: 0 Day: 0 Month: 0 Year: 0";
  608. $stat[9] = strftime "%Y-%m-%d_%H:%M:%S", localtime();
  609. @last = split / /, "Hour: - Day: - Month: - Year: -";
  610. statistics_Log $hash, 4, "Initializing statistic of '$hiddenReadingName'.";
  611. }
  612. # Do calculations if hidden reading exists
  613. else {
  614. @stat = split / /, $dev->{READINGS}{$statReadingName}{VAL};
  615. @hidden = split / /, $hash->{READINGS}{$hiddenReadingName}{VAL}; # Internal values
  616. $showDate = $hidden[3];
  617. $decPlaces = statistics_maxDecPlaces($value, $hidden[5]);
  618. if (exists ($dev->{READINGS}{$statReadingName."Last"})) {
  619. @last = split / /, $dev->{READINGS}{$statReadingName."Last"}{VAL};
  620. }
  621. else {
  622. @last = split / /, "Hour: - Day: - Month: - Year: -";
  623. }
  624. my $deltaValue = $value - $hidden[1];
  625. # Do statistic
  626. $stat[1] += $deltaValue;
  627. $stat[3] += $deltaValue;
  628. $stat[5] += $deltaValue;
  629. $stat[7] += $deltaValue;
  630. if ($periodSwitch>=1) { statistics_doStatisticSpecialPeriod ( $hash, $dev, $readingName, $decPlaces, $stat[1] ); }
  631. # Determine if "since" value has to be shown in current and last reading
  632. # If change of year, change yearly statistic
  633. if ($periodSwitch == 4 || $periodSwitch == -4) {
  634. $last[7] = sprintf "%.".$decPlaces."f", $stat[7];
  635. $stat[7] = 0;
  636. if ($showDate == 1) { $showDate = 0; } # Do not show the "since:" value for year changes anymore
  637. if ($showDate >= 2) { $showDate = 1; $last[9] = $stat[9]; } # Shows the "since:" value for the first year change
  638. statistics_Log $hash, 4, "Shifting current year in last value of '$statReadingName'.";
  639. }
  640. # If change of month, change monthly statistic and check for special periods
  641. if ($periodSwitch >= 3 || $periodSwitch <= -3){
  642. statistics_doStatisticSpecialPeriod2 ($hash, $dev, $readingName, "Month", $decPlaces, $stat[5]);
  643. $last[5] = sprintf "%.".$decPlaces."f", $stat[5];
  644. $stat[5] = 0;
  645. if ($showDate == 3) { $showDate = 2; } # Do not show the "since:" value for month changes anymore
  646. if ($showDate >= 4) { $showDate = 3; $last[9] = $stat[9]; } # Shows the "since:" value for the first month change
  647. statistics_Log $hash, 4, "Shifting current month in last value of '$statReadingName'.";
  648. }
  649. # If change of day, change daily statistic and check for special periods
  650. if ($periodSwitch >= 2 || $periodSwitch <= -2){
  651. statistics_doStatisticSpecialPeriod2 ($hash, $dev, $readingName, "Day", $decPlaces, $stat[3]);
  652. $last[3] = sprintf "%.".$decPlaces."f", $stat[3];
  653. $stat[3] = 0;
  654. if ($showDate == 5) { $showDate = 4; } # Do not show the "since:" value for day changes anymore
  655. if ($showDate >= 6) { # Shows the "since:" value for the first day change
  656. $showDate = 5;
  657. $last[9] = $stat[9];
  658. # Next monthly and yearly values start normaly at 00:00 and show only date (no time)
  659. if (AttrVal($name, "dayChangeTime", "00:00") =~ /00:00|0:00/) {
  660. my $periodChangePreset = AttrVal($name, "periodChangePreset", 5);
  661. $stat[5] = 0;
  662. $stat[7] = 0;
  663. $stat[9] = strftime "%Y-%m-%d", localtime(gettimeofday()+$periodChangePreset); # start
  664. }
  665. }
  666. statistics_Log $hash,4,"Shifting current day in last value of '$statReadingName'.";
  667. }
  668. # If change of hour, change hourly statistic and check for special periods
  669. if ($periodSwitch >= 1){
  670. statistics_doStatisticSpecialPeriod2 ($hash, $dev, $readingName, "Hour", $decPlaces, $stat[1]);
  671. $last[1] = sprintf "%.".$decPlaces."f", $stat[1];
  672. $stat[1] = 0;
  673. if ($showDate == 7) { $showDate = 6; } # Do not show the "since:" value for day changes anymore
  674. if ($showDate >= 8) { $showDate = 7; $last[9] = $stat[9]; } # Shows the "since:" value for the first hour change
  675. statistics_Log $hash, 4, "Shifting current hour in last value of '$statReadingName'.";
  676. }
  677. }
  678. # Store hidden reading
  679. $result = "LastValue: $value ShowDate: $showDate DecPlaces: $decPlaces";
  680. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  681. statistics_Log $hash, 5, "Set '$hiddenReadingName'='$result'";
  682. # Store visible statistic readings (delta values)
  683. $result = sprintf "Hour: %.".$decPlaces."f Day: %.".$decPlaces."f Month: %.".$decPlaces."f Year: %.".$decPlaces."f", $stat[1], $stat[3], $stat[5], $stat[7];
  684. if ( $showDate >=2 ) { $result .= " (since: $stat[9] )"; }
  685. readingsBulkUpdate($dev,$statReadingName,$result, 1);
  686. statistics_Log $hash, 5, "Set '$statReadingName'='$result'";
  687. # if changed, store previous visible statistic (delta) values
  688. if ($periodSwitch != 0) {
  689. $result = "Hour: $last[1] Day: $last[3] Month: $last[5] Year: $last[7]";
  690. if ( $showDate =~ /1|3|5|7/ ) { $result .= " (since: $last[9] )"; }
  691. readingsBulkUpdate($dev,$statReadingName."Last",$result, 1);
  692. statistics_Log $hash, 4, "Set '".$statReadingName."Last'='$result'";
  693. }
  694. # Store single readings
  695. my $singularReadings = AttrVal($name, "singularReadings", "");
  696. if ($singularReadings ne "") {
  697. # statistics_storeSingularReadings $hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast
  698. my $statValue = sprintf "%.".$decPlaces."f", $stat[1];
  699. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName, $readingName, "Delta", "Hour", $statValue, $last[1], $periodSwitch >= 1);
  700. $statValue = sprintf "%.".$decPlaces."f", $stat[3];
  701. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName, $readingName, "Delta", "Day", $statValue, $last[3], $periodSwitch >= 2 || $periodSwitch <= -2);
  702. $statValue = sprintf "%.".$decPlaces."f", $stat[5];
  703. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName, $readingName, "Delta", "Month", $statValue, $last[5], $periodSwitch >= 3 || $periodSwitch <= -3);
  704. $statValue = sprintf "%.".$decPlaces."f", $stat[7];
  705. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName, $readingName, "Delta", "Year", $statValue, $last[7], $periodSwitch == 4 || $periodSwitch == -4);
  706. }
  707. return ;
  708. }
  709. # Calculates deltas for period of several hours
  710. ########################################
  711. sub statistics_doStatisticSpecialPeriod ($$$$$)
  712. {
  713. my ($hash, $dev, $readingName, $decPlaces, $value) = @_;
  714. my $name = $hash->{NAME};
  715. my $specialPeriod = AttrVal($name, "specialDeltaPeriodHours", 0);
  716. return if $specialPeriod == 0;
  717. my $statReadingName = $hash->{PREFIX} . ucfirst($readingName) . "SpecialPeriod";
  718. my $hiddenReadingName = ".".$dev->{NAME} . ":" . $readingName . "SpecialPeriod";
  719. # Update hidden stack
  720. my @hidden = ();
  721. if (exists ($hash->{READINGS}{$hiddenReadingName}{VAL}))
  722. { @hidden = split / /, $hash->{READINGS}{$hiddenReadingName}{VAL}; }
  723. unshift @hidden, $value;
  724. statistics_Log $hash, 4, "Add $value to $hiddenReadingName";
  725. while ( $#hidden >= $specialPeriod ) {
  726. my $lastValue = pop @hidden;
  727. statistics_Log $hash, 4, "Remove last (".$#hidden.") value '$lastValue' from '$hiddenReadingName'";
  728. }
  729. # Calculate specialPeriodValue
  730. my $result = 0;
  731. foreach (@hidden) { $result += $_; }
  732. $result = sprintf "%.".$decPlaces."f", $result;
  733. $result .= " (".($#hidden+1).".hours)" if $#hidden != $specialPeriod-1;
  734. statistics_Log $hash, 4, "Set '$statReadingName = $result'";
  735. readingsBulkUpdate($dev, $statReadingName, $result, 1);
  736. # Store hidden stack
  737. $result = join( " ", @hidden );
  738. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  739. statistics_Log $hash, 4, "Set '$hiddenReadingName = $result'";
  740. }
  741. # Calculates deltas for period of several hours
  742. ########################################
  743. sub statistics_doStatisticSpecialPeriod2 ($$$$$$)
  744. {
  745. my ($hash, $dev, $readingName, $periodText, $decPlaces, $value) = @_;
  746. my $name = $hash->{NAME};
  747. my $pattern;
  748. my $specialDeltaPeriods = AttrVal($name, "specialDeltaPeriods", "");
  749. return unless $specialDeltaPeriods;
  750. #Check if reading occures in attribute specialDeltaPeriods, extract period numbers
  751. my $devName = $dev->{NAME};
  752. $pattern = $devName . ":" . $readingName . ":" . $periodText . ':([\d:]+)';
  753. return unless $specialDeltaPeriods =~ $pattern;
  754. #Determine number of periods and maximal number
  755. my @periods = split /:/, $1;
  756. my $maxNumber=0;
  757. @periods = sort { $a <=> $b } @periods;
  758. $maxNumber = $periods[$#periods];
  759. #Determine reading names
  760. my $statReadingName = $hash->{PREFIX} . ucfirst($readingName) . ucfirst($periodText);
  761. my $hiddenReadingName = ".".$devName . ":" . $readingName . $periodText;
  762. # Update hidden stack
  763. my @hidden = ();
  764. if (exists ($hash->{READINGS}{$hiddenReadingName}{VAL}))
  765. { @hidden = split / /, $hash->{READINGS}{$hiddenReadingName}{VAL}; }
  766. unshift @hidden, $value;
  767. statistics_Log $hash, 4, "Add $value to $hiddenReadingName";
  768. while ( $#hidden > $maxNumber ) {
  769. my $lastValue = pop @hidden;
  770. statistics_Log $hash, 4, "Remove last value '$lastValue' from '$hiddenReadingName'";
  771. }
  772. #Calculate and write specialPeriodValues
  773. my $result = 0;
  774. my $counterHidden = 0;
  775. my $counterPeriods = 0;
  776. foreach (@hidden) {
  777. $result += $_;
  778. $counterHidden++;
  779. if ( $counterHidden == $periods[$counterPeriods] ) {
  780. my $rName = $statReadingName . $periods[$counterPeriods];
  781. my $rValue = sprintf "%.".$decPlaces."f", $result;
  782. statistics_Log $hash, 4, "Set '$rName = $rValue'";
  783. readingsBulkUpdate($dev, $rName, $rValue, 1);
  784. $counterPeriods++;
  785. }
  786. }
  787. #write specialPeriodValues for periods not reached yet
  788. for (my $i = $counterPeriods; $i <= $#periods; $i++) {
  789. my $rName = $statReadingName . $periods[$i];
  790. my $rValue = sprintf "%.".$decPlaces."f (%d %s)", $result, $#hidden+1, $periodText;
  791. statistics_Log $hash, 4, "Set '$rName = $rValue'";
  792. readingsBulkUpdate($dev, $rName, $rValue, 1);
  793. }
  794. # Store hidden stack
  795. $result = join( " ", @hidden );
  796. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  797. statistics_Log $hash, 4, "Set '$hiddenReadingName = $result'";
  798. }
  799. # Calculates single Duration Values and informs about end of day and month
  800. ########################################
  801. sub statistics_doStatisticDuration ($$$$)
  802. {
  803. my ($hash, $dev, $readingName, $periodSwitch) = @_;
  804. my $name = $hash->{NAME};
  805. my $devName = $dev->{NAME};
  806. return if not exists ($dev->{READINGS}{$readingName});
  807. # Get reading
  808. my $state = $dev->{READINGS}{$readingName}{VAL};
  809. $state = "(null)" if $state eq "";
  810. statistics_Log $hash, 4, "Calculating duration statistics for '".$dev->{NAME}.":$readingName = $state'";
  811. # Daily Statistic
  812. statistics_doStatisticDurationSingle $hash, $dev, $readingName, "Hour", $state, ($periodSwitch !=0)
  813. if AttrVal($name, "durationPeriodHour", 0) == 1;
  814. # Daily Statistic
  815. statistics_doStatisticDurationSingle $hash, $dev, $readingName, "Day", $state, ($periodSwitch >= 2 || $periodSwitch <= -2);
  816. # Monthly Statistic
  817. statistics_doStatisticDurationSingle $hash, $dev, $readingName, "Month", $state, ($periodSwitch >= 3 || $periodSwitch <= -3);
  818. # Yearly Statistic
  819. statistics_doStatisticDurationSingle $hash, $dev, $readingName, "Year", $state, ($periodSwitch == 4 || $periodSwitch == -4);
  820. return ;
  821. }
  822. # Calculates single duration values
  823. ########################################
  824. sub statistics_doStatisticDurationSingle ($$$$$$)
  825. {
  826. my ($hash, $dev, $readingName, $period, $state, $saveLast) = @_;
  827. my $result;
  828. my $hiddenReadingName = ".".$dev->{NAME}.":".$readingName.$period;
  829. my $name=$hash->{NAME};
  830. my $devName = $dev->{NAME};
  831. $state =~ s/ /_/g;
  832. $state = "on" if $state =~ /set_on/;
  833. $state = "off" if $state =~ /set_off/;
  834. my $statReadingName = $hash->{PREFIX};
  835. $statReadingName .= ucfirst($readingName).$period;
  836. my %hidden;
  837. my %stat;
  838. my $firstRun = not exists($hash->{READINGS}{$hiddenReadingName});
  839. my $lastState;
  840. # Show since-Value
  841. if ( $firstRun ) {
  842. $hidden{"showDate:"} = 1;
  843. $saveLast = 0;
  844. $lastState = $state;
  845. $hidden{"(since:"} = strftime ("%Y-%m-%d_%H:%M:%S)",localtime() );
  846. $hidden{$state.":"} = 0;
  847. $hidden{$state."_Count:"} = 1;
  848. }
  849. # Do calculations if hidden reading exists
  850. else {
  851. %hidden = split / /, $hash->{READINGS}{$hiddenReadingName}{VAL}; # Internal values
  852. $lastState = $hidden{"lastState:"};
  853. my $timeDiff = int(gettimeofday())-$hidden{"lastTime:"};
  854. $hidden{$lastState.":"} += $timeDiff;
  855. $hidden{$state."_Count:"}++
  856. if $state ne $lastState;
  857. }
  858. $hidden{"lastState:"} = $state;
  859. $hidden{"lastTime:"} = int(gettimeofday());
  860. # Prepare new current reading, delete hidden reading if it is used again
  861. $result = "";
  862. foreach my $key (sort keys %hidden) {
  863. if ($key !~ /^(lastState|lastTime|showDate|\(since):$/) {
  864. #Store current value for single readings
  865. if ($key !~ /_Count:$/) {
  866. $stat{$key} = statistics_FormatDuration($hidden{$key});
  867. }
  868. else {
  869. $stat{$key} = $hidden{$key};
  870. }
  871. # Create current summary reading
  872. $result .= " " if $result;
  873. $result .= "$key ".$stat{$key};
  874. delete $hidden{$key} if $saveLast; # Reset hidden reading if period changed
  875. }
  876. }
  877. if ($hidden{"showDate:"} == 1) { $result .= " (since: ".$hidden{"(since:"}; }
  878. # Store current summary reading as last reading, create new summary reading
  879. if ($saveLast) {
  880. readingsBulkUpdate($dev, $statReadingName . "Last", $result, 1);
  881. statistics_Log $hash, 4, "Set '".$statReadingName . "Last = $result'";
  882. $result = $state.": 00:00:00 ".$state."_Count: 1";
  883. $hidden{$state.":"} = 0;
  884. $hidden{$state."_Count:"} = 1;
  885. $hidden{"showDate:"} = 0;
  886. }
  887. # Store current reading
  888. readingsBulkUpdate($dev, $statReadingName, $result, 0);
  889. statistics_Log $hash, 5, "Set '$statReadingName = $result'";
  890. # Store single readings
  891. my $singularReadings = AttrVal($name, "singularReadings", "");
  892. if ($singularReadings ne "") {
  893. # Do this for each state of the current or last period
  894. while (my ($statKey, $statValue) = each(%stat) ) {
  895. chop ($statKey);
  896. unless ($saveLast) {
  897. # statistics_storeSingularReadings
  898. # $hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast
  899. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,$statKey,$period,$statValue,0,0);
  900. }
  901. else {
  902. my $newValue = 0;
  903. $newValue = "00:00:00" if $statKey !~ /_Count:$/;
  904. $newValue = $hidden{$statKey} if defined $hidden{$statKey};
  905. # statistics_storeSingularReadings
  906. # $hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast
  907. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,$statKey,$period,$newValue,$statValue,$saveLast);
  908. }
  909. }
  910. }
  911. # Store hidden reading
  912. $result = "";
  913. while ( my ($key, $duration) = each(%hidden) ) {
  914. $result .= " " if $result;
  915. $result .= "$key $duration";
  916. }
  917. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  918. statistics_Log $hash, 5, "Set '$hiddenReadingName = $result'";
  919. return;
  920. }
  921. ####################
  922. sub statistics_storeSingularReadings ($$$$$$$$$$)
  923. {
  924. my ($hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast) = @_;
  925. return if $singularReadings eq "";
  926. if ($statType =~ /Delta|Tendency/) { $statReadingName .= $period; }
  927. else { $statReadingName .= $statType; }
  928. my $devName=$dev->{NAME};
  929. if ("$devName:$readingName:$statType:$period" =~ /^($singularReadings)$/) {
  930. readingsBulkUpdate($dev, $statReadingName, $statValue, 1);
  931. statistics_Log $hashName, 5, "Set ".$statReadingName." = $statValue"; # Fehler um 24 Uhr
  932. if ($saveLast) {
  933. readingsBulkUpdate($dev, $statReadingName."Last", $lastValue, 1);
  934. statistics_Log $hashName, 5, "Set ".$statReadingName."Last = $lastValue";
  935. }
  936. }
  937. }
  938. ####################
  939. sub statistics_getStoredDevices ($)
  940. {
  941. my ($hash) = @_;
  942. my $result="";
  943. foreach my $r (sort keys %{$hash->{READINGS}}) {
  944. if ($r =~ /^\.(.*):.*/) {
  945. my $device = $1;
  946. my $regExp = '^'.$1.'$|^'.$1.',|,'.$1.'$|,'.$1.',';
  947. if ($result !~ /$regExp/) {
  948. $result.="," . $device;
  949. }
  950. }
  951. }
  952. return $result;
  953. }
  954. ########################################
  955. sub statistics_FormatDuration($)
  956. {
  957. my ($value) = @_;
  958. #Tage
  959. my $returnstr ="";
  960. if ($value > 86400) { $returnstr = sprintf "%dd ", int($value/86400); }
  961. # Stunden
  962. if ($value == 86400) {
  963. $returnstr = "24:00:00";
  964. } else {
  965. $value %= 86400;
  966. $returnstr .= sprintf "%02d:", int($value/3600);
  967. $value %= 3600;
  968. $returnstr .= sprintf "%02d:", int($value/60);
  969. $value %= 60;
  970. $returnstr .= sprintf "%02d", $value;
  971. }
  972. return $returnstr;
  973. }
  974. ########################################
  975. sub statistics_maxDecPlaces($$)
  976. {
  977. my ($value, $decMax) = @_;
  978. $decMax = 0 if ! defined $decMax;
  979. if ( $value =~ /.*\.(.*)/ ) {
  980. my $decPlaces = length($1);
  981. $decMax = $decPlaces >= $decMax ? $decPlaces : $decMax;
  982. }
  983. return $decMax;
  984. }
  985. ########################################
  986. sub statistics_UpdateDevReading($$$$)
  987. {
  988. my ($dev, $rname, $val, $event) = @_;
  989. $dev->{READINGS}{$rname}{VAL} = $val;
  990. $dev->{READINGS}{$rname}{TIME} = TimeNow();
  991. if ($event==1) {
  992. if (exists ($dev->{CHANGED})) {
  993. my $max = int(@{$dev->{CHANGED}});
  994. $dev->{CHANGED}[$max] = "$rname: $val";
  995. }
  996. } else {
  997. readingsBulkUpdate($dev, $rname, $val, 1);
  998. }
  999. }
  1000. ##########################
  1001. 1;
  1002. =pod
  1003. =begin html
  1004. =item helper
  1005. =item summary Calculates for statistical values and adds them to the devices.
  1006. =item summary_DE Berechnet statistische Werte und f&uuml;gt sie dem Ger&auml;t hinzu.
  1007. <a name="statistics"></a>
  1008. <h3>statistics</h3>
  1009. (en | <a href="http://fhem.de/commandref_DE.html#statistics">de</a>)
  1010. <div>
  1011. <ul>
  1012. This modul calculates for certain readings of given devices statistical values and adds them to the devices.
  1013. <br>
  1014. For detail instructions, look at and please maintain the <a href="http://www.fhemwiki.de/wiki/statistics"><b>FHEM-Wiki</b></a>.
  1015. <br>
  1016. Until now statistics for the following readings are automatically built:
  1017. <ul>
  1018. <br>
  1019. <li><b>Min|Avg|Max</b> Minimum, average and maximum of instantaneous values:
  1020. <br>
  1021. over a period of day, month and year:
  1022. <br>
  1023. <i>brightness, current, energy_current, humidity, luminosity, temperature, voltage</i>
  1024. <br>
  1025. over a period of hour, day, month and year:
  1026. <br>
  1027. <i>wind, wind_speed, windSpeed</i>
  1028. </li><br>
  1029. <li><b>Tendency</b> over 1h, 2h, 3h und 6h: <i>pressure</i>
  1030. </li><br>
  1031. <li><b>Delta</b> between start and end values - over a period of hour, day, month and year:
  1032. <br>
  1033. <i>count, energy, energy_total, power, total, rain, rain_rate, rain_total</i>
  1034. </li><br>
  1035. <li><b>Duration</b> (and counter) of the states (on, off, open, closed...) over a period of day, month and year:
  1036. <br>
  1037. <i>lightsensor, lock, motion, Window, window, state (if no other reading is recognized)</i>
  1038. </li><br>
  1039. </ul>
  1040. Further readings can be added via the <a href="#statisticsattr">attributes</a> <code>deltaReadings, durationReadings, minAvgMaxReadings, tendencyReadings</code>.
  1041. This allows also to assign a reading to another statistic type.
  1042. <br>&nbsp;
  1043. <br>
  1044. <b>Define</b>
  1045. <ul>
  1046. <br>
  1047. <code>define &lt;name&gt; statistics &lt;deviceNameRegExp&gt; [Prefix]</code>
  1048. <br>
  1049. Example: <code>define Statistik statistics Sensor_.*|Wettersensor</code>
  1050. <br>&nbsp;
  1051. <li><code>&lt;DeviceNameRegExp&gt;</code>
  1052. <br>
  1053. Regular expression of device names. <b>!!! Not the device readings !!!</b>
  1054. </li><br>
  1055. <li><code>[Prefix]</code>
  1056. <br>
  1057. Optional. Prefix set is place before statistical data. Default is <i>stat</i>
  1058. </li><br>
  1059. </ul>
  1060. <br>
  1061. <b>Set</b>
  1062. <ul>
  1063. <br>
  1064. <li><code>resetStatistics &lt;All|DeviceName&gt;</code>
  1065. <br>
  1066. Resets the statistic values of the selected device.
  1067. </li><br>
  1068. <li><code>doStatistics</code>
  1069. <br>
  1070. Calculates the current statistic values of all monitored devices.
  1071. </li><br>
  1072. </ul>
  1073. <br>
  1074. <b>Get</b>
  1075. <ul>not implemented yet
  1076. </ul>
  1077. <br>
  1078. <a name="statisticsattr"></a>
  1079. <b>Attributes</b>
  1080. <ul>
  1081. <br>
  1082. <li><code>dayChangeTime &lt;time&gt;</code>
  1083. <br>
  1084. Time of day change. Default is 00:00. For weather data the day change can be set e.g. to 06:50.
  1085. </li><br>
  1086. <li><code>deltaReadings &lt;readings&gt;</code>
  1087. <br>
  1088. Comma separated list of reading names for which a delta statistic shall be calculated.
  1089. </li><br>
  1090. <li><code>durationPeriodHour &lt; 1 | 0 &gt;</code>
  1091. <br>
  1092. If set to 1, then duration readings will get hourly statistics too.
  1093. </li><br>
  1094. <li><code>durationReadings &lt;readings&gt;</code>
  1095. <br>
  1096. Comma separated list of reading names for which a duration statistic shall be calculated.
  1097. </li><br>
  1098. <li><code>excludedReadings &lt;DeviceRegExp:ReadingNameRegExp&gt;</code>
  1099. <br>
  1100. Regular expression of the readings that shall be excluded from the statistics.<br>
  1101. The reading have to be entered in the form <i>deviceName:readingName</i>.
  1102. <br>
  1103. E.g. <code>FritzDect:current|Sensor_.*:humidity</code>
  1104. <br>
  1105. </li><br>
  1106. <li><code>ignoreDefaultAssignments <code>&lt; 0 | 1 &gt;</code></code>
  1107. <br>
  1108. Ignores the default assignments of readings to a statistic type (see above).<br>
  1109. So, only the readings that are listed in the specific attributes are evaluated.
  1110. <br>
  1111. </li><br>
  1112. <li><code>minAvgMaxReadings &lt;readings&gt;</code>
  1113. <br>
  1114. Comma separated list of reading names for which a min/average/max statistic shall be calculated.
  1115. </li><br>
  1116. <li><code>periodChangePreset &lt;seconds&gt;</code>
  1117. <br>
  1118. Preponed start of the calculation of periodical data. Default is 5 second before each full hour.
  1119. <br>
  1120. Allows thus the correct timely assignment within plots. Should be adapted to the CPU speed or load of the server.
  1121. <br>
  1122. </li><br>
  1123. <li><code>singularReadings &lt;DeviceRegExp:ReadingRegExp&gt;:statTypes:period</code>
  1124. <ul>
  1125. <li>statTypes: Min|Avg|Max|Delta|<i>DurationState</i>|<span style="color:blue;">Tendency</span></li>
  1126. <li>period: Hour|Day|Month|Year|<span style="color:blue;">1h|2h|3h|6h</span></li>
  1127. </ul>
  1128. <br>
  1129. Regulare expression of statistic values, which for which singular readings are created <u>additionally</u> to the summary readings. Eases the creation of plots. For duration readings the name of the state has to be used as statTypes.
  1130. <dt>Example:</dt>
  1131. <dd>
  1132. <code>Wettersensor:rain:Delta:(Hour|Day)|(FritzDect:(current|power):(Avg|Max|Delta):(Hour|Day)</code>
  1133. <br>
  1134. <code>Badfenster:Window:(Open|Open_Count):Month</code>
  1135. </dd>
  1136. </li><br>
  1137. <li><code>specialDeltaPeriods &lt;Device:Reading:Period:count1:count2:...&gt;</code>
  1138. <br>
  1139. Creates for the given delta reading additional singular readings of the given numbers of a period (Hour, Day, Month)
  1140. <br>
  1141. Regular expressions <u>cannot</u> be used. Additional readings or additional periods can be defined but have to be separated by a comma (without spaces).
  1142. <br>
  1143. <dt>Example:</dt>
  1144. <dd>
  1145. <code>attr Statistik specialDeltaPeriods Wettersensor:rain:Hour:06:72:96</code>
  1146. <br>
  1147. This will create 3 additional readings for the rain of the last 6, 72 and 96 hours.
  1148. <br>
  1149. <code>attr Statistik specialDeltaPeriods Wettersensor:rain:Hour:48,Wettersensor:rain:Day:30,EZaehler:energy:Month:6:12</code>
  1150. <br>
  1151. This will create 4 additional readings for the rain of the last 48 hours and the last 30 Days and the energy consumtion of the last 6 and 12 months.
  1152. </dd>
  1153. </li><br>
  1154. <li><code>specialDeltaPeriodHours</code>
  1155. <br>
  1156. depreciated
  1157. </li><br>
  1158. <li><code>tendencyReadings &lt;readings&gt;</code>
  1159. <br>
  1160. Comma separated list of reading names for which a tendendy statistic shall be calculated.
  1161. </li><br>
  1162. </ul>
  1163. </ul>
  1164. </div>
  1165. =end html
  1166. =begin html_DE
  1167. <a name="statistics"></a>
  1168. <h3>statistics</h3>
  1169. (<a href="http://fhem.de/commandref.html#statistics">en</a> | de)
  1170. <div>
  1171. <ul>
  1172. Dieses Modul wertet von den angegebenen Ger&auml;ten (als regul&auml;rer Ausdruck) bestimmte Werte statistisch aus und f&uuml;gt das Ergebnis den jeweiligen Ger&auml;ten als neue Werte hinzu.
  1173. <br>
  1174. F&uuml;r detailierte Anleitungen bitte die <a href="http://www.fhemwiki.de/wiki/Statistics"><b>FHEM-Wiki</b></a> konsultieren und erg&auml;nzen.
  1175. <br>&nbsp;
  1176. <br>
  1177. Es unterscheidet in vier Statistik-Typen denen bereits standardm&auml;ssig Ger&auml;tewerte zugeordnet sind:
  1178. <ul>
  1179. <li><b>Min|Avg|Max</b> Minimum, Durchschnitt und Maximum von Momentanwerten:
  1180. <br>
  1181. &uuml;ber den Zeitraum Tag, Monat und Jahr:
  1182. <br>
  1183. <i>brightness, current, energy_current, humidity, luminosity, temperature, voltage</i>
  1184. <br>
  1185. &uuml;ber den Zeitraum Stunde, Tag, Monat und Jahr:
  1186. <br>
  1187. <i>wind, wind_speed, windSpeed</i>
  1188. </li><br>
  1189. <li><b>Tendency</b> Tendenz &uuml;ber 1h, 2h, 3h und 6h: <i>pressure</i>
  1190. </li><br>
  1191. <li><b>Delta</b> Differenz zwischen Anfangs- und Endwerte innerhalb eines Zeitraums (Stunde, Tag, Monat, Jahr):
  1192. <br>
  1193. <i>count, energy, energy_total, power, total, rain, rain_rate, rain_total</i>
  1194. </li><br>
  1195. <li><b>Duration</b> Dauer und Anzahl der Zust&auml;nde (on, off, open, closed...) innerhalb eines Zeitraums (Tag, Monat, Jahr):
  1196. <br>
  1197. <i>lightsensor, lock, motion, Window, window, state (wenn kein anderer Ger&auml;tewert g&uuml;ltig)</i>
  1198. </li><br>
  1199. </ul>
  1200. &Uuml;ber die <a href="#statisticsattr">Attribute</a> <code>deltaReadings, durationReadings, minAvgMaxReadings, tendencyReadings</code> k&ouml;nnen weitere Ger&auml;tewerte hinzugef&uuml;gt oder
  1201. einem anderen Statistik-Typ zugeordnet werden.
  1202. <br>&nbsp;
  1203. <br>
  1204. <b>Define</b>
  1205. <ul>
  1206. <br>
  1207. <code>define &lt;Name&gt; statistics &lt;Ger&auml;teNameRegExp&gt; [Prefix]</code>
  1208. <br>
  1209. Beispiel: <code>define Statistik statistics Wettersensor|Badsensor</code>
  1210. <br>&nbsp;
  1211. <li><code>&lt;Ger&auml;teNameRegExp&gt;</code>
  1212. <br>
  1213. Regul&auml;rer Ausdruck f&uuml;r den Ger&auml;tenamen. <b>!!! Nicht die Ger&auml;tewerte !!!</b>
  1214. </li><br>
  1215. <li><code>[Prefix]</code>
  1216. <br>
  1217. Optional. Der Prefix wird vor den Namen der statistischen Ger&auml;tewerte gesetzt. Standardm&auml;ssig <i>stat</i>
  1218. </li><br>
  1219. </ul>
  1220. <br>
  1221. <b>Set</b>
  1222. <ul>
  1223. <br>
  1224. <li><code>resetStatistics &lt;All|Ger&auml;tename&gt;</code>
  1225. <br>
  1226. Setzt die Statistiken der ausgew&auml;hlten Ger&auml;te zur&uuml;ck.
  1227. </li><br>
  1228. <li><code>doStatistics</code>
  1229. <br>
  1230. Berechnet die aktuellen Statistiken aller beobachteten Ger&auml;te.
  1231. </li><br>
  1232. </ul>
  1233. <br>
  1234. <b>Get</b>
  1235. <ul>nicht implementiert
  1236. </ul>
  1237. <br>
  1238. <a name="statisticsattr"></a>
  1239. <b>Attributes</b>
  1240. <ul>
  1241. <br>
  1242. <li><code>dayChangeTime &lt;Zeit&gt;</code>
  1243. <br>
  1244. Uhrzeit des Tageswechsels. Standardm&auml;ssig 00:00. Bei Wetterdaten kann der Tageswechsel z.B. auf 6:50 gesetzt werden.
  1245. </li><br>
  1246. <li><code>deltaReadings &lt;Ger&auml;tewerte&gt;</code>
  1247. <br>
  1248. Durch Kommas getrennte Liste von weiteren Ger&auml;tewerten, f&uuml;r welche die Differenz zwischen den Werten am Anfang und Ende einer Periode (Stunde/Tag/Monat/Jahr) bestimmt wird.
  1249. </li><br>
  1250. <li><code>durationPeriodHour &lt; 1 | 0 &gt;</code>
  1251. <br>
  1252. Wenn auf 1 gesetzt, dann werden f&uuml;r "durationReadings" auch st&uuml;ndliche Statistiken gebildet.
  1253. </li><br>
  1254. <li><code>durationReadings &lt;Ger&auml;tewerte&gt;</code>
  1255. <br>
  1256. Durch Kommas getrennte Liste von weiteren Ger&auml;tewerten, f&uuml;r welche die Dauer einzelner Ger&auml;tewerte innerhalb bestimmte Zeitr&auml;ume (Stunde/Tag/Monat/Jahr) erfasst wird.
  1257. </li><br>
  1258. <li><code>excludedReadings &lt;Ger&auml;tenameRegExp:Ger&auml;tewertRegExp&gt;</code>
  1259. <br>
  1260. Regul&auml;rer Ausdruck der Ger&auml;tewerte die nicht ausgewertet werden sollen.
  1261. z.B. <code>FritzDect:current|Sensor_.*:humidity</code>
  1262. <br>
  1263. </li><br>
  1264. <li><code>ignoreDefaultAssignments <code>&lt;0 | 1&gt;</code></code>
  1265. <br>
  1266. Ignoriert die Standardzuordnung von Ger&auml;tewerten zu Statistiktypen..<br>
  1267. D.h., nur die Ger&auml;tewerte, die &uuml;ber Attribute den Statistiktypen zugeordnet sind, werden ausgewertet.
  1268. <br>
  1269. </li><br>
  1270. <li><code>hideAllSummaryReadings &lt;0 | 1&gt;</code>
  1271. <br>
  1272. noch nicht implementiert - Es werden keine gesammelten Statistiken angezeigt, sondern nur die unter "singularReadings" definierten Einzelwerte
  1273. </li><br>
  1274. <li><code>minAvgMaxReadings &lt;Ger&auml;tewerte&gt;</code>
  1275. <br>
  1276. Durch Kommas getrennte Liste von Ger&auml;tewerten, f&uuml;r die in bestimmten Zeitr&auml;umen (Tag, Monat, Jahr) Minimum, Mittelwert und Maximum erfasst werden.
  1277. </li><br>
  1278. <li><code>periodChangePreset &lt;Sekunden&gt;</code>
  1279. <br>
  1280. Start der Berechnung der periodischen Daten, standardm&auml;ssig 5 Sekunden vor der vollen Stunde,
  1281. <br>
  1282. Erlaubt die korrekte zeitliche Zuordnung in Plots, kann je nach Systemauslastung verringert oder vergr&ouml;&szlig;ert werden.
  1283. <br>
  1284. </li><br>
  1285. <li><code>singularReadings &lt;Ger&auml;tRegExp:Ger&auml;teWertRegExp:Statistiktyp:Zeitraum&gt;</code>
  1286. <ul>
  1287. <li>Statistiktyp: Min|Avg|Max|Delta|<i>DurationState</i>|<span style="color:blue;">Tendency</span></li>
  1288. <li>Zeitraum: Hour|Day|Month|Year|<span style="color:blue;">1h|2h|3h|6h</span></li>
  1289. </ul>
  1290. Regul&auml;rer Ausdruck statistischer Werte, die <u>zus&auml;tzlich</u> auch als einzelne Werte gespeichert werden sollen.
  1291. Erleichtert die Erzeugung von Plots und anderer Auswertungen (notify).
  1292. <br>
  1293. F&uuml;r "duration"-Ger&auml;tewerte muss der Name des jeweiligen Statuswertes als <code>Statistiktyp</code> eingesetzt werden.
  1294. <dt>Beispiel:</dt>
  1295. <dd>
  1296. <code>Wettersensor:rain:Delta:(Hour|Day)|FritzDect:power:Delta:Day</code>
  1297. <br>
  1298. <code>Wettersensor:rain:Delta:(Hour|Day)|FritzDect:power:Delta:Day</code>
  1299. </dd>
  1300. </li><br>
  1301. <li><code>specialDeltaPeriods &lt;Ger&auml;t:Ger&auml;tewert:Zeitraum:Anzahl1:Anzahl2:...&gt;</code>
  1302. <br>
  1303. Erzeugt f&uuml;r die angegebenen "delta"-Ger&auml;tewerte zus&auml;tzliche Einzelwerte &uuml;ber die angegebene Anzahl eines Zeitraums (Hour, Day, Month).
  1304. <br>
  1305. Regul&auml;re Ausdr&uuml;cke k&ouml;nnen <u>nicht</u> genutzt werden. Es k&ouml;nnen auch mehrere Ger&auml;tewert und/oder Zeitr&auml;ume hinzugef&uuml;gt werden. Diese m&uuml;ssen durch Kommas (ohne Leerzeichen) getrennt werden.
  1306. <br>
  1307. <dt>Beispiel:</dt>
  1308. <dd>
  1309. <code>attr Statistik specialDeltaPeriods Wettersensor:rain:Hour:06:72:96</code>
  1310. <br>
  1311. Dies erzeugt 3 zus&auml;tzliche Werte f&uuml;r die Regenmenge in den letzten 6, 72, 96 Stunden.
  1312. <br>
  1313. <code>attr Statistik specialDeltaPeriods Wettersensor:rain:Hour:48,Wettersensor:rain:Day:30,EZaehler:energy:Month:6:12</code>
  1314. <br>
  1315. Dies erzeugt 4 zus&auml;tzliche Werte f&uuml;r die Regenmenge in den letzten 48 Stunden und den letzten 30 Tagen und den Energieverbrauch der letzten 6 und 12 Monate.
  1316. </dd>
  1317. </li><br>
  1318. <li><code>specialDeltaPeriodHours</code>
  1319. <br>
  1320. veraltet
  1321. </li><br>
  1322. <li><code>tendencyReadings &lt;Ger&auml;tewerte&gt;</code>
  1323. <br>
  1324. Durch Kommas getrennte Liste von weiteren Ger&auml;tewerten, f&uuml;r die innerhalb bestimmter Zeitr&auml;ume (1h, 2h, 3h, 6h) die Differenz zwischen Anfangs- und Endwert ermittelt wird.
  1325. </li><br>
  1326. <li><a href="#readingFnAttributes">readingFnAttributes</a>
  1327. </li><br>
  1328. </ul>
  1329. </ul>
  1330. </div>
  1331. =end html_DE
  1332. =cut