91_notify.pm 27 KB

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