From f266cb241d32a94dd9ac72f846dcc9149bc3ba9d Mon Sep 17 00:00:00 2001 From: rudolfkoenig <> Date: Tue, 31 Mar 2020 19:31:34 +0000 Subject: [PATCH] 10_MQTT2_DEVICE.pm: add periodicCmd git-svn-id: https://svn.fhem.de/fhem/trunk@21566 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/10_MQTT2_DEVICE.pm | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/fhem/FHEM/10_MQTT2_DEVICE.pm b/fhem/FHEM/10_MQTT2_DEVICE.pm index 716a7ac7b..372e84975 100644 --- a/fhem/FHEM/10_MQTT2_DEVICE.pm +++ b/fhem/FHEM/10_MQTT2_DEVICE.pm @@ -40,6 +40,7 @@ MQTT2_DEVICE_Initialize($) imageLink jsonMap:textField-long model + periodicCmd readingList:textField-long setExtensionsEvent:1,0 setList:textField-long @@ -498,9 +499,52 @@ MQTT2_DEVICE_Attr($$) } } + if($attrName eq "periodicCmd") { + if($type eq "set") { + if($init_done) { + my ($gets,undef) = MQTT2_getCmdHash(AttrVal($dev, "getList", "")); + my ($sets,undef) = MQTT2_getCmdHash(AttrVal($dev, "setList", "")); + for my $np (split(" ", $param)) { + return "$np ist not of the form cmd:period" if($np !~ m/(.*):(.*)/); + return "$1 is neither a get nor a set command" + if(!$gets->{$1} && !$sets->{$1}); + return "$2 (from $np) is not an integer" if($2 !~ m/^\d+$/); + } + } + RemoveInternalTimer($hash); + $hash->{periodicCounter} = 0 if(!$hash->{periodicCounter}); + InternalTimer(time()+60, "MQTT2_DEVICE_periodic", $hash, 0); + } else { + RemoveInternalTimer($hash); + } + } + return undef; } +sub +MQTT2_DEVICE_periodic() +{ + my ($hash) = @_; + my $name = $hash->{NAME}; + my $param = AttrVal($name, "periodicCmd", ""); + return if(!$param); + my ($gets,undef) = MQTT2_getCmdHash(AttrVal($name, "getList", "")); + my $cnt = ++$hash->{periodicCounter}; + for my $np (split(" ", $param)) { + next if($np !~ m/(.*):(.*)/ || $cnt % int($2)); + my $cmd = $1; + my $ret; + if($gets->{$cmd}) { + $ret = MQTT2_DEVICE_Get($hash, $name, $cmd); + } else { + $ret = MQTT2_DEVICE_Set($hash, $name, $cmd); + } + Log3 $hash, 3, "$name periodicCmd $cmd: $ret" if($ret); + } + InternalTimer(time()+60, "MQTT2_DEVICE_periodic", $hash, 0); +} + sub MQTT2_DEVICE_setBridgeRegexp() { @@ -596,6 +640,8 @@ MQTT2_DEVICE_Undef($$) $modules{MQTT2_DEVICE}{defptr}{cid}{$hash->{CID}} = \@nh; } MQTT2_DEVICE_setBridgeRegexp(); + RemoveInternalTimer($hash->{asyncGet}) if($hash->{asyncGet}); + RemoveInternalTimer($hash) if($hash->{periodicCounter}); return undef; } @@ -951,6 +997,14 @@ zigbee2mqtt_devStateIcon255($;$$) oldReading.
+ +
  • periodicCmd <cmd1>:<period1> <cmd2>:<period2>... +
    + periodically execute the get or set command. The command will not take + any arguments, create a new command without argument, if necessary. + period is measured in minutes, and it must be an integer. +

  • +
  • readingList <regexp> [readingName|perl-Expression] ...