98_statistics.pm 59 KB

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