33_readingsProxy Add $SELF for use in setFn & getFn

Begonnen von Nestor, 10 Juli 2019, 14:51:59

Vorheriges Thema - Nächstes Thema

Nestor

Patch included:
--- -   2019-07-10 14:50:00.105431006 +0200
+++ /srv/fhem/FHEM/33_readingsProxy.pm  2019-07-10 14:39:10.073247592 +0200
@@ -252,6 +252,7 @@
   my $v = join(" ", @a);
   my $set_fn = AttrVal( $hash->{NAME}, "setFn", "" );
   if( $set_fn =~ m/^{.*}$/s ) {
+    my $SELF = $hash->{NAME};
     my $CMD = $a[0];
     my $DEVICE = $hash->{DEVICE};
     my $READING = $hash->{READING};
@@ -302,6 +303,7 @@
   my $v = join(" ", @a);
   my $get_fn = AttrVal( $hash->{NAME}, "getFn", "" );
   if( $get_fn =~ m/^{.*}$/s ) {
+    my $SELF = $hash->{NAME};
     my $CMD = $a[0];
     my $DEVICE = $hash->{DEVICE};
     my $READING = $hash->{READING};
@@ -334,6 +335,7 @@
   if( $cmd eq "set" ) {
     if( $attrName eq 'getFn' || $attrName eq 'setFn' || $attrName eq 'valueFn' ) {
       my %specials= (
+        "%SELF" => $name,
         "%CMD" => $name,
         "%DEVICE" => $name,
         "%READING" => $name,

justme1968

thanks for the patch.

you can already use $name for this.

is there a reason you prefer the $SELF?

by the way: $name also works in most other places.
hue, tradfri, alexa-fhem, homebridge-fhem, LightScene, readingsGroup, ...

https://github.com/sponsors/justme-1968

Nestor

#2
I get following error when using $name in setFn. $name works in stateFormat & devStateIcon.

Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at (eval 52652) line 1.


justme1968

hue, tradfri, alexa-fhem, homebridge-fhem, LightScene, readingsGroup, ...

https://github.com/sponsors/justme-1968

Nestor

#4
Any update on this issue?

Is it possible the variable scope is different since you're capturing the curly braces in the regex and doing a block eval (eval {})?
https://github.com/fhem/fhem-mirror/blob/master/fhem/FHEM/33_readingsProxy.pm#L254

Curly braces are not captured with the regex in the stateFormat eval:
https://github.com/fhem/fhem-mirror/blob/master/fhem/fhem.pl#L4624

Gary

Old thread, sorry to reopen - but just stumbled across the same error. So mybe still a problem and not fixed?

When I use global variable $name in SetFn and ValueFn I get the same error like Nestor above (meaning not defined in this context).

Tried to do e.g. this

attr Garagentor setFn {my $s=$CMD." ".$ARGS; $s =~ s/pct //; fhem("setreading $name state $s"); undef}

Did I miss something?

TomLee

There is no $name in readingsProxy but $DEVICE

setFn
perl expresion that will return the set command forwarded to the parent device. has access to $CMD, $DEVICE, $READING and $ARGS.


attr Garagentor setFn {my $s=$CMD." ".$ARGS; $s =~ s/pct //; fhem("setreading $DEVICE state $s"); undef}

Gary

Thank you for your answer.

I thought $DEVICE was the name of the parent device.

But I would like to refer to the name of the ReadingsProxy-Device (as $name should usually give the name of the calling device itself).

Nestor

It only works with the patch (which I'm using for a few years now) afaik.