98_statistics.pm 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. ##############################################
  2. # $Id: 98_statistics.pm 15455 2017-11-19 12:30:39Z 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->{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:textField-long "
  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: 2017-11-19 13:30:39 +0100 (Sun, 19 Nov 2017) $';
  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. $singularReadings=~s/\n|\r//g;
  510. if ($singularReadings ne "") {
  511. # statistics_storeSingularReadings $hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast
  512. my $statValue = sprintf "%.".$decPlaces."f", $stat[1];
  513. if ($saveLast) { $lastValue = $statValue; $statValue = $value; }
  514. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Min",$period,$statValue,$lastValue,$saveLast);
  515. $statValue = sprintf "%.".$decPlaces."f", $stat[3];
  516. if ($saveLast) { $lastValue = $statValue; $statValue = $value; }
  517. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Avg",$period,$statValue,$lastValue,$saveLast);
  518. $statValue = sprintf "%.".$decPlaces."f", $stat[5];
  519. if ($saveLast) { $lastValue = $statValue; $statValue = $value; }
  520. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Max",$period,$statValue,$lastValue,$saveLast);
  521. }
  522. # Store hidden reading
  523. $result = "Sum: $hidden[1] Time: $hidden[3] LastValue: ".$value." LastTime: ".int(gettimeofday())." ShowDate: $hidden[9] DecPlaces: $decPlaces";
  524. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  525. statistics_Log $hash, 5, "Set '$hiddenReadingName'='$result'";
  526. return;
  527. }
  528. # Calculates tendency values
  529. ########################################
  530. sub statistics_doStatisticTendency ($$$)
  531. {
  532. my ($hash, $dev, $readingName) = @_;
  533. my $name = $hash->{NAME};
  534. my $devName = $dev->{NAME};
  535. my $decPlaces = 0;
  536. return if not exists ($dev->{READINGS}{$readingName});
  537. # Get reading, cut out first number without units
  538. my $value = $dev->{READINGS}{$readingName}{VAL};
  539. $value =~ s/\s*(-?[\d.]*).*/$1/e;
  540. statistics_Log $hash, 4, "Calculating hourly tendency statistics for '".$dev->{NAME}.":$readingName = $value'";
  541. my $statReadingName = $hash->{PREFIX} . ucfirst($readingName) . "Tendency";
  542. my $hiddenReadingName = ".".$dev->{NAME}.":".$readingName."Tendency";
  543. my @hidden; my @stat;
  544. my $firstRun = not exists($hash->{READINGS}{$hiddenReadingName});
  545. if ( $firstRun ) {
  546. @stat = split / /, "1h: - 2h: - 3h: - 6h: -";
  547. statistics_Log $hash,4,"Initializing statistic of '$hiddenReadingName'.";
  548. $hash->{READINGS}{$hiddenReadingName}{VAL} = "";
  549. }
  550. else {
  551. @stat = split / /, $dev->{READINGS}{$statReadingName}{VAL};
  552. }
  553. statistics_Log $hash, 4, "Add $value to $hiddenReadingName";
  554. my $result = $value;
  555. if (exists ($hash->{READINGS}{$hiddenReadingName}{VAL})) { $result .= " " . $hash->{READINGS}{$hiddenReadingName}{VAL}; }
  556. @hidden = split / /, $result; # Internal values
  557. # determine decPlaces with stored values
  558. foreach (@hidden) {
  559. $decPlaces = statistics_maxDecPlaces($_, $decPlaces);
  560. }
  561. if ( exists($hidden[7]) ) {
  562. statistics_Log $hash, 4, "Remove last value ".$hidden[7]." from '$hiddenReadingName'";
  563. delete $hidden[7];
  564. }
  565. if ( exists($hidden[1]) ) {$stat[1] = sprintf "%+.".$decPlaces."f", $value-$hidden[1];}
  566. if ( exists($hidden[2]) ) {$stat[3] = sprintf "%+.".$decPlaces."f", $value-$hidden[2];}
  567. if ( exists($hidden[3]) ) {$stat[5] = sprintf "%+.".$decPlaces."f", $value-$hidden[3];}
  568. if ( exists($hidden[6]) ) {$stat[7] = sprintf "%+.".$decPlaces."f", $value-$hidden[6];}
  569. $result = "1h: " . $stat[1] ." 2h: ". $stat[3] ." 3h: ". $stat[5] ." 6h: ". $stat[7];
  570. readingsBulkUpdate($dev, $statReadingName, $result, 1);
  571. # Store single readings
  572. my $singularReadings = AttrVal($name, "singularReadings", "");
  573. $singularReadings=~s/\n|\r//g;
  574. if ($singularReadings ne "") {
  575. # statistics_storeSingularReadings $hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast
  576. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Tendency","1h",$stat[1],0,0);
  577. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Tendency","2h",$stat[3],0,0);
  578. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Tendency","3h",$stat[5],0,0);
  579. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,"Tendency","6h",$stat[7],0,0);
  580. }
  581. $result = join( " ", @hidden );
  582. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  583. statistics_Log $hash, 4, "Set '$hiddenReadingName = $result'";
  584. return ;
  585. }
  586. # Calculates deltas for day, month and year
  587. ########################################
  588. sub statistics_doStatisticDelta ($$$$)
  589. {
  590. my ($hash, $dev, $readingName, $periodSwitch) = @_;
  591. my $dummy;
  592. my $result;
  593. my $showDate;
  594. my $name = $hash->{NAME};
  595. my $decPlaces = 0;
  596. return if not exists ($dev->{READINGS}{$readingName});
  597. # Get reading, extract first number without units
  598. my $value = $dev->{READINGS}{$readingName}{VAL};
  599. $value =~ s/\s*(-?[\d.]*).*/$1/e;
  600. statistics_Log $hash, 4, "Calculating delta statistics for '".$dev->{NAME}.":$readingName = $value'";
  601. my $hiddenReadingName = ".".$dev->{NAME}.":".$readingName;
  602. my $statReadingName = $hash->{PREFIX};
  603. $statReadingName .= ucfirst($readingName);
  604. my @hidden; my @stat; my @last;
  605. my $firstRun = not exists($hash->{READINGS}{$hiddenReadingName});
  606. # Show since-Value and initialize all readings
  607. if ( $firstRun ) {
  608. $showDate = 8;
  609. @stat = split / /, "Hour: 0 Day: 0 Month: 0 Year: 0";
  610. $stat[9] = strftime "%Y-%m-%d_%H:%M:%S", localtime();
  611. @last = split / /, "Hour: - Day: - Month: - Year: -";
  612. statistics_Log $hash, 4, "Initializing statistic of '$hiddenReadingName'.";
  613. }
  614. # Do calculations if hidden reading exists
  615. else {
  616. @stat = split / /, $dev->{READINGS}{$statReadingName}{VAL};
  617. @hidden = split / /, $hash->{READINGS}{$hiddenReadingName}{VAL}; # Internal values
  618. $showDate = $hidden[3];
  619. $decPlaces = statistics_maxDecPlaces($value, $hidden[5]);
  620. if (exists ($dev->{READINGS}{$statReadingName."Last"})) {
  621. @last = split / /, $dev->{READINGS}{$statReadingName."Last"}{VAL};
  622. }
  623. else {
  624. @last = split / /, "Hour: - Day: - Month: - Year: -";
  625. }
  626. my $deltaValue = $value - $hidden[1];
  627. # Do statistic
  628. $stat[1] += $deltaValue;
  629. $stat[3] += $deltaValue;
  630. $stat[5] += $deltaValue;
  631. $stat[7] += $deltaValue;
  632. if ($periodSwitch>=1) { statistics_doStatisticSpecialPeriod ( $hash, $dev, $readingName, $decPlaces, $stat[1] ); }
  633. # Determine if "since" value has to be shown in current and last reading
  634. # If change of year, change yearly statistic
  635. if ($periodSwitch == 4 || $periodSwitch == -4) {
  636. $last[7] = sprintf "%.".$decPlaces."f", $stat[7];
  637. $stat[7] = 0;
  638. if ($showDate == 1) { $showDate = 0; } # Do not show the "since:" value for year changes anymore
  639. if ($showDate >= 2) { $showDate = 1; $last[9] = $stat[9]; } # Shows the "since:" value for the first year change
  640. statistics_Log $hash, 4, "Shifting current year in last value of '$statReadingName'.";
  641. }
  642. # If change of month, change monthly statistic and check for special periods
  643. if ($periodSwitch >= 3 || $periodSwitch <= -3){
  644. statistics_doStatisticSpecialPeriod2 ($hash, $dev, $readingName, "Month", $decPlaces, $stat[5]);
  645. $last[5] = sprintf "%.".$decPlaces."f", $stat[5];
  646. $stat[5] = 0;
  647. if ($showDate == 3) { $showDate = 2; } # Do not show the "since:" value for month changes anymore
  648. if ($showDate >= 4) { $showDate = 3; $last[9] = $stat[9]; } # Shows the "since:" value for the first month change
  649. statistics_Log $hash, 4, "Shifting current month in last value of '$statReadingName'.";
  650. }
  651. # If change of day, change daily statistic and check for special periods
  652. if ($periodSwitch >= 2 || $periodSwitch <= -2){
  653. statistics_doStatisticSpecialPeriod2 ($hash, $dev, $readingName, "Day", $decPlaces, $stat[3]);
  654. $last[3] = sprintf "%.".$decPlaces."f", $stat[3];
  655. $stat[3] = 0;
  656. if ($showDate == 5) { $showDate = 4; } # Do not show the "since:" value for day changes anymore
  657. if ($showDate >= 6) { # Shows the "since:" value for the first day change
  658. $showDate = 5;
  659. $last[9] = $stat[9];
  660. # Next monthly and yearly values start normaly at 00:00 and show only date (no time)
  661. if (AttrVal($name, "dayChangeTime", "00:00") =~ /00:00|0:00/) {
  662. my $periodChangePreset = AttrVal($name, "periodChangePreset", 5);
  663. $stat[5] = 0;
  664. $stat[7] = 0;
  665. $stat[9] = strftime "%Y-%m-%d", localtime(gettimeofday()+$periodChangePreset); # start
  666. }
  667. }
  668. statistics_Log $hash,4,"Shifting current day in last value of '$statReadingName'.";
  669. }
  670. # If change of hour, change hourly statistic and check for special periods
  671. if ($periodSwitch >= 1){
  672. statistics_doStatisticSpecialPeriod2 ($hash, $dev, $readingName, "Hour", $decPlaces, $stat[1]);
  673. $last[1] = sprintf "%.".$decPlaces."f", $stat[1];
  674. $stat[1] = 0;
  675. if ($showDate == 7) { $showDate = 6; } # Do not show the "since:" value for day changes anymore
  676. if ($showDate >= 8) { $showDate = 7; $last[9] = $stat[9]; } # Shows the "since:" value for the first hour change
  677. statistics_Log $hash, 4, "Shifting current hour in last value of '$statReadingName'.";
  678. }
  679. }
  680. # Store hidden reading
  681. $result = "LastValue: $value ShowDate: $showDate DecPlaces: $decPlaces";
  682. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  683. statistics_Log $hash, 5, "Set '$hiddenReadingName'='$result'";
  684. # Store visible statistic readings (delta values)
  685. $result = sprintf "Hour: %.".$decPlaces."f Day: %.".$decPlaces."f Month: %.".$decPlaces."f Year: %.".$decPlaces."f", $stat[1], $stat[3], $stat[5], $stat[7];
  686. if ( $showDate >=2 ) { $result .= " (since: $stat[9] )"; }
  687. readingsBulkUpdate($dev,$statReadingName,$result, 1);
  688. statistics_Log $hash, 5, "Set '$statReadingName'='$result'";
  689. # if changed, store previous visible statistic (delta) values
  690. if ($periodSwitch != 0) {
  691. $result = "Hour: $last[1] Day: $last[3] Month: $last[5] Year: $last[7]";
  692. if ( $showDate =~ /1|3|5|7/ ) { $result .= " (since: $last[9] )"; }
  693. readingsBulkUpdate($dev,$statReadingName."Last",$result, 1);
  694. statistics_Log $hash, 4, "Set '".$statReadingName."Last'='$result'";
  695. }
  696. # Store single readings
  697. my $singularReadings = AttrVal($name, "singularReadings", "");
  698. $singularReadings=~s/\n|\r//g;
  699. if ($singularReadings ne "") {
  700. # statistics_storeSingularReadings $hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast
  701. my $statValue = sprintf "%.".$decPlaces."f", $stat[1];
  702. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName, $readingName, "Delta", "Hour", $statValue, $last[1], $periodSwitch >= 1);
  703. $statValue = sprintf "%.".$decPlaces."f", $stat[3];
  704. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName, $readingName, "Delta", "Day", $statValue, $last[3], $periodSwitch >= 2 || $periodSwitch <= -2);
  705. $statValue = sprintf "%.".$decPlaces."f", $stat[5];
  706. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName, $readingName, "Delta", "Month", $statValue, $last[5], $periodSwitch >= 3 || $periodSwitch <= -3);
  707. $statValue = sprintf "%.".$decPlaces."f", $stat[7];
  708. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName, $readingName, "Delta", "Year", $statValue, $last[7], $periodSwitch == 4 || $periodSwitch == -4);
  709. }
  710. return ;
  711. }
  712. # Calculates deltas for period of several hours
  713. ########################################
  714. sub statistics_doStatisticSpecialPeriod ($$$$$)
  715. {
  716. my ($hash, $dev, $readingName, $decPlaces, $value) = @_;
  717. my $name = $hash->{NAME};
  718. my $specialPeriod = AttrVal($name, "specialDeltaPeriodHours", 0);
  719. return if $specialPeriod == 0;
  720. my $statReadingName = $hash->{PREFIX} . ucfirst($readingName) . "SpecialPeriod";
  721. my $hiddenReadingName = ".".$dev->{NAME} . ":" . $readingName . "SpecialPeriod";
  722. # Update hidden stack
  723. my @hidden = ();
  724. if (exists ($hash->{READINGS}{$hiddenReadingName}{VAL}))
  725. { @hidden = split / /, $hash->{READINGS}{$hiddenReadingName}{VAL}; }
  726. unshift @hidden, $value;
  727. statistics_Log $hash, 4, "Add $value to $hiddenReadingName";
  728. while ( $#hidden >= $specialPeriod ) {
  729. my $lastValue = pop @hidden;
  730. statistics_Log $hash, 4, "Remove last (".$#hidden.") value '$lastValue' from '$hiddenReadingName'";
  731. }
  732. # Calculate specialPeriodValue
  733. my $result = 0;
  734. foreach (@hidden) { $result += $_; }
  735. $result = sprintf "%.".$decPlaces."f", $result;
  736. $result .= " (".($#hidden+1).".hours)" if $#hidden != $specialPeriod-1;
  737. statistics_Log $hash, 4, "Set '$statReadingName = $result'";
  738. readingsBulkUpdate($dev, $statReadingName, $result, 1);
  739. # Store hidden stack
  740. $result = join( " ", @hidden );
  741. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  742. statistics_Log $hash, 4, "Set '$hiddenReadingName = $result'";
  743. }
  744. # Calculates deltas for period of several hours
  745. ########################################
  746. sub statistics_doStatisticSpecialPeriod2 ($$$$$$)
  747. {
  748. my ($hash, $dev, $readingName, $periodText, $decPlaces, $value) = @_;
  749. my $name = $hash->{NAME};
  750. my $pattern;
  751. my $specialDeltaPeriods = AttrVal($name, "specialDeltaPeriods", "");
  752. return unless $specialDeltaPeriods;
  753. #Check if reading occures in attribute specialDeltaPeriods, extract period numbers
  754. my $devName = $dev->{NAME};
  755. $pattern = $devName . ":" . $readingName . ":" . $periodText . ':([\d:]+)';
  756. return unless $specialDeltaPeriods =~ $pattern;
  757. #Determine number of periods and maximal number
  758. my @periods = split /:/, $1;
  759. my $maxNumber=0;
  760. @periods = sort { $a <=> $b } @periods;
  761. $maxNumber = $periods[$#periods];
  762. #Determine reading names
  763. my $statReadingName = $hash->{PREFIX} . ucfirst($readingName) . ucfirst($periodText);
  764. my $hiddenReadingName = ".".$devName . ":" . $readingName . $periodText;
  765. # Update hidden stack
  766. my @hidden = ();
  767. if (exists ($hash->{READINGS}{$hiddenReadingName}{VAL}))
  768. { @hidden = split / /, $hash->{READINGS}{$hiddenReadingName}{VAL}; }
  769. unshift @hidden, $value;
  770. statistics_Log $hash, 4, "Add $value to $hiddenReadingName";
  771. while ( $#hidden > $maxNumber ) {
  772. my $lastValue = pop @hidden;
  773. statistics_Log $hash, 4, "Remove last value '$lastValue' from '$hiddenReadingName'";
  774. }
  775. #Calculate and write specialPeriodValues
  776. my $result = 0;
  777. my $counterHidden = 0;
  778. my $counterPeriods = 0;
  779. foreach (@hidden) {
  780. $result += $_;
  781. $counterHidden++;
  782. if ( $counterHidden == $periods[$counterPeriods] ) {
  783. my $rName = $statReadingName . $periods[$counterPeriods];
  784. my $rValue = sprintf "%.".$decPlaces."f", $result;
  785. statistics_Log $hash, 4, "Set '$rName = $rValue'";
  786. readingsBulkUpdate($dev, $rName, $rValue, 1);
  787. $counterPeriods++;
  788. }
  789. }
  790. #write specialPeriodValues for periods not reached yet
  791. for (my $i = $counterPeriods; $i <= $#periods; $i++) {
  792. my $rName = $statReadingName . $periods[$i];
  793. my $rValue = sprintf "%.".$decPlaces."f (%d %s)", $result, $#hidden+1, $periodText;
  794. statistics_Log $hash, 4, "Set '$rName = $rValue'";
  795. readingsBulkUpdate($dev, $rName, $rValue, 1);
  796. }
  797. # Store hidden stack
  798. $result = join( " ", @hidden );
  799. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  800. statistics_Log $hash, 4, "Set '$hiddenReadingName = $result'";
  801. }
  802. # Calculates single Duration Values and informs about end of day and month
  803. ########################################
  804. sub statistics_doStatisticDuration ($$$$)
  805. {
  806. my ($hash, $dev, $readingName, $periodSwitch) = @_;
  807. my $name = $hash->{NAME};
  808. my $devName = $dev->{NAME};
  809. return if not exists ($dev->{READINGS}{$readingName});
  810. # Get reading
  811. my $state = $dev->{READINGS}{$readingName}{VAL};
  812. $state = "(null)" if $state eq "";
  813. statistics_Log $hash, 4, "Calculating duration statistics for '".$dev->{NAME}.":$readingName = $state'";
  814. # Daily Statistic
  815. statistics_doStatisticDurationSingle $hash, $dev, $readingName, "Hour", $state, ($periodSwitch !=0)
  816. if AttrVal($name, "durationPeriodHour", 0) == 1;
  817. # Daily Statistic
  818. statistics_doStatisticDurationSingle $hash, $dev, $readingName, "Day", $state, ($periodSwitch >= 2 || $periodSwitch <= -2);
  819. # Monthly Statistic
  820. statistics_doStatisticDurationSingle $hash, $dev, $readingName, "Month", $state, ($periodSwitch >= 3 || $periodSwitch <= -3);
  821. # Yearly Statistic
  822. statistics_doStatisticDurationSingle $hash, $dev, $readingName, "Year", $state, ($periodSwitch == 4 || $periodSwitch == -4);
  823. return ;
  824. }
  825. # Calculates single duration values
  826. ########################################
  827. sub statistics_doStatisticDurationSingle ($$$$$$)
  828. {
  829. my ($hash, $dev, $readingName, $period, $state, $saveLast) = @_;
  830. my $result;
  831. my $hiddenReadingName = ".".$dev->{NAME}.":".$readingName.$period;
  832. my $name=$hash->{NAME};
  833. my $devName = $dev->{NAME};
  834. $state =~ s/ /_/g;
  835. $state = "on" if $state =~ /set_on/;
  836. $state = "off" if $state =~ /set_off/;
  837. my $statReadingName = $hash->{PREFIX};
  838. $statReadingName .= ucfirst($readingName).$period;
  839. my %hidden;
  840. my %stat;
  841. my $firstRun = not exists($hash->{READINGS}{$hiddenReadingName});
  842. my $lastState;
  843. # Show since-Value
  844. if ( $firstRun ) {
  845. $hidden{"showDate:"} = 1;
  846. $saveLast = 0;
  847. $lastState = $state;
  848. $hidden{"(since:"} = strftime ("%Y-%m-%d_%H:%M:%S)",localtime() );
  849. $hidden{$state.":"} = 0;
  850. $hidden{$state."_Count:"} = 1;
  851. }
  852. # Do calculations if hidden reading exists
  853. else {
  854. %hidden = split / /, $hash->{READINGS}{$hiddenReadingName}{VAL}; # Internal values
  855. $lastState = $hidden{"lastState:"};
  856. my $timeDiff = int(gettimeofday())-$hidden{"lastTime:"};
  857. $hidden{$lastState.":"} += $timeDiff;
  858. $hidden{$state."_Count:"}++
  859. if $state ne $lastState;
  860. }
  861. $hidden{"lastState:"} = $state;
  862. $hidden{"lastTime:"} = int(gettimeofday());
  863. # Prepare new current reading, delete hidden reading if it is used again
  864. $result = "";
  865. foreach my $key (sort keys %hidden) {
  866. if ($key !~ /^(lastState|lastTime|showDate|\(since):$/) {
  867. #Store current value for single readings
  868. if ($key !~ /_Count:$/) {
  869. $stat{$key} = statistics_FormatDuration($hidden{$key});
  870. }
  871. else {
  872. $stat{$key} = $hidden{$key};
  873. }
  874. # Create current summary reading
  875. $result .= " " if $result;
  876. $result .= "$key ".$stat{$key};
  877. delete $hidden{$key} if $saveLast; # Reset hidden reading if period changed
  878. }
  879. }
  880. if ($hidden{"showDate:"} == 1) { $result .= " (since: ".$hidden{"(since:"}; }
  881. # Store current summary reading as last reading, create new summary reading
  882. if ($saveLast) {
  883. readingsBulkUpdate($dev, $statReadingName . "Last", $result, 1);
  884. statistics_Log $hash, 4, "Set '".$statReadingName . "Last = $result'";
  885. $result = $state.": 00:00:00 ".$state."_Count: 1";
  886. $hidden{$state.":"} = 0;
  887. $hidden{$state."_Count:"} = 1;
  888. $hidden{"showDate:"} = 0;
  889. }
  890. # Store current reading
  891. readingsBulkUpdate($dev, $statReadingName, $result, 0);
  892. statistics_Log $hash, 5, "Set '$statReadingName = $result'";
  893. # Store single readings
  894. my $singularReadings = AttrVal($name, "singularReadings", "");
  895. $singularReadings=~s/\n|\r//g;
  896. if ($singularReadings ne "") {
  897. # Do this for each state of the current or last period
  898. while (my ($statKey, $statValue) = each(%stat) ) {
  899. chop ($statKey);
  900. unless ($saveLast) {
  901. # statistics_storeSingularReadings
  902. # $hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast
  903. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,$statKey,$period,$statValue,0,0);
  904. }
  905. else {
  906. my $newValue = 0;
  907. $newValue = "00:00:00" if $statKey !~ /_Count:$/;
  908. $newValue = $hidden{$statKey} if defined $hidden{$statKey};
  909. # statistics_storeSingularReadings
  910. # $hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast
  911. statistics_storeSingularReadings ($name,$singularReadings,$dev,$statReadingName,$readingName,$statKey,$period,$newValue,$statValue,$saveLast);
  912. }
  913. }
  914. }
  915. # Store hidden reading
  916. $result = "";
  917. while ( my ($key, $duration) = each(%hidden) ) {
  918. $result .= " " if $result;
  919. $result .= "$key $duration";
  920. }
  921. readingsSingleUpdate($hash, $hiddenReadingName, $result, 0);
  922. statistics_Log $hash, 5, "Set '$hiddenReadingName = $result'";
  923. return;
  924. }
  925. ####################
  926. sub statistics_storeSingularReadings ($$$$$$$$$$)
  927. {
  928. my ($hashName,$singularReadings,$dev,$statReadingName,$readingName,$statType,$period,$statValue,$lastValue,$saveLast) = @_;
  929. return if $singularReadings eq "";
  930. if ($statType =~ /Delta|Tendency/) { $statReadingName .= $period; }
  931. else { $statReadingName .= $statType; }
  932. my $devName=$dev->{NAME};
  933. if ("$devName:$readingName:$statType:$period" =~ /^($singularReadings)$/) {
  934. readingsBulkUpdate($dev, $statReadingName, $statValue, 1);
  935. statistics_Log $hashName, 5, "Set ".$statReadingName." = $statValue"; # Fehler um 24 Uhr
  936. if ($saveLast) {
  937. readingsBulkUpdate($dev, $statReadingName."Last", $lastValue, 1);
  938. statistics_Log $hashName, 5, "Set ".$statReadingName."Last = $lastValue";
  939. }
  940. }
  941. }
  942. ####################
  943. sub statistics_getStoredDevices ($)
  944. {
  945. my ($hash) = @_;
  946. my $result="";
  947. foreach my $r (sort keys %{$hash->{READINGS}}) {
  948. if ($r =~ /^\.(.*):.*/) {
  949. my $device = $1;
  950. my $regExp = '^'.$1.'$|^'.$1.',|,'.$1.'$|,'.$1.',';
  951. if ($result !~ /$regExp/) {
  952. $result.="," . $device;
  953. }
  954. }
  955. }
  956. return $result;
  957. }
  958. ########################################
  959. sub statistics_FormatDuration($)
  960. {
  961. my ($value) = @_;
  962. #Tage
  963. my $returnstr ="";
  964. if ($value > 86400) { $returnstr = sprintf "%dd ", int($value/86400); }
  965. # Stunden
  966. if ($value == 86400) {
  967. $returnstr = "24:00:00";
  968. } else {
  969. $value %= 86400;
  970. $returnstr .= sprintf "%02d:", int($value/3600);
  971. $value %= 3600;
  972. $returnstr .= sprintf "%02d:", int($value/60);
  973. $value %= 60;
  974. $returnstr .= sprintf "%02d", $value;
  975. }
  976. return $returnstr;
  977. }
  978. ########################################
  979. sub statistics_maxDecPlaces($$)
  980. {
  981. my ($value, $decMax) = @_;
  982. $decMax = 0 if ! defined $decMax;
  983. if ( $value =~ /.*\.(.*)/ ) {
  984. my $decPlaces = length($1);
  985. $decMax = $decPlaces >= $decMax ? $decPlaces : $decMax;
  986. }
  987. return $decMax;
  988. }
  989. ########################################
  990. sub statistics_UpdateDevReading($$$$)
  991. {
  992. my ($dev, $rname, $val, $event) = @_;
  993. $dev->{READINGS}{$rname}{VAL} = $val;
  994. $dev->{READINGS}{$rname}{TIME} = TimeNow();
  995. if ($event==1) {
  996. if (exists ($dev->{CHANGED})) {
  997. my $max = int(@{$dev->{CHANGED}});
  998. $dev->{CHANGED}[$max] = "$rname: $val";
  999. }
  1000. } else {
  1001. readingsBulkUpdate($dev, $rname, $val, 1);
  1002. }
  1003. }
  1004. ##########################
  1005. 1;
  1006. =pod
  1007. =begin html
  1008. =item helper
  1009. =item summary Calculates for statistical values and adds them to the devices.
  1010. =item summary_DE Berechnet statistische Werte und f&uuml;gt sie dem Ger&auml;t hinzu.
  1011. <a name="statistics"></a>
  1012. <h3>statistics</h3>
  1013. (en | <a href="http://fhem.de/commandref_DE.html#statistics">de</a>)
  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. (<a href="http://fhem.de/commandref.html#statistics">en</a> | de)
  1174. <div>
  1175. <ul>
  1176. 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.
  1177. <br>
  1178. 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.
  1179. <br>&nbsp;
  1180. <br>
  1181. Es unterscheidet in vier Statistik-Typen denen bereits standardm&auml;ssig Ger&auml;tewerte zugeordnet sind:
  1182. <ul>
  1183. <li><b>Min|Avg|Max</b> Minimum, Durchschnitt und Maximum von Momentanwerten:
  1184. <br>
  1185. &uuml;ber den Zeitraum Tag, Monat und Jahr:
  1186. <br>
  1187. <i>brightness, current, energy_current, humidity, luminosity, temperature, voltage</i>
  1188. <br>
  1189. &uuml;ber den Zeitraum Stunde, Tag, Monat und Jahr:
  1190. <br>
  1191. <i>wind, wind_speed, windSpeed</i>
  1192. </li><br>
  1193. <li><b>Tendency</b> Tendenz &uuml;ber 1h, 2h, 3h und 6h: <i>pressure</i>
  1194. </li><br>
  1195. <li><b>Delta</b> Differenz zwischen Anfangs- und Endwerte innerhalb eines Zeitraums (Stunde, Tag, Monat, Jahr):
  1196. <br>
  1197. <i>count, energy, energy_total, power, total, rain, rain_rate, rain_total</i>
  1198. </li><br>
  1199. <li><b>Duration</b> Dauer und Anzahl der Zust&auml;nde (on, off, open, closed...) innerhalb eines Zeitraums (Tag, Monat, Jahr):
  1200. <br>
  1201. <i>lightsensor, lock, motion, Window, window, state (wenn kein anderer Ger&auml;tewert g&uuml;ltig)</i>
  1202. </li><br>
  1203. </ul>
  1204. &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
  1205. einem anderen Statistik-Typ zugeordnet werden.
  1206. <br>&nbsp;
  1207. <br>
  1208. <b>Define</b>
  1209. <ul>
  1210. <br>
  1211. <code>define &lt;Name&gt; statistics &lt;Ger&auml;teNameRegExp&gt; [Prefix]</code>
  1212. <br>
  1213. Beispiel: <code>define Statistik statistics Wettersensor|Badsensor</code>
  1214. <br>&nbsp;
  1215. <li><code>&lt;Ger&auml;teNameRegExp&gt;</code>
  1216. <br>
  1217. Regul&auml;rer Ausdruck f&uuml;r den Ger&auml;tenamen. <b>!!! Nicht die Ger&auml;tewerte !!!</b>
  1218. </li><br>
  1219. <li><code>[Prefix]</code>
  1220. <br>
  1221. Optional. Der Prefix wird vor den Namen der statistischen Ger&auml;tewerte gesetzt. Standardm&auml;ssig <i>stat</i>
  1222. </li><br>
  1223. </ul>
  1224. <br>
  1225. <b>Set</b>
  1226. <ul>
  1227. <br>
  1228. <li><code>resetStatistics &lt;All|Ger&auml;tename&gt;</code>
  1229. <br>
  1230. Setzt die Statistiken der ausgew&auml;hlten Ger&auml;te zur&uuml;ck.
  1231. </li><br>
  1232. <li><code>doStatistics</code>
  1233. <br>
  1234. Berechnet die aktuellen Statistiken aller beobachteten Ger&auml;te.
  1235. </li><br>
  1236. </ul>
  1237. <br>
  1238. <b>Get</b>
  1239. <ul>nicht implementiert
  1240. </ul>
  1241. <br>
  1242. <a name="statisticsattr"></a>
  1243. <b>Attributes</b>
  1244. <ul>
  1245. <br>
  1246. <li><code>dayChangeTime &lt;Zeit&gt;</code>
  1247. <br>
  1248. Uhrzeit des Tageswechsels. Standardm&auml;ssig 00:00. Bei Wetterdaten kann der Tageswechsel z.B. auf 6:50 gesetzt werden.
  1249. </li><br>
  1250. <li><code>deltaReadings &lt;Ger&auml;tewerte&gt;</code>
  1251. <br>
  1252. 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.
  1253. </li><br>
  1254. <li><code>durationPeriodHour &lt; 1 | 0 &gt;</code>
  1255. <br>
  1256. Wenn auf 1 gesetzt, dann werden f&uuml;r "durationReadings" auch st&uuml;ndliche Statistiken gebildet.
  1257. </li><br>
  1258. <li><code>durationReadings &lt;Ger&auml;tewerte&gt;</code>
  1259. <br>
  1260. 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.
  1261. </li><br>
  1262. <li><code>excludedReadings &lt;Ger&auml;tenameRegExp:Ger&auml;tewertRegExp&gt;</code>
  1263. <br>
  1264. Regul&auml;rer Ausdruck der Ger&auml;tewerte die nicht ausgewertet werden sollen.
  1265. z.B. <code>FritzDect:current|Sensor_.*:humidity</code>
  1266. <br>
  1267. </li><br>
  1268. <li><code>ignoreDefaultAssignments <code>&lt;0 | 1&gt;</code></code>
  1269. <br>
  1270. Ignoriert die Standardzuordnung von Ger&auml;tewerten zu Statistiktypen..<br>
  1271. D.h., nur die Ger&auml;tewerte, die &uuml;ber Attribute den Statistiktypen zugeordnet sind, werden ausgewertet.
  1272. <br>
  1273. </li><br>
  1274. <li><code>hideAllSummaryReadings &lt;0 | 1&gt;</code>
  1275. <br>
  1276. noch nicht implementiert - Es werden keine gesammelten Statistiken angezeigt, sondern nur die unter "singularReadings" definierten Einzelwerte
  1277. </li><br>
  1278. <li><code>minAvgMaxReadings &lt;Ger&auml;tewerte&gt;</code>
  1279. <br>
  1280. 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.
  1281. </li><br>
  1282. <li><code>periodChangePreset &lt;Sekunden&gt;</code>
  1283. <br>
  1284. Start der Berechnung der periodischen Daten, standardm&auml;ssig 5 Sekunden vor der vollen Stunde,
  1285. <br>
  1286. Erlaubt die korrekte zeitliche Zuordnung in Plots, kann je nach Systemauslastung verringert oder vergr&ouml;&szlig;ert werden.
  1287. <br>
  1288. </li><br>
  1289. <li><code>singularReadings &lt;Ger&auml;tRegExp:Ger&auml;teWertRegExp:Statistiktyp:Zeitraum&gt;</code>
  1290. <ul>
  1291. <li>Statistiktyp: Min|Avg|Max|Delta|<i>DurationState</i>|<span style="color:blue;">Tendency</span></li>
  1292. <li>Zeitraum: Hour|Day|Month|Year|<span style="color:blue;">1h|2h|3h|6h</span></li>
  1293. </ul>
  1294. Regul&auml;rer Ausdruck statistischer Werte, die <u>zus&auml;tzlich</u> auch als einzelne Werte gespeichert werden sollen.
  1295. Erleichtert die Erzeugung von Plots und anderer Auswertungen (notify).
  1296. <br>
  1297. F&uuml;r "duration"-Ger&auml;tewerte muss der Name des jeweiligen Statuswertes als <code>Statistiktyp</code> eingesetzt werden.
  1298. <dt>Beispiel:</dt>
  1299. <dd>
  1300. <code>Wettersensor:rain:Delta:(Hour|Day)|FritzDect:power:Delta:Day</code>
  1301. <br>
  1302. <code>Wettersensor:rain:Delta:(Hour|Day)|FritzDect:power:Delta:Day</code>
  1303. </dd>
  1304. </li><br>
  1305. <li><code>specialDeltaPeriods &lt;Ger&auml;t:Ger&auml;tewert:Zeitraum:Anzahl1:Anzahl2:...&gt;</code>
  1306. <br>
  1307. Erzeugt f&uuml;r die angegebenen "delta"-Ger&auml;tewerte zus&auml;tzliche Einzelwerte &uuml;ber die angegebene Anzahl eines Zeitraums (Hour, Day, Month).
  1308. <br>
  1309. 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.
  1310. <br>
  1311. <dt>Beispiel:</dt>
  1312. <dd>
  1313. <code>attr Statistik specialDeltaPeriods Wettersensor:rain:Hour:06:72:96</code>
  1314. <br>
  1315. Dies erzeugt 3 zus&auml;tzliche Werte f&uuml;r die Regenmenge in den letzten 6, 72, 96 Stunden.
  1316. <br>
  1317. <code>attr Statistik specialDeltaPeriods Wettersensor:rain:Hour:48,Wettersensor:rain:Day:30,EZaehler:energy:Month:6:12</code>
  1318. <br>
  1319. 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.
  1320. </dd>
  1321. </li><br>
  1322. <li><code>specialDeltaPeriodHours</code>
  1323. <br>
  1324. veraltet
  1325. </li><br>
  1326. <li><code>tendencyReadings &lt;Ger&auml;tewerte&gt;</code>
  1327. <br>
  1328. 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.
  1329. </li><br>
  1330. <li><a href="#readingFnAttributes">readingFnAttributes</a>
  1331. </li><br>
  1332. </ul>
  1333. </ul>
  1334. </div>
  1335. =end html_DE
  1336. =cut