############################################## # $Id$ package main; use strict; use warnings; ##################################### sub notify_Initialize($) { my ($hash) = @_; $hash->{DefFn} = "notify_Define"; $hash->{NotifyFn} = "notify_Exec"; $hash->{AttrFn} = "notify_Attr"; $hash->{AttrList} = "disable:0,1 forwardReturnValue:0,1"; } ##################################### sub notify_Define($$) { my ($hash, $def) = @_; my ($name, $type, $re, $command) = split("[ \t]+", $def, 4); if(!$command) { if($hash->{OLDDEF}) { # Called from modify, where command is optional (undef, $command) = split("[ \t]+", $hash->{OLDDEF}, 2); $hash->{DEF} = "$re $command"; } else { return "Usage: define notify "; } } # Checking for misleading regexps eval { "Hallo" =~ m/^$re$/ }; return "Bad regexp: $@" if($@); $hash->{REGEXP} = $re; $hash->{STATE} = "active"; return undef; } ##################################### sub notify_Exec($$) { my ($ntfy, $dev) = @_; my $ln = $ntfy->{NAME}; return "" if($attr{$ln} && $attr{$ln}{disable}); my $n = $dev->{NAME}; my $re = $ntfy->{REGEXP}; return if(!$dev->{CHANGED}); # Some previous notify deleted the array. my $max = int(@{$dev->{CHANGED}}); my $t = $dev->{TYPE}; my $ret = ""; for (my $i = 0; $i < $max; $i++) { my $s = $dev->{CHANGED}[$i]; $s = "" if(!defined($s)); my $found = ($n =~ m/^$re$/ || "$n:$s" =~ m/^$re$/); if(!$found && AttrVal($n, "eventMap", undef)) { (undef, $s) = ReplaceEventMap($n, [$n,$s], 0); $found = ("$n:$s" =~ m/^$re$/); } if($found) { Log3 $ln, 5, "Triggering $ln"; my (undef, $exec) = split("[ \t]+", $ntfy->{DEF}, 2); my %specials= ( "%NAME" => $n, "%TYPE" => $t, "%EVENT" => $s ); $exec= EvalSpecials($exec, %specials); my $r = AnalyzeCommandChain(undef, $exec); Log3 $ln, 3, "$ln return value: $r" if($r); $ret .= " $r" if($r); } } return $ret if(AttrVal($ln, "forwardReturnValue", 0)); return undef; } sub notify_Attr(@) { my @a = @_; my $do = 0; if($a[0] eq "set" && $a[2] eq "disable") { $do = (!defined($a[3]) || $a[3]) ? 1 : 2; } $do = 2 if($a[0] eq "del" && (!$a[2] || $a[2] eq "disable")); return if(!$do); $defs{$a[1]}{STATE} = ($do == 1 ? "disabled" : "active"); return undef; } 1; =pod =begin html

notify


    Define
      define <name> notify <pattern> <command>

      Execute a command when received an event for the definition <pattern>. If <command> is enclosed in {}, then it is a perl expression, if it is enclosed in "", then it is a shell command, else it is a "plain" fhem.pl command (chain). See the trigger command for testing it. Examples:
        define b3lampV1 notify btn3 set lamp $EVENT
        define b3lampV2 notify btn3 { fhem "set lamp $EVENT" }
        define b3lampV3 notify btn3 "/usr/local/bin/setlamp "$EVENT""
        define b3lampV3 notify btn3 set lamp1 $EVENT;;set lamp2 $EVENT
        define wzMessLg notify wz:measured.* "/usr/local/bin/logfht $NAME "$EVENT""
        define LogUndef notify global:UNDEFINED.* "send-me-mail.sh "$EVENT""

      Notes:
      • <pattern> is either the name of the triggering device, or devicename:event.
      • <pattern> must completely (!) match either the device name, or the compound of the device name and the event. To identify the events use "inform" command in telnet or "Event Monitor" in FHEMWEB.
      • in the command section you can access the event:
        • The variable $EVENT will contain the complete event, e.g. measured-temp: 21.7 (Celsius)
        • $EVTPART0,$EVTPART1,$EVTPART2,etc contain the space separated event parts (e.g. $EVTPART0="measured-temp:", $EVTPART1="21.7", $EVTPART2="(Celsius)". This data is available as a local variable in perl, as environment variable for shell scripts, and will be textually replaced for FHEM commands.
        • $NAME contains the device triggering the event, e.g. myFht
      • Note: the following is deprecated and will be removed in a future release. The described replacement is attempted if none of the above variables ($NAME/$EVENT/etc) found in the command.
        • The character % will be replaced with the received event, e.g. with on or off or measured-temp: 21.7 (Celsius)
          It is advisable to put the % into double quotes, else the shell may get a syntax error.
        • The character @ will be replaced with the device name.
        • To use % or @ in the text itself, use the double mode (%% or @@).
        • Instead of % and @, the parameters %EVENT (same as %), %NAME (same as @) and %TYPE (contains the device type, e.g. FHT) can be used. The space separated event "parts" are available as %EVTPART0, %EVTPART1, etc. A single % looses its special meaning if any of these parameters appears in the definition.
      • To use database logging, define a dblog instance and change the $dbconn parameter in the file.
      • Following special events will be generated for the device "global"
        • INITIALIZED after initialization is finished.
        • DEFINED <devname> after a device is defined.
        • DELETED <devname> after a device was deleted.
        • RENAMED <old> <new> after a device was renamed.
        • UNDEFINED <defspec> upon reception of a message for an undefined device.
      • Notify can be used to store macros for manual execution. Use the trigger command to execute the macro. E.g.
        fhem> define MyMacro notify MyMacro { Log 1, "Hello"}
        fhem> trigger MyMacro

    Set
      N/A

    Get
      N/A

    Attributes
    • disable
    • forwardReturnValue
      Forward the return value of the executed command to the caller, default is disabled (0). If enabled (1), then e.g. a set command which triggers this notify will also return this value. This can cause e.g FHEMWEB to display this value, when clicking "on" or "off", which is often not intended.

=end html =cut