98_statistics.pm 59 KB

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