91_notify.pm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. ##############################################
  2. # $Id: 91_notify.pm 17225 2018-08-29 12:34:29Z rudolfkoenig $
  3. package main;
  4. use strict;
  5. use warnings;
  6. use vars qw($FW_ME); # webname (default is fhem)
  7. #####################################
  8. sub
  9. notify_Initialize($)
  10. {
  11. my ($hash) = @_;
  12. $hash->{DefFn} = "notify_Define";
  13. $hash->{NotifyFn} = "notify_Exec";
  14. $hash->{AttrFn} = "notify_Attr";
  15. no warnings 'qw';
  16. my @attrList = qw(
  17. addStateEvent:1,0
  18. disable:1,0
  19. disabledForIntervals
  20. disabledAfterTrigger
  21. forwardReturnValue:1,0
  22. ignoreRegexp
  23. readLog:1,0
  24. showtime:1,0
  25. );
  26. use warnings 'qw';
  27. $hash->{AttrList} = join(" ", @attrList);
  28. $hash->{SetFn} = "notify_Set";
  29. $hash->{StateFn} = "notify_State";
  30. $hash->{FW_detailFn} = "notify_fhemwebFn";
  31. }
  32. #####################################
  33. sub
  34. notify_Define($$)
  35. {
  36. my ($hash, $def) = @_;
  37. my ($name, $type, $re, $command) = split("[ \t\n]+", $def, 4);
  38. if(!$command) {
  39. if($hash->{OLDDEF}) { # Called from modify, where command is optional
  40. (undef, $command) = split("[ \t]+", $hash->{OLDDEF}, 2);
  41. $hash->{DEF} = "$re $command";
  42. } else {
  43. return "Usage: define <name> notify <regexp> <command>";
  44. }
  45. }
  46. # Checking for misleading regexps
  47. return "Bad regexp: starting with *" if($re =~ m/^\*/);
  48. eval { "Hallo" =~ m/^$re$/ };
  49. return "Bad regexp: $@" if($@);
  50. $hash->{REGEXP} = $re;
  51. my %specials= (
  52. "%NAME" => $name,
  53. "%TYPE" => $name,
  54. "%EVENT" => "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0",
  55. "%SELF" => $name,
  56. );
  57. my $err = perlSyntaxCheck($command, %specials);
  58. return $err if($err);
  59. $hash->{".COMMAND"} = $command;
  60. my $doTrigger = ($name !~ m/^$re$/); # Forum #34516
  61. readingsSingleUpdate($hash, "state", "active", $doTrigger);
  62. InternalTimer(0, sub(){ notifyRegexpChanged($hash, $re); }, $hash);
  63. return undef;
  64. }
  65. #####################################
  66. sub
  67. notify_Exec($$)
  68. {
  69. my ($ntfy, $dev) = @_;
  70. my $ln = $ntfy->{NAME};
  71. return "" if(IsDisabled($ln));
  72. my $now = gettimeofday();
  73. my $dat = AttrVal($ln, "disabledAfterTrigger", 0);
  74. return "" if($ntfy->{TRIGGERTIME} && $now < $ntfy->{TRIGGERTIME}+$dat);
  75. my $n = $dev->{NAME};
  76. my $re = $ntfy->{REGEXP};
  77. my $iRe = AttrVal($ln, "ignoreRegexp", undef);
  78. my $events = deviceEvents($dev, AttrVal($ln, "addStateEvent", 0));
  79. return if(!$events); # Some previous notify deleted the array.
  80. my $max = int(@{$events});
  81. my $t = $dev->{TYPE};
  82. my $ret = "";
  83. for (my $i = 0; $i < $max; $i++) {
  84. my $s = $events->[$i];
  85. $s = "" if(!defined($s));
  86. my $found = ($n =~ m/^$re$/ || "$n:$s" =~ m/^$re$/s);
  87. if(!$found && AttrVal($n, "eventMap", undef)) {
  88. my @res = ReplaceEventMap($n, [$n,$s], 0);
  89. shift @res;
  90. $s = join(" ", @res);
  91. $found = ("$n:$s" =~ m/^$re$/);
  92. }
  93. if($found) {
  94. next if($iRe && ($n =~ m/^$iRe$/ || "$n:$s" =~ m/^$iRe$/));
  95. Log3 $ln, 5, "Triggering $ln";
  96. my %specials= (
  97. "%NAME" => $n,
  98. "%TYPE" => $t,
  99. "%EVENT" => $s,
  100. "%SELF" => $ln
  101. );
  102. my $exec = EvalSpecials($ntfy->{".COMMAND"}, %specials);
  103. Log3 $ln, 4, "$ln exec $exec";
  104. my $r = AnalyzeCommandChain(undef, $exec);
  105. Log3 $ln, 3, "$ln return value: $r" if($r);
  106. $ret .= " $r" if($r);
  107. $ntfy->{TRIGGERTIME} = $now;
  108. $ntfy->{STATE} =
  109. AttrVal($ln,'showtime',1) ? $dev->{NTFY_TRIGGERTIME} : 'active';
  110. }
  111. }
  112. return $ret if(AttrVal($ln, "forwardReturnValue", 0));
  113. return undef;
  114. }
  115. sub
  116. notify_Attr(@)
  117. {
  118. my @a = @_;
  119. my $do = 0;
  120. my $hash = $defs{$a[1]};
  121. if($a[0] eq "set" && $a[2] eq "readLog") {
  122. if(!defined($a[3]) || $a[3]) {
  123. $logInform{$a[1]} = sub($$){
  124. my ($me, $msg) = @_;
  125. return if(defined($hash->{CHANGED}));
  126. $hash->{CHANGED}[0] = $msg;
  127. notify_Exec($hash, $hash);
  128. delete($hash->{CHANGED});
  129. }
  130. } else {
  131. delete $logInform{$a[1]};
  132. }
  133. return;
  134. }
  135. if($a[0] eq "set" && $a[2] eq "ignoreRegexp") {
  136. return "Missing argument for ignoreRegexp" if(!defined($a[3]));
  137. eval { "HALLO" =~ m/$a[3]/ };
  138. return $@;
  139. }
  140. if($a[0] eq "set" && $a[2] eq "disable") {
  141. $do = (!defined($a[3]) || $a[3]) ? 1 : 2;
  142. }
  143. $do = 2 if($a[0] eq "del" && (!$a[2] || $a[2] eq "disable"));
  144. return if(!$do);
  145. readingsSingleUpdate($hash, "state", $do==1 ? "disabled":"active", 1);
  146. return undef;
  147. }
  148. ###################################
  149. sub
  150. notify_Set($@)
  151. {
  152. my ($hash, @a) = @_;
  153. my $me = $hash->{NAME};
  154. return "no set argument specified" if(int(@a) < 2);
  155. my %sets = (addRegexpPart=>2, removeRegexpPart=>1, inactive=>0, active=>0);
  156. my $cmd = $a[1];
  157. if(!defined($sets{$cmd})) {
  158. my $ret ="Unknown argument $cmd, choose one of ".join(" ", sort keys %sets);
  159. $ret =~ s/active/active:noArg/g;
  160. return $ret;
  161. }
  162. return "$cmd needs $sets{$cmd} parameter(s)" if(@a-$sets{$cmd} != 2);
  163. if($cmd eq "addRegexpPart") {
  164. my %h;
  165. my $re = "$a[2]:$a[3]";
  166. map { $h{$_} = 1 } split(/\|/, $hash->{REGEXP});
  167. $h{$re} = 1;
  168. $re = join("|", sort keys %h);
  169. return "Bad regexp: starting with *" if($re =~ m/^\*/);
  170. eval { "Hallo" =~ m/^$re$/ };
  171. return "Bad regexp: $@" if($@);
  172. $hash->{REGEXP} = $re;
  173. $hash->{DEF} = "$re ".$hash->{".COMMAND"};
  174. notifyRegexpChanged($hash, $re);
  175. } elsif($cmd eq "removeRegexpPart") {
  176. my %h;
  177. map { $h{$_} = 1 } split(/\|/, $hash->{REGEXP});
  178. return "Cannot remove regexp part: not found" if(!$h{$a[2]});
  179. return "Cannot remove last regexp part" if(int(keys(%h)) == 1);
  180. delete $h{$a[2]};
  181. my $re = join("|", sort keys %h);
  182. return "Bad regexp: starting with *" if($re =~ m/^\*/);
  183. eval { "Hallo" =~ m/^$re$/ };
  184. return "Bad regexp: $@" if($@);
  185. $hash->{REGEXP} = $re;
  186. $hash->{DEF} = "$re ".$hash->{".COMMAND"};
  187. notifyRegexpChanged($hash, $re);
  188. } elsif($cmd eq "inactive") {
  189. readingsSingleUpdate($hash, "state", "inactive", 1);
  190. }
  191. elsif($cmd eq "active") {
  192. readingsSingleUpdate($hash, "state", "active", 1)
  193. if(!AttrVal($me, "disable", undef));
  194. }
  195. return undef;
  196. }
  197. #############
  198. sub
  199. notify_State($$$$)
  200. {
  201. my ($hash, $tim, $vt, $val) = @_;
  202. return undef if($vt ne "state" || $val ne "inactive");
  203. readingsSingleUpdate($hash, "state", "inactive", 1);
  204. return undef;
  205. }
  206. #########################
  207. sub
  208. notify_fhemwebFn($$$$)
  209. {
  210. my ($FW_wname, $d, $room, $pageHash) = @_; # pageHash is set for summaryFn.
  211. my $hash = $defs{$d};
  212. my $ret .= "<div class='makeTable wide'><span>Change wizard</span>".
  213. "<table class='block wide'>";
  214. my $row = 0;
  215. my @ra = split(/\|/, $hash->{REGEXP});
  216. $ret .= "<tr class='".(($row++&1)?"odd":"even").
  217. "'><td colspan='2'>Change the condition:</td></tr>";
  218. if(@ra > 1) {
  219. foreach my $r (@ra) {
  220. $ret .= "<tr class='".(($row++&1)?"odd":"even")."'>";
  221. my $cmd = "cmd.X= set $d removeRegexpPart&val.X=$r"; # =.set: avoid JS
  222. $ret .= "<td>$r</td>";
  223. $ret .= FW_pH("$cmd&detail=$d", "removeRegexpPart", 1,undef,1);
  224. $ret .= "</tr>";
  225. }
  226. }
  227. my @et = devspec2array("TYPE=eventTypes");
  228. if(!@et) {
  229. $ret .= "<tr class='".(($row++&1)?"odd":"even")."'>";
  230. $ret .= FW_pH("$FW_ME/docs/commandref.html#eventTypes",
  231. "To add a regexp an eventTypes definition is needed",
  232. 1, undef, 1)."</tr>";
  233. } else {
  234. my %dh;
  235. my $etList = AnalyzeCommand(undef, "get $et[0] list");
  236. $etList = "" if(!$etList);
  237. foreach my $l (split("\n", $etList)) {
  238. my @a = split(/[ \r\n]/, $l);
  239. $a[1] = "" if(!defined($a[1]));
  240. $a[1] =~ s/\.\*//g;
  241. $a[1] =~ s/,.*//g;
  242. next if(@a < 2);
  243. $dh{$a[0]}{".*"} = 1;
  244. $dh{$a[0]}{$a[1].".*"} = 1;
  245. }
  246. my $list = "";
  247. foreach my $dev (sort keys %dh) {
  248. $list .= " $dev:" . join(",", sort keys %{$dh{$dev}});
  249. }
  250. $list =~ s/(['"])/./g;
  251. $ret .= "<tr class=\"".(($row++&1)?"odd":"even")."\">";
  252. $ret .= '<td colspan="2">';
  253. $ret .= FW_detailSelect($d, "set", $list, "addRegexpPart");
  254. $ret .= "</td></tr>";
  255. }
  256. my ($tr, $js) = notfy_addFWCmd($d, $hash->{REGEXP}, $row);
  257. return "$ret$tr</table></div><br>$js";
  258. }
  259. sub
  260. notfy_addFWCmd($$$)
  261. {
  262. my ($d, $param, $row) = @_;
  263. my $ret="";
  264. $ret .= "<tr class='".(($row++&1)?"odd":"even")."'><td colspan='2'>".
  265. "&nbsp;</td></tr>";
  266. $ret .= "<tr class='".(($row++&1)?"odd":"even")."'><td colspan='2'>".
  267. "Change the executed command:</td></tr>";
  268. $ret .= "<tr class='".(($row++&1)?"odd":"even")."'><td colspan='2'>";
  269. my @list = grep { !$defs{$_}{TEMPORARY} && $_ ne $d &&
  270. $modules{$defs{$_}{TYPE}}{SetFn} } sort keys %defs;
  271. $ret .= "<input class='set' id='modCmd' type='submit' value='modify' ".
  272. "data-d='$d' data-p='$param'>";
  273. $ret .= "<div class='set downText'>&nbsp;$d $param set </div>";
  274. $ret .= FW_select("modDev", "mod", \@list, undef, "set");
  275. $ret .= "<select class='set' id='modArg'></select>";
  276. $ret .= "<input type='text' name='modVal' size='10'/>";
  277. $ret .= "</td></tr>";
  278. my $js = << 'END';
  279. <script>
  280. var ntArr, ntDev;
  281. function
  282. ntfyCmd()
  283. {
  284. ntDev=$("#modDev").val();
  285. FW_cmd(FW_root+
  286. "?cmd="+addcsrf(encodeURIComponent("set "+ntDev+" ?"))+"&XHR=1",
  287. function(ret) {
  288. ret = ret.replace(/[\r\n]/g,'');
  289. ntArr=ret.substr(ret.indexOf("choose one of")+14,ret.length).split(" ");
  290. var str="";
  291. for(var i1=0; i1<ntArr.length; i1++) {
  292. var off = ntArr[i1].indexOf(":");
  293. str += "<option value="+i1+">"+
  294. (off==-1 ? ntArr[i1] : ntArr[i1].substr(0,off))+
  295. "</option>";
  296. }
  297. $("#modArg").html(str);
  298. ntfyArg();
  299. });
  300. }
  301. function
  302. ntfyArg()
  303. {
  304. var v=ntArr[$("#modArg").val()];
  305. var vArr = [];
  306. if(v.indexOf(":") > 0)
  307. vArr = v.substr(v.indexOf(":")+1).split(",");
  308. FW_replaceWidget($("#modArg").next(), ntDev, vArr);
  309. }
  310. function
  311. ntfyGo()
  312. {
  313. var d=$("#modCmd").attr("data-d");
  314. var p=$("#modCmd").attr("data-p");
  315. var cmd = "modify "+d+" "+p+" set "+$("#modDev").val()+" "+
  316. $("#modArg :selected").text()+" "+$("[name=modVal]").val();
  317. location=FW_root+"?cmd="+addcsrf(encodeURIComponent(cmd))+"&detail="+d;
  318. }
  319. $(document).ready(function(){
  320. $("#modDev").change(ntfyCmd);
  321. $("#modArg").change(ntfyArg);
  322. $("#modCmd").click(ntfyGo);
  323. $("#").change(ntfyArg);
  324. ntfyCmd();
  325. });
  326. </script>
  327. END
  328. return ($ret, $js);
  329. }
  330. 1;
  331. =pod
  332. =item helper
  333. =item summary execute a command upon receiving an event
  334. =item summary_DE f&uuml;hrt bei Events Anweisungen aus
  335. =begin html
  336. <a name="notify"></a>
  337. <h3>notify</h3>
  338. <ul>
  339. <br>
  340. <a name="notifydefine"></a>
  341. <b>Define</b>
  342. <ul>
  343. <code>define &lt;name&gt; notify &lt;pattern&gt; &lt;command&gt;</code>
  344. <br><br>
  345. Execute a command when received an event for the <a
  346. href="#define">definition</a> <code>&lt;pattern&gt;</code>. If
  347. &lt;command&gt; is enclosed in {}, then it is a perl expression, if it is
  348. enclosed in "", then it is a shell command, else it is a "plain" fhem.pl
  349. command (chain). See the <a href="#trigger">trigger</a> command for
  350. testing it.
  351. Examples:
  352. <ul>
  353. <code>define b3lampV1 notify btn3 set lamp $EVENT</code><br>
  354. <code>define b3lampV2 notify btn3 { fhem "set lamp $EVENT" }</code><br>
  355. <code>define b3lampV3 notify btn3 "/usr/local/bin/setlamp "$EVENT""</code><br>
  356. <code>define b3lampV3 notify btn3 set lamp1 $EVENT;;set lamp2 $EVENT</code><br>
  357. <code>define wzMessLg notify wz:measured.* "/usr/local/bin/logfht $NAME "$EVENT""</code><br>
  358. <code>define LogUndef notify global:UNDEFINED.* "send-me-mail.sh "$EVENT""</code><br>
  359. </ul>
  360. <br>
  361. Notes:
  362. <ul>
  363. <li><code>&lt;pattern&gt;</code> is either the name of the triggering
  364. device, or <code>devicename:event</code>.</li>
  365. <li><code>&lt;pattern&gt;</code> must completely (!)
  366. match either the device name, or the compound of the device name and
  367. the event. To identify the events use the inform command from the
  368. telnet prompt or the "Event Monitor" link in the browser
  369. (FHEMWEB), and wait for the event to be printed. See also the
  370. eventTypes device.</li>
  371. <li>in the command section you can access the event:
  372. <ul>
  373. <li>The variable $EVENT will contain the complete event, e.g.
  374. <code>measured-temp: 21.7 (Celsius)</code></li>
  375. <li>$EVTPART0,$EVTPART1,$EVTPART2,etc contain the space separated event
  376. parts (e.g. <code>$EVTPART0="measured-temp:", $EVTPART1="21.7",
  377. $EVTPART2="(Celsius)"</code>. This data is available as a local
  378. variable in perl, as environment variable for shell scripts, and will
  379. be textually replaced for FHEM commands.</li>
  380. <li>$NAME and $TYPE contain the name and type of the device triggering
  381. the event, e.g. myFht and FHT</li>
  382. </ul></li>
  383. <li>Note: the following is deprecated and will be removed in a future
  384. release. It is only active for featurelevel up to 5.6.
  385. The described replacement is attempted if none of the above
  386. variables ($NAME/$EVENT/etc) found in the command.
  387. <ul>
  388. <li>The character <code>%</code> will be replaced with the received
  389. event, e.g. with <code>on</code> or <code>off</code> or
  390. <code>measured-temp: 21.7 (Celsius)</code><br> It is advisable to put
  391. the <code>%</code> into double quotes, else the shell may get a syntax
  392. error.</li>
  393. <li>The character @ will be replaced with the device
  394. name.</li>
  395. <li>To use % or @ in the text itself, use the double mode (%% or
  396. @@).</li>
  397. <li>Instead of % and @, the parameters %EVENT (same as %), %NAME (same
  398. as @) and %TYPE (contains the device type,
  399. e.g. FHT) can be used. The space separated event "parts"
  400. are available as %EVTPART0, %EVTPART1, etc. A single %
  401. looses its special meaning if any of these parameters appears in the
  402. definition.</li>
  403. </ul></li>
  404. <li>Following special events will be generated for the device "global"
  405. <ul>
  406. <li>INITIALIZED after initialization is finished.</li>
  407. <li>REREADCFG after the configuration is reread.</li>
  408. <li>SAVE before the configuration is saved.</li>
  409. <li>SHUTDOWN before FHEM is shut down.</li>
  410. <li>DEFINED &lt;devname&gt; after a device is defined.</li>
  411. <li>DELETED &lt;devname&gt; after a device was deleted.</li>
  412. <li>RENAMED &lt;old&gt; &lt;new&gt; after a device was renamed.</li>
  413. <li>UNDEFINED &lt;defspec&gt; upon reception of a message for an
  414. undefined device.</li>
  415. </ul></li>
  416. <li>Notify can be used to store macros for manual execution. Use the <a
  417. href="#trigger">trigger</a> command to execute the macro.
  418. E.g.<br>
  419. <code>fhem> define MyMacro notify MyMacro { Log 1, "Hello"}</code><br>
  420. <code>fhem> trigger MyMacro</code><br>
  421. </li>
  422. </ul>
  423. </ul>
  424. <br>
  425. <a name="notifyset"></a>
  426. <b>Set </b>
  427. <ul>
  428. <li>addRegexpPart &lt;device&gt; &lt;regexp&gt;<br>
  429. add a regexp part, which is constructed as device:regexp. The parts
  430. are separated by |. Note: as the regexp parts are resorted, manually
  431. constructed regexps may become invalid. </li>
  432. <li>removeRegexpPart &lt;re&gt;<br>
  433. remove a regexp part. Note: as the regexp parts are resorted, manually
  434. constructed regexps may become invalid.<br>
  435. The inconsistency in addRegexpPart/removeRegexPart arguments originates
  436. from the reusage of javascript functions.</li>
  437. <li>inactive<br>
  438. Inactivates the current device. Note the slight difference to the
  439. disable attribute: using set inactive the state is automatically saved
  440. to the statefile on shutdown, there is no explicit save necesary.<br>
  441. This command is intended to be used by scripts to temporarily
  442. deactivate the notify.<br>
  443. The concurrent setting of the disable attribute is not recommended.</li>
  444. <li>active<br>
  445. Activates the current device (see inactive).</li>
  446. </ul>
  447. <br>
  448. <a name="notifyget"></a>
  449. <b>Get</b> <ul>N/A</ul><br>
  450. <a name="notifyattr"></a>
  451. <b>Attributes</b>
  452. <ul>
  453. <li><a href="#disable">disable</a></li>
  454. <li><a href="#disabledForIntervals">disabledForIntervals</a></li>
  455. <a name="disabledAfterTrigger"></a>
  456. <li>disabledAfterTrigger someSeconds<br>
  457. disable the execution for someSeconds after it triggered.
  458. </li>
  459. <a name="addStateEvent"></a>
  460. <li>addStateEvent<br>
  461. The event associated with the state Reading is special, as the "state: "
  462. string is stripped, i.e $EVENT is not "state: on" but just "on". In some
  463. circumstances it is desireable to get the event without "state: "
  464. stripped. In such a case the addStateEvent attribute should be set to 1
  465. (default is 0, i.e. strip the "state: " string).<br>
  466. Note 1: you have to set this attribute for the event "receiver", i.e.
  467. notify, FileLog, etc.<br>
  468. Note 2: this attribute will only work for events generated by devices
  469. supporting the <a href="#readingFnAttributes">readingFnAttributes</a>.
  470. </li>
  471. <a name="forwardReturnValue"></a>
  472. <li>forwardReturnValue<br>
  473. Forward the return value of the executed command to the caller,
  474. default is disabled (0). If enabled (1), then e.g. a set command which
  475. triggers this notify will also return this value. This can cause e.g
  476. FHEMWEB to display this value, when clicking "on" or "off", which is
  477. often not intended.</li>
  478. <a name="ignoreRegexp"></a>
  479. <li>ignoreRegexp regexp<br>
  480. It is hard to create a regexp which is _not_ matching something, this
  481. attribute helps in this case, as the event is ignored if matches the
  482. argument. The syntax is the same as for the original regexp.
  483. </li>
  484. <a name="readLog"></a>
  485. <li>readLog<br>
  486. Execute the notify for messages appearing in the FHEM Log. The device
  487. in this case is set to the notify itself, e.g. checking for the startup
  488. message looks like:
  489. <ul><code>
  490. define n notify n:.*Server.started.* { Log 1, "Really" }<br>
  491. attr n readLog
  492. </code></ul>
  493. </li>
  494. <a name="perlSyntaxCheck"></a>
  495. <li>perlSyntaxCheck<br>
  496. by setting the <b>global</b> attribute perlSyntaxCheck, a syntax check
  497. will be executed upon definition or modification, if the command is
  498. perl and FHEM is already started.
  499. </li>
  500. </ul>
  501. <br>
  502. </ul>
  503. =end html
  504. =begin html_DE
  505. <a name="notify"></a>
  506. <h3>notify</h3>
  507. <ul>
  508. <br>
  509. <a name="notifydefine"></a>
  510. <b>Define</b>
  511. <ul>
  512. <code>define &lt;name&gt; notify &lt;Suchmuster&gt; &lt;Anweisung&gt;</code>
  513. <br><br>
  514. F&uuml;hrt eine oder mehrere Anweisungen aus, wenn ein Event generiert
  515. wurde, was dem &lt;Suchmuster&gt; (Ger&auml;tename oder
  516. Ger&auml;tename:Event) entspricht.
  517. Die Anweisung ist einer der FHEM <a href="#command">Befehlstypen</a>.
  518. Zum Test dient das <a href="#trigger">trigger</a>-Kommando.
  519. <br><br>
  520. Beispiele:
  521. <ul>
  522. <code>define b3lampV1 notify btn3 set lamp $EVENT</code><br>
  523. <code>define b3lampV2 notify btn3 { fhem "set lamp $EVENT" }</code><br>
  524. <code>define b3lampV3 notify btn3 "/usr/local/bin/setlamp
  525. "$EVENT""</code><br>
  526. <code>define b3lampV3 notify btn3 set lamp1 $EVENT;;set lamp2
  527. $EVENT</code><br>
  528. <code>define wzMessLg notify wz:measured.* "/usr/local/bin/logfht $NAME
  529. "$EVENT""</code><br>
  530. <code>define LogUndef notify global:UNDEFINED.* "send-me-mail.sh
  531. "$EVENT""</code><br>
  532. </ul>
  533. <br>
  534. Hinweise:
  535. <ul>
  536. <li><code>&lt;Suchmuster&gt;</code> ist entweder der Name des
  537. ausl&ouml;senden ("triggernden") Ger&auml;tes oder die Kombination aus
  538. Ger&auml;t und ausl&ouml;sendem Ereignis (Event)
  539. <code>Ger&auml;tename:Event</code>.</li>
  540. <li>Das <code>&lt;Suchmuster&gt;</code> muss exakt (!)
  541. entweder dem Ger&auml;tenamen entsprechen oder der Zusammenf&uuml;gung
  542. aus Ger&auml;tename:Event. Events lassen sich mit "inform" in Telnet
  543. oder durch Beobachtung des "Event-Monitors" in FHEMWEB ermitteln.</li>
  544. <li>In der Anweisung von Notify kann das ausl&ouml;sende Ereignis (Event)
  545. genutzt werden:
  546. <ul>
  547. <li>Die Anweisung $EVENT wird das komplette Ereignis (Event)
  548. beinhalten, z.B. <code>measured-temp: 21.7 (Celsius)</code></li>
  549. <li>$EVTPART0,$EVTPART1,$EVTPART2,etc enthalten die durch Leerzeichen
  550. getrennten Teile des Events der Reihe nach (im Beispiel also
  551. <code>$EVTPART0="measured-temp:", $EVTPART1="21.7",
  552. $EVTPART2="(Celsius)"</code>.<br> Diese Daten sind verf&uuml;gbar
  553. als lokale Variablen in Perl, als Umgebungs-Variablen f&uuml;r
  554. Shell-Scripts, und werden als Text ausgetauscht in
  555. FHEM-Kommandos.</li>
  556. <li>$NAME und $TYPE enthalten den Namen bzw. Typ des Ereignis
  557. ausl&ouml;senden Ger&auml;tes, z.B. myFht und FHT</li>
  558. </ul></li>
  559. <li>Achtung: Folgende Vorgehensweise ist abgek&uuml;ndigt, funktioniert
  560. bis featurelevel 5.6 und wird in einem zuk&uuml;nftigen Release von
  561. FHEM nicht mehr unterst&uuml;tzt. Wenn keine der oben genannten
  562. Variablen ($NAME/$EVENT/usw.) in der Anweisung gefunden wird, werden
  563. Platzhalter ersetzt.
  564. <ul>
  565. <li>Das Zeichen % wird ersetzt mit dem empfangenen
  566. Ereignis (Event), z.B. mit on oder off oder
  567. <code>measured-temp: 21.7 (Celsius)</code>.
  568. </li>
  569. <li>Das Zeichen @ wird ersetzt durch den
  570. Ger&auml;tenamen.</li>
  571. <li>Um % oder @ im Text selbst benutzen zu k&ouml;nnen, m&uuml;ssen
  572. sie verdoppelt werden (%% oder @@).</li>
  573. <li>Anstelle von % und @, k&ouml;nnen die
  574. Parameter %EVENT (funktionsgleich mit %),
  575. %NAME (funktionsgleich mit @) und
  576. %TYPE (enth&auml;lt den Typ des Ger&auml;tes, z.B.
  577. FHT) benutzt werden. Die von Leerzeichen unterbrochenen
  578. Teile eines Ereignisses (Event) sind verf&uuml;gbar als %EVTPART0,
  579. %EVTPART1, usw. Ein einzeln stehendes % verliert seine
  580. oben beschriebene Bedeutung, falls auch nur einer dieser Parameter
  581. in der Definition auftaucht.</li>
  582. </ul></li>
  583. <li>Folgende spezielle Ereignisse werden f&uuml;r das Ger&auml;t "global"
  584. erzeugt:
  585. <ul>
  586. <li>INITIALIZED sobald die Initialization vollst&auml;ndig ist.</li>
  587. <li>REREADCFG nachdem die Konfiguration erneut eingelesen wurde.</li>
  588. <li>SAVE bevor die Konfiguration gespeichert wird.</li>
  589. <li>SHUTDOWN bevor FHEM heruntergefahren wird.</li>
  590. <li>DEFINED &lt;devname&gt; nach dem Definieren eines
  591. Ger&auml;tes.</li>
  592. <li>DELETED &lt;devname&gt; nach dem L&ouml;schen eines
  593. Ger&auml;tes.</li>
  594. <li>RENAMED &lt;old&gt; &lt;new&gt; nach dem Umbenennen eines
  595. Ger&auml;tes.</li>
  596. <li>UNDEFINED &lt;defspec&gt; beim Auftreten einer Nachricht f&uuml;r
  597. ein undefiniertes Ger&auml;t.</li>
  598. </ul></li>
  599. <li>Notify kann dazu benutzt werden, um Makros f&uuml;r eine manuelle
  600. Ausf&uuml;hrung zu speichern. Mit einem <a
  601. href="#trigger">trigger</a> Kommando k&ouml;nnen solche Makros dann
  602. ausgef&uuml;hrt werden. Z.B.<br> <code>fhem> define MyMacro notify
  603. MyMacro { Log 1, "Hello"}</code><br> <code>fhem> trigger
  604. MyMacro</code><br> </li>
  605. </ul>
  606. </ul>
  607. <br>
  608. <a name="notifyset"></a>
  609. <b>Set </b>
  610. <ul>
  611. <li>addRegexpPart &lt;device&gt; &lt;regexp&gt;<br>
  612. F&uuml;gt ein regexp Teil hinzu, der als device:regexp aufgebaut ist.
  613. Die Teile werden nach Regexp-Regeln mit | getrennt. Achtung: durch
  614. hinzuf&uuml;gen k&ouml;nnen manuell erzeugte Regexps ung&uuml;ltig
  615. werden.</li>
  616. <li>removeRegexpPart &lt;re&gt;<br>
  617. Entfernt ein regexp Teil. Die Inkonsistenz von addRegexpPart /
  618. removeRegexPart-Argumenten hat seinen Ursprung in der Wiederverwendung
  619. von Javascript-Funktionen.</li>
  620. <li>inactive<br>
  621. Deaktiviert das entsprechende Ger&auml;t. Beachte den leichten
  622. semantischen Unterschied zum disable Attribut: "set inactive"
  623. wird bei einem shutdown automatisch in fhem.state gespeichert, es ist
  624. kein save notwendig.<br>
  625. Der Einsatzzweck sind Skripte, um das notify tempor&auml;r zu
  626. deaktivieren.<br>
  627. Das gleichzeitige Verwenden des disable Attributes wird nicht empfohlen.
  628. </li>
  629. <li>active<br>
  630. Aktiviert das entsprechende Ger&auml;t, siehe inactive.
  631. </li>
  632. </ul>
  633. <br>
  634. <a name="notifyget"></a>
  635. <b>Get</b> <ul>N/A</ul><br>
  636. <a name="notifyattr"></a>
  637. <b>Attribute</b>
  638. <ul>
  639. <li><a href="#disable">disable</a></li>
  640. <li><a href="#disabledForIntervals">disabledForIntervals</a></li>
  641. <a name="addStateEvent"></a>
  642. <li>addStateEvent<br>
  643. Das mit dem state Reading verkn&uuml;pfte Event ist speziell, da das
  644. dazugeh&ouml;rige Prefix "state: " entfernt wird, d.h. $EVENT ist nicht
  645. "state: on", sondern nur "on". In manchen F&auml;llen ist es aber
  646. erw&uuml;nscht das unmodifizierte Event zu bekommen, d.h. wo "state: "
  647. nicht entfernt ist. F&uuml;r diese F&auml;lle sollte addStateEvent auf 1
  648. gesetzt werden, die Voreinstellung ist 0 (deaktiviert).<br>
  649. Achtung:
  650. <ul>
  651. <li>dieses Attribut muss beim Empf&auml;nger (notify, FileLog, etc)
  652. gesetzt werden.</li>
  653. <li>dieses Attribut zeigt nur f&uuml;r solche Ger&auml;te-Events eine
  654. Wirkung, die <a href="#readingFnAttributes">readingFnAttributes</a>
  655. unterst&uuml;tzen.</li>
  656. </ul>
  657. </li>
  658. <a name="forwardReturnValue"></a>
  659. <li>forwardReturnValue<br>
  660. R&uuml;ckgabe der Werte eines ausgef&uuml;hrten Kommandos an den
  661. Aufrufer. Die Voreinstellung ist 0 (ausgeschaltet), um weniger
  662. Meldungen im Log zu haben.
  663. </li>
  664. <a name="ignoreRegexp"></a>
  665. <li>ignoreRegexp regexp<br>
  666. Es ist nicht immer einfach ein Regexp zu bauen, was etwas _nicht_
  667. matcht. Dieses Attribu hilft in diesen F&auml;llen: das Event wird
  668. ignoriert, falls den angegebenen Regexp. Syntax ist gleich wie in der
  669. Definition.
  670. </li>
  671. <a name="readLog"></a>
  672. <li>readLog<br>
  673. Das notify wird f&uuml;r Meldungen, die im FHEM-Log erscheinen,
  674. ausgegef&uuml;hrt. Das "Event-Generierende-Ger&auml;t" wird auf dem
  675. notify selbst gesetzt. Z.Bsp. kann man mit folgendem notify auf die
  676. Startup Meldung reagieren:
  677. <ul><code>
  678. define n notify n:.*Server.started.* { Log 1, "Wirklich" }<br>
  679. attr n readLog
  680. </code></ul>
  681. </li>
  682. <a name="perlSyntaxCheck"></a>
  683. <li>perlSyntaxCheck<br>
  684. nach setzen des <b>global</b> Attributes perlSyntaxCheck wird eine
  685. Syntax-Pr&uuml;fung der Anweisung durchgef&uuml;hrt bei jeder
  686. &Auml;nderung (define oder modify), falls die Anweisung Perl ist, und
  687. FHEM bereits gestartet ist. </li>
  688. </ul>
  689. <br>
  690. </ul>
  691. =end html_DE
  692. =cut