############################################## # $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 loglevel:0,1,2,3,4,5,6"; } ##################################### 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) { Log GetLogLevel($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); Log GetLogLevel($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 %
        define b3lampV2 notify btn3 { fhem "set lamp %" }
        define b3lampV3 notify btn3 "/usr/local/bin/setlamp "%""
        define b3lampV3 notify btn3 set lamp1 %;;set lamp2 %
        define wzMessLg notify wz:measured.* "/usr/local/bin/logfht @ "%""
        define LogUndef notify global:UNDEFINED.* "send-me-mail.sh "%""

      Notes:
      • 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.
      • <pattern> may also be a compound of definition:event to filter for events.
      • <pattern> must completely (!) match either the device name, or the compound of the device name and the event. The event is either the string you see in the list output in paranthesis after the device name, or the string you see when you do a detailed list of the device.
      • To use database logging, copy the file contrib/91_DbLog.pm into your modules directory, 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
    • loglevel
    • 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