FHEM Forum

FHEM => Automatisierung => Thema gestartet von: Nestor am 10 Juli 2019, 14:51:59

Titel: 33_readingsProxy Add $SELF for use in setFn & getFn
Beitrag von: Nestor am 10 Juli 2019, 14:51:59
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,
Titel: Antw:33_readingsProxy Add $SELF for use in setFn
Beitrag von: justme1968 am 10 Juli 2019, 18:07:33
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.
Titel: Antw:33_readingsProxy Add $SELF for use in setFn
Beitrag von: Nestor am 10 Juli 2019, 19:24:13
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.

Titel: Antw:33_readingsProxy Add $SELF for use in setFn
Beitrag von: justme1968 am 10 Juli 2019, 19:55:09
i will check
Titel: Antw:33_readingsProxy Add $SELF for use in setFn & getFn
Beitrag von: Nestor am 24 Juli 2019, 17:53:10
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
Titel: Antw:33_readingsProxy Add $SELF for use in setFn & getFn
Beitrag von: Gary am 17 Mai 2021, 16:13:33
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?
Titel: Antw:33_readingsProxy Add $SELF for use in setFn & getFn
Beitrag von: TomLee am 17 Mai 2021, 18:23:26
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}
Titel: Antw:33_readingsProxy Add $SELF for use in setFn & getFn
Beitrag von: Gary am 17 Mai 2021, 19:05:52
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).
Titel: Antw:33_readingsProxy Add $SELF for use in setFn & getFn
Beitrag von: Nestor am 17 Mai 2021, 20:10:08
It only works with the patch (which I'm using for a few years now) afaik.