98_RandomTimer.pm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. # $Id: 98_RandomTimer.pm 16005 2018-01-27 06:05:51Z igami $
  2. ##############################################################################
  3. #
  4. # 98_RandomTimer_Initialize.pm
  5. # written by Dietmar Ortmann
  6. # Maintained by igami since 02-2018
  7. #
  8. # This file is part of fhem.
  9. #
  10. # Fhem is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # Fhem is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. # 10.09.2013 Svenson : disable direct if attribute changed, add state disabled;
  25. # randomtimer run every day if attribut runonce 0 (default is 1)
  26. #
  27. ##############################################################################
  28. package main;
  29. use strict;
  30. use warnings;
  31. no if $] >= 5.017011, warnings => 'experimental::smartmatch';
  32. # use IO::Socket;
  33. use Time::HiRes qw(gettimeofday);
  34. use Time::Local 'timelocal_nocheck';
  35. sub RandomTimer_stopTimeReached($);
  36. sub RandomTimer_schaltZeitenErmitteln ($$);
  37. sub RandomTimer_setState($);
  38. sub RandomTimer_Initialize($)
  39. {
  40. my ($hash) = @_;
  41. if(!$modules{Twilight}{LOADED} && -f "$attr{global}{modpath}/FHEM/59_Twilight.pm") {
  42. my $ret = CommandReload(undef, "59_Twilight");
  43. Log3 undef, 1, $ret if($ret);
  44. }
  45. $hash->{DefFn} = "RandomTimer_Define";
  46. $hash->{UndefFn} = "RandomTimer_Undef";
  47. $hash->{AttrFn} = "RandomTimer_Attr";
  48. $hash->{AttrList} = "onCmd offCmd switchmode disable:0,1 disableCond runonce:0,1 keepDeviceAlive forceStoptimeSameDay ".
  49. $readingFnAttributes;
  50. }
  51. ########################################################################
  52. sub RandomTimer_Undef($$) {
  53. my ($hash, $arg) = @_;
  54. myRemoveInternalTimer("SetTimer", $hash);
  55. myRemoveInternalTimer("Exec", $hash);
  56. delete $modules{RandomTimer}{defptr}{$hash->{NAME}};
  57. return undef;
  58. }
  59. ########################################################################
  60. sub RandomTimer_Define($$)
  61. {
  62. my ($hash, $def) = @_;
  63. RemoveInternalTimer($hash);
  64. my ($name, $type, $timespec_start, $device, $timespec_stop, $timeToSwitch) =
  65. split("[ \t][ \t]*", $def);
  66. return "wrong syntax: define <name> RandomTimer <timespec_start> <device> <timespec_stop> <timeToSwitch>"
  67. if(!defined $timeToSwitch);
  68. return "Wrong timespec_start <$timespec_start>, use \"[+][*]<time or func>\""
  69. if($timespec_start !~ m/^(\+)?(\*)?(.*)$/i);
  70. my ($rel, $rep, $tspec) = ($1, $2, $3);
  71. my ($err, $hr, $min, $sec, $fn) = GetTimeSpec($tspec);
  72. return $err if($err);
  73. $rel = "" if(!defined($rel));
  74. $rep = "" if(!defined($rep));
  75. return "Wrong timespec_stop <$timespec_stop>, use \"[+][*]<time or func>\""
  76. if($timespec_stop !~ m/^(\+)?(\*)?(.*)$/i);
  77. my ($srel, $srep, $stspec) = ($1, $2, $3);
  78. my ($e, $h, $m, $s, $f) = GetTimeSpec($stspec);
  79. return $e if($e);
  80. return "invalid timeToSwitch <$timeToSwitch>, use 9999"
  81. if(!($timeToSwitch =~ m/^[0-9]{2,4}$/i));
  82. RandomTimer_setSwitchmode ($hash, "800/200") if (!defined $hash->{helper}{SWITCHMODE});
  83. $hash->{NAME} = $name;
  84. $hash->{DEVICE} = $device;
  85. $hash->{helper}{TIMESPEC_START} = $timespec_start;
  86. $hash->{helper}{TIMESPEC_STOP} = $timespec_stop;
  87. $hash->{helper}{TIMETOSWITCH} = $timeToSwitch;
  88. $hash->{helper}{REP} = $rep;
  89. $hash->{helper}{REL} = $rel;
  90. $hash->{helper}{S_REP} = $srep;
  91. $hash->{helper}{S_REL} = $srel;
  92. $hash->{COMMAND} = Value($hash->{DEVICE});
  93. #$attr{$name}{verbose} = 4;
  94. readingsSingleUpdate ($hash, "TimeToSwitch", $hash->{helper}{TIMETOSWITCH}, 1);
  95. myRemoveInternalTimer("SetTimer", $hash);
  96. myInternalTimer ("SetTimer", time(), "RandomTimer_SetTimer", $hash, 0);
  97. return undef;
  98. }
  99. ########################################################################
  100. sub RandomTimer_SetTimer($) {
  101. my ($myHash) = @_;
  102. my $hash = myGetHashIndirekt($myHash, (caller(0))[3]);
  103. return if (!defined($hash));
  104. my $now = time();
  105. my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($now);
  106. RandomTimer_setActive($hash, 0);
  107. RandomTimer_schaltZeitenErmitteln($hash, $now);
  108. RandomTimer_setState($hash);
  109. Log3 $hash, 4, "[".$hash->{NAME}."]" . " timings RandomTimer on $hash->{DEVICE}: "
  110. . strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - "
  111. . strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime}));
  112. my $secToMidnight = 24*3600 -(3600*$hour + 60*$min + $sec);
  113. my $setExecTime = max($now, $hash->{helper}{startTime});
  114. myRemoveInternalTimer("Exec", $hash);
  115. myInternalTimer ("Exec", $setExecTime, "RandomTimer_Exec", $hash, 0);
  116. if ($hash->{helper}{REP} gt "") {
  117. my $setTimerTime = max($now+$secToMidnight + 15,
  118. $hash->{helper}{stopTime}) + $hash->{helper}{TIMETOSWITCH}+15;
  119. myRemoveInternalTimer("SetTimer", $hash);
  120. myInternalTimer ("SetTimer", $setTimerTime, "RandomTimer_SetTimer", $hash, 0);
  121. }
  122. }
  123. ########################################################################
  124. # define Test RandomTimer +00:00:05 Brunnen +00:05:00 60 ;
  125. # attr Test room RandomTimerX ;
  126. # attr Test verbose 5 ;
  127. # define ds at +00:00:30 attr Test disable 1 ;
  128. #
  129. # delete RT_Test; define RT_Test RandomTimer *22:00:00 Brunnen 23:00:00 300
  130. # set RT_Test disable 0
  131. # delete RT_Test
  132. sub RandomTimer_Exec($) {
  133. my ($myHash) = @_;
  134. my $hash = myGetHashIndirekt($myHash, (caller(0))[3]);
  135. return if (!defined($hash));
  136. my $now = time();
  137. # Wenn aktiv aber disabled, dann timer abschalten, Meldung ausgeben.
  138. my $active = RandomTimer_isAktive($hash);
  139. my $disabled = RandomTimer_isDisabled($hash);
  140. my $stopTimeReached = RandomTimer_stopTimeReached($hash);
  141. if ($active) {
  142. # wenn temporär ausgeschaltet
  143. if ($disabled) {
  144. Log3 $hash, 3, "[".$hash->{NAME}."]"." ending RandomTimer on $hash->{DEVICE}: "
  145. . strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - "
  146. . strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime}));
  147. RandomTimer_down($hash);
  148. RandomTimer_setActive($hash,0);
  149. RandomTimer_setState ($hash);
  150. }
  151. # Wenn aktiv und Abschaltzeit erreicht, dann Gerät ausschalten, Meldung ausgeben und Timer schließen
  152. if ($stopTimeReached) {
  153. Log3 $hash, 3, "[".$hash->{NAME}."]"." ending RandomTimer on $hash->{DEVICE}: "
  154. . strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - "
  155. . strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime}));
  156. RandomTimer_down($hash);
  157. RandomTimer_setActive($hash, 0);
  158. if ( AttrVal($hash->{NAME}, "runonce", -1) eq 1 ) {
  159. Log 3, "[".$hash->{NAME}. "]" ."runonceMode";
  160. fhem ("delete $hash->{NAME}") ;
  161. }
  162. RandomTimer_setState($hash);
  163. return;
  164. }
  165. } else { # !active
  166. if ($disabled) {
  167. Log3 $hash, 4, "[".$hash->{NAME}. "] RandomTimer on $hash->{DEVICE} timer disabled - no switch";
  168. RandomTimer_setState($hash);
  169. RandomTimer_setActive($hash,0);
  170. }
  171. if ($stopTimeReached) {
  172. Log3 $hash, 4, "[".$hash->{NAME}."]"." definition RandomTimer on $hash->{DEVICE}: "
  173. . strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - "
  174. . strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime}));
  175. RandomTimer_setState ($hash);
  176. RandomTimer_setActive($hash,0);
  177. return;
  178. }
  179. if (!$disabled) {
  180. if ($now>$hash->{helper}{startTime} && $now<$hash->{helper}{stopTime}) {
  181. Log3 $hash, 3, "[".$hash->{NAME}."]"." starting RandomTimer on $hash->{DEVICE}: "
  182. . strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - "
  183. . strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime}));
  184. RandomTimer_setActive($hash,1);
  185. }
  186. }
  187. }
  188. RandomTimer_setState($hash);
  189. if ($now>$hash->{helper}{startTime} && $now<$hash->{helper}{stopTime}) {
  190. RandomTimer_device_toggle($hash) if (!$disabled);
  191. }
  192. my $nextSwitch = time() + RandomTimer_getSecsToNextAbschaltTest($hash);
  193. myRemoveInternalTimer("Exec", $hash);
  194. myInternalTimer ("Exec", $nextSwitch, "RandomTimer_Exec", $hash, 0);
  195. }
  196. ########################################################################
  197. sub RandomTimer_stopTimeReached($) {
  198. my ($hash) = @_;
  199. return ( time()>$hash->{helper}{stopTime} );
  200. }
  201. ########################################################################
  202. sub RandomTimer_setActive($$) {
  203. my ($hash, $value) = @_;
  204. $hash->{helper}{active} = $value;
  205. my $trigger = (RandomTimer_isDisabled($hash)) ? 0 : 1;
  206. readingsSingleUpdate ($hash, "active", $value, $trigger);
  207. }
  208. ########################################################################
  209. sub RandomTimer_isAktive ($) {
  210. my ($hash) = @_;
  211. return defined ($hash->{helper}{active}) ? $hash->{helper}{active} : 0;
  212. }
  213. ########################################################################
  214. sub RandomTimer_down($) {
  215. my ($hash) = @_;
  216. $hash->{COMMAND} = AttrVal($hash->{NAME}, "keepDeviceAlive", 0) ? "on" : "off";
  217. RandomTimer_device_switch($hash);
  218. }
  219. ########################################################################
  220. sub RandomTimer_setState($) {
  221. my ($hash) = @_;
  222. if (RandomTimer_isDisabled($hash)) {
  223. #$hash->{STATE} = "disabled";
  224. readingsSingleUpdate ($hash, "state", "disabled", 0);
  225. } else {
  226. my $state = $hash->{helper}{active} ? "on" : "off";
  227. readingsSingleUpdate ($hash, "state", $state, 1);
  228. }
  229. }
  230. ########################################################################
  231. sub RandomTimer_Attr($$$) {
  232. my ($cmd, $name, $attrName, $attrVal) = @_;
  233. my $hash = $defs{$name};
  234. if( $attrName ~~ ["switchmode"] ) {
  235. RandomTimer_setSwitchmode($hash, $attrVal);
  236. }
  237. if( $attrName ~~ ["disable","disableCond"] ) {
  238. # Schaltung vorziehen, damit bei einem disable abgeschaltet wird.
  239. myRemoveInternalTimer("Exec", $hash);
  240. myInternalTimer ("Exec", time()+1, "RandomTimer_Exec", $hash, 0);
  241. }
  242. return undef;
  243. }
  244. ########################################################################
  245. sub RandomTimer_setSwitchmode ($$) {
  246. my ($hash, $attrVal) = @_;
  247. my $mod = "[".$hash->{NAME} ."] ";
  248. if(!($attrVal =~ m/^([0-9]{1,3})\/([0-9]{1,3})$/i)) {
  249. Log3 undef, 3, $mod . "invalid switchMode <$attrVal>, use 999/999";
  250. } else {
  251. my ($sigmaWhenOff, $sigmaWhenOn) = ($1, $2);
  252. $hash->{helper}{SWITCHMODE} = $attrVal;
  253. $hash->{helper}{SIGMAWHENON} = $sigmaWhenOn;
  254. $hash->{helper}{SIGMAWHENOFF} = $sigmaWhenOff;
  255. $attr{$hash->{NAME}}{switchmode} = $attrVal;
  256. }
  257. }
  258. ########################################################################
  259. sub RandomTimer_getSecsToNextAbschaltTest($)
  260. {
  261. my ($hash) = @_;
  262. my $intervall = $hash->{helper}{TIMETOSWITCH};
  263. my $proz = 10;
  264. my $delta = $intervall * $proz/100;
  265. my $nextSecs = $intervall - $delta/2 + int(rand($delta));
  266. return $nextSecs;
  267. }
  268. ########################################################################
  269. sub RandomTimer_schaltZeitenErmitteln ($$) {
  270. my ($hash,$now) = @_;
  271. RandomTimer_startZeitErmitteln($hash, $now);
  272. RandomTimer_stopZeitErmitteln ($hash, $now);
  273. readingsBeginUpdate($hash);
  274. readingsBulkUpdate ($hash, "Startzeit", FmtDateTime($hash->{helper}{startTime}));
  275. readingsBulkUpdate ($hash, "Stoppzeit", FmtDateTime($hash->{helper}{stopTime}));
  276. readingsEndUpdate ($hash, defined($hash->{LOCAL} ? 0 : 1));
  277. }
  278. ########################################################################
  279. sub RandomTimer_zeitBerechnen ($$$$) {
  280. my ($now, $hour, $min, $sec) = @_;
  281. my @jetzt_arr = localtime($now);
  282. #Stunden Minuten Sekunden
  283. $jetzt_arr[2] = $hour; $jetzt_arr[1] = $min; $jetzt_arr[0] = $sec;
  284. my $next = timelocal_nocheck(@jetzt_arr);
  285. return $next;
  286. }
  287. ########################################################################
  288. sub RandomTimer_addDays ($$) {
  289. my ($now, $days) = @_;
  290. my @jetzt_arr = localtime($now);
  291. $jetzt_arr[3] += $days;
  292. my $next = timelocal_nocheck(@jetzt_arr);
  293. return $next;
  294. }
  295. ########################################################################
  296. sub RandomTimer_startZeitErmitteln ($$) {
  297. my ($hash,$now) = @_;
  298. my $timespec_start = $hash->{helper}{TIMESPEC_START};
  299. return "Wrong timespec_start <$timespec_start>, use \"[+][*]<time or func>\""
  300. if($timespec_start !~ m/^(\+)?(\*)?(.*)$/i);
  301. my ($rel, $rep, $tspec) = ($1, $2, $3);
  302. my ($err, $hour, $min, $sec, $fn) = GetTimeSpec($tspec);
  303. return $err if($err);
  304. my $startTime;
  305. if($rel) {
  306. $startTime = $now + 3600* $hour + 60* $min + $sec;
  307. } else {
  308. $startTime = RandomTimer_zeitBerechnen($now, $hour, $min, $sec);
  309. }
  310. $hash->{helper}{startTime} = $startTime;
  311. $hash->{helper}{STARTTIME} = strftime("%d.%m.%Y %H:%M:%S",localtime($startTime));
  312. }
  313. ########################################################################
  314. sub RandomTimer_stopZeitErmitteln ($$) {
  315. my ($hash,$now) = @_;
  316. my $timespec_stop = $hash->{helper}{TIMESPEC_STOP};
  317. return "Wrong timespec_stop <$timespec_stop>, use \"[+][*]<time or func>\""
  318. if($timespec_stop !~ m/^(\+)?(\*)?(.*)$/i);
  319. my ($rel, $rep, $tspec) = ($1, $2, $3);
  320. my ($err, $hour, $min, $sec, $fn) = GetTimeSpec($tspec);
  321. return $err if($err);
  322. my $stopTime;
  323. if($rel) {
  324. $stopTime = $hash->{helper}{startTime} + 3600* $hour + 60* $min + $sec;
  325. } else {
  326. $stopTime = RandomTimer_zeitBerechnen($now, $hour, $min, $sec);
  327. }
  328. if (!AttrVal($hash->{NAME}, "forceStoptimeSameDay", 0)) {
  329. if ($hash->{helper}{startTime} > $stopTime) {
  330. $stopTime = RandomTimer_addDays($stopTime, 1);
  331. }
  332. }
  333. $hash->{helper}{stopTime} = $stopTime;
  334. $hash->{helper}{STOPTIME} = strftime("%d.%m.%Y %H:%M:%S",localtime($stopTime));
  335. }
  336. ########################################################################
  337. sub RandomTimer_device_toggle ($) {
  338. my ($hash) = @_;
  339. my $status = Value($hash->{DEVICE});
  340. if ($status ne "on" && $status ne "off" ) {
  341. Log3 $hash, 3, "[".$hash->{NAME}."]"." result of function Value($hash->{DEVICE}) must be 'on' or 'off'";
  342. }
  343. my $sigma = ($status eq "on")
  344. ? $hash->{helper}{SIGMAWHENON}
  345. : $hash->{helper}{SIGMAWHENOFF};
  346. my $zufall = int(rand(1000));
  347. Log3 $hash, 4, "[".$hash->{NAME}."]"." IstZustand:$status sigmaWhen-$status:$sigma random:$zufall<$sigma=>" . (($zufall < $sigma)?"true":"false");
  348. if ($zufall < $sigma ) {
  349. $hash->{COMMAND} = ($status eq "on") ? "off" : "on";
  350. RandomTimer_device_switch($hash);
  351. }
  352. }
  353. ########################################################################
  354. sub RandomTimer_device_switch ($) {
  355. my ($hash) = @_;
  356. my $command = "set @ $hash->{COMMAND}";
  357. if ($hash->{COMMAND} eq "on") {
  358. $command = AttrVal($hash->{NAME}, "onCmd", $command);
  359. } else {
  360. $command = AttrVal($hash->{NAME}, "offCmd", $command);
  361. }
  362. $command =~ s/@/$hash->{DEVICE}/g;
  363. $command = SemicolonEscape($command);
  364. Log3 $hash, 4, "[".$hash->{NAME}. "]"." command: $command";
  365. my $ret = AnalyzeCommandChain(undef, $command);
  366. Log3 ($hash, 3, "[$hash->{NAME}] ERROR: " . $ret . " SENDING " . $command) if($ret);
  367. #Log3 $hash, 3, "[$hash->{NAME}] Value($hash->{COMMAND})=".Value($hash->{DEVICE});
  368. #$hash->{"$hash->{COMMAND}Value"} = Value($hash->{DEVICE});
  369. }
  370. ########################################################################
  371. sub RandomTimer_isDisabled($) {
  372. my ($hash) = @_;
  373. my $disable = AttrVal($hash->{NAME}, "disable", 0 );
  374. return $disable if($disable);
  375. my $disableCond = AttrVal($hash->{NAME}, "disableCond", "nf" );
  376. if ($disableCond eq "nf") {
  377. return 0;
  378. } else {
  379. $disable = eval ($disableCond);
  380. if ($@) {
  381. $@ =~ s/\n/ /g;
  382. Log3 ($hash, 3, "[$hash->{NAME}] ERROR: " . $@ . " EVALUATING " . $disableCond);
  383. }
  384. return $disable;
  385. }
  386. }
  387. ########################################################################
  388. sub RandomTimer_Wakeup() { # {RandomTimer_Wakeup()}
  389. foreach my $hc ( sort keys %{$modules{RandomTimer}{defptr}} ) {
  390. my $hash = $modules{RandomTimer}{defptr}{$hc};
  391. my $myHash->{HASH}=$hash;
  392. RandomTimer_SetTimer($myHash);
  393. Log3 undef, 3, "RandomTimer_Wakeup() for $hash->{NAME} done!";
  394. }
  395. Log3 undef, 3, "RandomTimer_Wakeup() done!";
  396. }
  397. 1;
  398. =pod
  399. =item device
  400. =item summary imitates the random switch functionality of a timer clock (FS20 ZSU)
  401. =item summary_DE bildet die Zufallsfunktion einer Zeitschaltuhr nach
  402. =begin html
  403. <a name="RandomTimer"></a>
  404. <h1>RandomTimer</h1>
  405. <h2>Define</h2>
  406. <ul>
  407. <code><font size="+2">define &lt;name&gt; RandomTimer &lt;timespec_start&gt; &lt;device&gt; &lt;timespec_stop&gt; [&lt;timeToSwitch&gt;]</font></code><br>
  408. <br>
  409. Defines a device, that imitates the random switch functionality of a timer clock, like a <b>FS20 ZSU</b>.
  410. The idea to create it, came from the problem, that is was always a little bit tricky to install a timer clock before
  411. holiday: finding the manual, testing it the days before and three different timer clocks with three different manuals - a horror.<br>
  412. By using it in conjunction with a dummy and a <a href="#disableCond">disableCond</a>, I'm able to switch the always defined timer on every weekend easily from all over the world.
  413. <br><br>
  414. <h3>Descrition</h3>
  415. a RandomTimer device starts at timespec_start switching device. Every (timeToSwitch
  416. seconds +-10%) it trys to switch device on/off. The switching period stops when the
  417. next time to switch is greater than timespec_stop.
  418. <br><br>
  419. </ul>
  420. <h3>Parameter</h3>
  421. <ul>
  422. <b>timespec_start</b>
  423. <br>
  424. The parameter <b>timespec_start</b> defines the start time of the timer with format: HH:MM:SS.
  425. It can be a Perlfunction as known from the <a href="#at">at</a> timespec &nbsp;.
  426. <br><br>
  427. <b>device</b>
  428. <br>
  429. The parameter <b>device</b> defines the fhem device that should be switched.
  430. <br><br>
  431. <b>timespec_stop</b>
  432. <br>
  433. The parameter <b>timespec_stop</b> defines the stop time of the timer with format: HH:MM:SS.
  434. It can be a Perlfunction as known from the timespec <a href="#at">at</a> &nbsp;.
  435. <br><br>
  436. <b>timeToSwitch</b>
  437. <br>
  438. The parameter <b>timeToSwitch</b> defines the time in seconds between two on/off switches.
  439. <br><br>
  440. </ul>
  441. <h3>Examples</h3>
  442. <ul>
  443. <li>
  444. <code>define ZufallsTimerTisch RandomTimer *{sunset_abs()} StehlampeTisch +03:00:00 500</code><br>
  445. defines a timer that starts at sunset an ends 3 hous later. The timer trys to switch every 500 seconds(+-10%).
  446. </li><br><br>
  447. <li>
  448. <code>define ZufallsTimerTisch RandomTimer *{sunset_abs()} StehlampeTisch *{sunset_abs(3*3600)} 480</code><br>
  449. defines a timer that starts at sunset and stops after sunset + 3 hours. The timer trys to switch every 480 seconds(+-10%).
  450. </li><br><br>
  451. <li>
  452. <code>define ZufallsTimerTisch RandomTimer *{sunset_abs()} StehlampeTisch 22:30:00 300</code><br>
  453. defines a timer that starts at sunset an ends at 22:30. The timer trys to switch every 300 seconds(+-10%).
  454. </li><br><br>
  455. </ul>
  456. <!-- -------------------------------------------------------------------------- -->
  457. <!-- Set ------------------------------------------------------------------ -->
  458. <!-- -------------------------------------------------------------------------- -->
  459. <a name="RandomTimerSet"></a>
  460. <h3>Set</h3>
  461. <ul>
  462. N/A
  463. </ul>
  464. <!-- ----------------------------- -->
  465. <!-- Get ------------------------- -->
  466. <!-- Get ------------------------------------------------------------------ -->
  467. <!-- -------------------------------------------------------------------------- -->
  468. <a name="RandomTimerGet"></a>
  469. <h3>Get</h3>
  470. <ul>
  471. N/A
  472. </ul>
  473. <!-- -------------------------------------------------------------------------- -->
  474. <!-- Attributes --------------------------------------------------------------- -->
  475. <!-- -------------------------------------------------------------------------- -->
  476. <a name="RandomTimerAttributes"></a>
  477. <h3>Attributes</h3>
  478. <ul>
  479. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  480. <li><a name="disableCond">disableCond</a><br>
  481. The default behavior of a RandomTimer is, that it works.
  482. To set the Randomtimer out of work, you can specify in the disableCond attibute a condition in perlcode that must evaluate to true.
  483. The Condition must be put into round brackets. The best way is to define
  484. a function in 99_utils.
  485. <br>
  486. <b>Examples</b>
  487. <pre>
  488. attr ZufallsTimerZ disableCond (!isVerreist())
  489. attr ZufallsTimerZ disableCond (Value("presenceDummy") ne "present")
  490. </pre>
  491. </li>
  492. <li><a name="keepDeviceAlive">keepDeviceAlive</a><br>
  493. The default behavior of a RandomTimer is, that it shuts down the device after stoptime is reached.
  494. The <b>keepDeviceAlive</b> attribute changes the behavior. If set, the device status is not changed when the stoptime is reached.
  495. <br>
  496. <b>Example</b>
  497. <pre>
  498. attr ZufallsTimerZ keepDeviceAlive
  499. </pre>
  500. </li>
  501. <li><a name="onOffCmd">onCmd, offCmd</a><br>
  502. Setting the on-/offCmd changes the command sent to the device. Standard is: "set &lt;device&gt; on".
  503. The device can be specified by a @.
  504. <br>
  505. <b>Examples</b>
  506. <pre>
  507. attr Timer oncmd {fhem("set @ on-for-timer 14")}
  508. attr Timer offCmd {fhem("set @ off 16")}
  509. attr Timer oncmd set @ on-for-timer 12
  510. attr Timer offCmd set @ off 12
  511. </pre>
  512. </li>
  513. the decision to switch on or off depends on the state of the device and is evaluated by the funktion Value(<device>). Value() must
  514. evaluate one of the values "on" or "off". The behavior of devices that do not evaluate one of those values can be corrected by defining a statFormat:
  515. <pre>
  516. attr stateFormat EDIPlug_01 {(ReadingsVal("EDIPlug_01","state","nF") =~ m/(ON|on)/i) ? "on" : "off" }
  517. </pre>
  518. if a devices Value() funktion does not evalute to on or off(like WLAN-Steckdose von Edimax) you get the message:
  519. <pre>
  520. [EDIPlug] result of function Value(EDIPlug_01) must be 'on' or 'off'
  521. </pre>
  522. <li><a name="switchmode">switchmode</a><br>
  523. Setting the switchmode you can influence the behavior of switching on/off.
  524. The parameter has the Format 999/999 and the default ist 800/200. The values are in "per mill".
  525. The first parameter sets the value of the probability that the device will be switched on when the device is off.
  526. The second parameter sets the value of the probability that the device will be switched off when the device is off.
  527. <b>Examples</b>
  528. <pre>
  529. attr ZufallsTimerZ switchmode 400/400
  530. </pre>
  531. </li>
  532. </ul>
  533. =end html
  534. =cut