[gelöst] Syntax Error (Variablen deklarieren)

Begonnen von Claus1985, 17 August 2019, 17:53:42

Vorheriges Thema - Nächstes Thema

Claus1985

Hi Zusammen,

da Loxone nicht in der Lage ist ganze Strings per UDP zu verarbeiten (nur den ersten Buchstaben),
möchte ich meine UDP Nachricht in FHEM entsprechend über myUtils99 anpassen.
Nun habe ich das Mini-Programm zwar soweit aber ich scheitere an einem Syntax Error.

Mein Code sieht aus wie folgt:

#NToLoxone
sub NToLoxone($)
{
my ($device) = @_;
my ($lockst) = ReadingsVal("$device","lockState","-1");

if ($lockst eq "lock") || ($lockst eq "locked") {
$lockst = "1";
}
elsif ($lockst eq "unlocked") || ($lockst eq "unlock"){
$lockst = "0";
}
elsif ($lockst eq "unlatched") {
$lockst = "2";
}
elsif ($lockst eq "unlocking") || ($lockst eq "unlatching") {
$lockst = "3";
}
elsif ($lockst eq "locking") {
$lockst = "4";
}
elsif ($lockst eq "uncalibrated") {
$lockst = "5";
}
elsif ($lockst eq "unlocked (lock 'n' go)") {
$lockst = "6";
}
elsif ($lockst eq "motor blocked") {
$lockst = "7";
}
elsif ($lockst eq "undefined") {
$lockst = "8";
}

my $batterie = ReadingsVal("$device","batteryState","-1");

if ($batterie eq "ok") {
$batterie = "1";
}
else {
$batterie = "0";
}
UDP_Msg("192.168.0.5" , "7000" , "$device: $lockst $batterie");
}


Offensichtlich mache ich etwas mit dem Deklarieren der Variablen falsch und bekomme daher den
Fehler:
ZitatGlobal symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?)

Ich habe mir ein paar Tutorials angeschaut und verschiedenes probiert, bekomme das aber nicht gelöst.
Habt Ihr einen Tipp für mich wie ich den Fehler korrigieren kann?

Danke Euch!

Gruß,

Claus

rischbiter123

Moin,
bei my ($lockst) = ReadingsVal("$device","lockState","-1");
ohne Klammern um $lockst.

LG
Andreas
4*Raspi, Max Thermostate und Fensterkontakte, FB7590, Mysensors und NanoCUL, IT und Sonoff, zigbee2mqtt2

Claus1985

Hi Andreas,

danke für den Tip, war allerdings nicht die Lösung,
bekomme noch die gleiche Fehlermeldung.

Gruß,

Claus

Otto123

Hi,

muss nicht die gesamte Bedingung geklammert werden?

()||() -> (()||())

Gruß Otto
Viele Grüße aus Leipzig  ⇉  nächster Stammtisch an der Lindennaundorfer Mühle
RaspberryPi B B+ B2 B3 B3+ ZeroW,HMLAN,HMUART,Homematic,Fritz!Box 7590,WRT3200ACS-OpenWrt,Sonos,VU+,Arduino nano,ESP8266,MQTT,Zigbee,deconz

Claus1985

Hi Otto,

hat leider auch nichts genutzt, Fehlermeldung kommt nach wie vor :-/

Gruß,

Claus

KölnSolar

Hallo Claus,
ich vermute das Problem liegt hier"unlocked (lock 'n' go)") mach es mal testweise ohne die Sonderzeichen. Richtig ist es vermutlich so "unlocked \(lock 'n' go\)")
Grüße Markus
RPi3/2 buster/stretch-SamsungAV_E/N-RFXTRX-IT-RSL-NC5462-Oregon-CUL433-GT-TMBBQ-01e-CUL868-FS20-EMGZ-1W(GPIO)-DS18B20-CO2-USBRS232-USBRS422-Betty_Boop-EchoDot-OBIS(Easymeter-Q3/EMH-KW8)-PCA301(S'duino)-Deebot(mqtt2)-zigbee2mqtt

Claus1985

Hi Markus,

hat leider nicht geklappt, hab es auch komplett rausgenommen - ohne Erfolg.

Zitatsyntax error at ./FHEM/99_myUtils.pm line 58, near ") ||" syntax error at ./FHEM/99_myUtils.pm line 61, near "elsif" Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 61. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 61. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 62. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 64. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 65. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 67. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 67. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 68. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 70. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 71. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 73. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 74. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 76. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 77. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 79. Global symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 80. syntax error at ./FHEM/99_myUtils.pm line 92, near "}" ./FHEM/99_myUtils.pm has too many errors.

Das Problem tritt laut FHEM auch in jeder Zeile auf  :o

Gruß,

Claus

Claus1985

Hi Zusammen,

habs hinbekommen, lag nicht an der Variablen sondern an den || in den If Bedingungen.
So funktionierts - wenn auch ineffizient:

#NukiToLoxone
sub NukiToLoxone($)
{
my ($device) = @_;
my $lockst = ReadingsVal("$device","lockState","-1");

if ($lockst eq "lock") {
$lockst = "1";
}
elsif ($lockst eq "locked") {
$lockst = "1";
}
elsif ($lockst eq "unlock") {
$lockst = "0";
}
elsif ($lockst eq "unlocked") {
$lockst = "0";
}
elsif ($lockst eq "unlatched") {
$lockst = "2";
}
elsif ($lockst eq "unlocking") {
$lockst = "3";
}
elsif ($lockst eq "unlatching") {
$lockst = "3";
}
elsif ($lockst eq "locking") {
$lockst = "4";
}
elsif ($lockst eq "uncalibrated") {
$lockst = "5";
}
elsif ($lockst eq "unlocked (lock 'n' go)") {
$lockst = "6";
}
elsif ($lockst eq "motor blocked") {
$lockst = "7";
}
elsif ($lockst eq "undefined") {
$lockst = "8";
}

my $batterie = ReadingsVal("$device","batteryState","-1");

if ($batterie eq "ok") {
$batterie = "1";
}
else {
$batterie = "0";
}
UDP_Msg("192.168.178.77" , "7000" , "$device: $lockst $batterie");
}


Gruß,

Claus

KölnSolar

Da hast Du meinen Ehrgeiz geweckt. So gehts#NToLoxone
sub NToLoxone($)
{
my ($device) = @_;
my ($lockst) = ReadingsVal("$device","lockState","-1");

if ($lockst eq "lock" || $lockst eq "locked") {
$lockst = "1";
}
elsif ($lockst eq "unlocked" || $lockst eq "unlock"){
$lockst = "0";
}
elsif ($lockst eq "unlatched") {
$lockst = "2";
}
elsif ($lockst eq "unlocking" || $lockst eq "unlatching") {
$lockst = "3";
}
elsif ($lockst eq "locking") {
$lockst = "4";
}
elsif ($lockst eq "uncalibrated") {
$lockst = "5";
}
elsif ($lockst eq "unlocked (lock 'n' go)") {
$lockst = "6";
}
elsif ($lockst eq "motor blocked") {
$lockst = "7";
}
elsif ($lockst eq "undefined") {
$lockst = "8";
}

my $batterie = ReadingsVal("$device","batteryState","-1");

if ($batterie eq "ok") {
$batterie = "1";
}
else {
$batterie = "0";
}
UDP_Msg("192.168.0.5" , "7000" , "$device: $lockst $batterie");
}

(Ottos u. meinen Hinweis umgesetzt)
RPi3/2 buster/stretch-SamsungAV_E/N-RFXTRX-IT-RSL-NC5462-Oregon-CUL433-GT-TMBBQ-01e-CUL868-FS20-EMGZ-1W(GPIO)-DS18B20-CO2-USBRS232-USBRS422-Betty_Boop-EchoDot-OBIS(Easymeter-Q3/EMH-KW8)-PCA301(S'duino)-Deebot(mqtt2)-zigbee2mqtt

Otto123

genau, hab auch gerade nochmal meine Variante getestet. Ich nehme dazu immer eine abgespeckte Variante in der FHEM Kommandozeile.
Das geht, obwohl die Klammern um die "eq" Vergleiche überflüssig sind :)
{my $lockst= "unlock1";;if (($lockst eq "unlocked") || ($lockst eq "unlock")){return "wahr"} else {return "falsch"}}

Gruß Otto
Viele Grüße aus Leipzig  ⇉  nächster Stammtisch an der Lindennaundorfer Mühle
RaspberryPi B B+ B2 B3 B3+ ZeroW,HMLAN,HMUART,Homematic,Fritz!Box 7590,WRT3200ACS-OpenWrt,Sonos,VU+,Arduino nano,ESP8266,MQTT,Zigbee,deconz

Christoph Morrison

Sauberer wäre es, wenn du die Status des Devices in ein Array als Key mit dem Return-Wert als Value definierst und dann einfach nur den entsprechenden Key abfragst.
Diese ganzen if/elseif/yada / switch / given/when-Dinger sind mir so ein Graus.

So ungefähr (ungetestet):

use strict;

my $lockstate = ReadingsVal("$device", "lockState", -1);
my %lockst = (
"lock"                 => 1,
"locked"                 => 1,
"unlocked"                 => 0,
"unlock"                 => 0,
"unlatched"                 => 2,
"unlocking"                 => 3,
"unlatching"                => 3,
"locking"                 => 4,
"uncalibrated"              =>  5,
"unlocked (lock 'n' go)"    =>  6,
"motor blocked"             =>  7,
"undefined"                 => 8,
);

my $state = (defined $lockst{$lockstate}) ? $lockst{$lockstate} : "-1";

Claus1985

Hi Christoph,

finde die Idee mit dem Array sehr gut.
Dabei bekomme ich allerdings den guten alten Error:
ZitatGlobal symbol "$lockst" requires explicit package name (did you forget to declare "my $lockst"?) at ./FHEM/99_myUtils.pm line 80.

Frage zum Verständnis: Warum hast Du im letzten Satz
my $state = (defined $lockst{$lockstate}) ? $lockst{$lockstate} : "-1";

eine neue Variable $state definiert? Würde das wenn es geht gerne direkt in $lockst übergeben.

Danke und Gruß,

Claus

Otto123

#12
Hallo Claus,

Hinweis zur Fehlersuche:
da die declaration je am Anfang gemacht wurde, deuten solche Meldungen häufig auf nicht abgeschlossene Befehle ( ; ) nicht geschlossen Klammern, fehlende Klammern um Blöcke usw.

In der Meldung steht ja glücklicherweise exakt die Zeile (80) also liegt der Fehler in dieser Zeile oder davor. Wenn Du es nicht selbst findest, würde es uns helfen wenn die diesen Bereich postest mit der Angabe welche Zeile Nummer 80 ist. :)

Gruß Otto
Viele Grüße aus Leipzig  ⇉  nächster Stammtisch an der Lindennaundorfer Mühle
RaspberryPi B B+ B2 B3 B3+ ZeroW,HMLAN,HMUART,Homematic,Fritz!Box 7590,WRT3200ACS-OpenWrt,Sonos,VU+,Arduino nano,ESP8266,MQTT,Zigbee,deconz

Christoph Morrison

Zitat von: Claus1985 am 18 August 2019, 10:31:28
finde die Idee mit dem Array sehr gut.
Dabei bekomme ich allerdings den guten alten Error:

Dann hast du irgendwo einen Synxtax-Error fabriziert, schätze mal du greifst auf $lockst zu (Skalar, nicht %lockst) ohne $lockst deklariert zu haben.

Zitat von: Claus1985 am 18 August 2019, 10:31:28
Frage zum Verständnis: Warum hast Du im letzten Satz
my $state = (defined $lockst{$lockstate}) ? $lockst{$lockstate} : "-1";

eine neue Variable $state definiert? Würde das wenn es geht gerne direkt in $lockst übergeben.

Du kannst natürlich $lockst und %lockst gleichzeitig benutzen, aber ich halte das für schlechten Stil. Deshalb gibt es $state. Alternativ kann man das auch direkt in UDP_Msg() zusammenbauen, aber das war mir zu viel zu schreiben, Nachts um 11.

Claus1985

Hi Ihr zwei,

danke für die Hinweise, mit $state funktioniert es einwandfrei.
War bisher der Meinung, dass $state schon eine Art Systemvariable in FHEM ist und
wollte jegliche Komplikationen vermeiden. Daher habe ich bisher immer auf eigene
Variablen zurückgegriffen.

Gruß,

Claus