How to read a "string item" in a dsl rule?

I would like to read a string of an item in a DSL rule. Therefore I opened a new DSL rule directly in the UI of OH4.1 and wrote this:

logInfo("Aussentemperature", "Aussentemperatur ist: " + Wetterstation_Dach_Aussentemperatur.state.toString )

if (items.getItem('Wetterstation_Dach_Rain').state == 'NEIN') {
	logInfo("Aussentemperature", "Aussentemperatur ist: " + Wetterstation_Dach_Rain )
	}

but I get following error:

if (items.getItem('Wetterstation_Dach_Rain').state == 'NEIN') {
	logInfo("Aussentemperature", "Aussentemperatur ist: " + Wetterstation_Dach_Rain )
	}

   The method or field items is undefined; line 4, column 115, length 5

Could someone pls tell me how I can access this item?

Thanks

Just as you do in the logInfo call:

Wetterstation_Dach_Aussentemperatur.state.toString

needs to be

logInfo("Aussentemperature", "Aussentemperatur ist: " + Wetterstation_Dach_Rain.state )

it does not work even I change the line. The error is on the line#4

if (items.getItem('Wetterstation_Dach_Rain').state == 'NEIN') 

How to I check if this string item is "NEIN" or "JA" correctly?
if (Wetterstation_Dach_Rain.state == 'NEIN') 

As to my understanding, if you are using toString method, you need parenthesis toString(). I have seen weird messages/results when omitting them. But you might be dropping toString method entirely, depending on the item‘s type.

To be on the safe side and to put it alltogether, incl. Jims comment:

logInfo("Aussentemperature", "Aussentemperatur ist: " + Wetterstation_Dach_Aussentemperatur.state.toString())

if (Wetterstation_Dach_Rain.state.toString() == 'NEIN') {
	logInfo("Aussentemperature", "Aussentemperatur ist: " + Wetterstation_Dach_Rain.state.toString())
}

When calling logInfo it’s best to use parameter placeholders.

So don’t do this:

logInfo("Aussentemperature", "Aussentemperatur ist: " + Wetterstation_Dach_Aussentemperatur.state.toString())

Instead, Do this:
logInfo("Aussentemperature", "Aussentemperatur ist: {}", Wetterstation_Dach_Aussentemperatur.state)

You don’t need to call toString because logInfo will do that for you.

You also don’t need to cal toString because this will work fine:
"Aussentemperatur ist: " + Wetterstation_Dach_Rain.state

RulesDSL doesn’t require () after toString although adding it also works.

In any case you simply try it and see.

Lastly, consider switching to jruby (my preference) or javascript instead of rulesdsl.

You can see a comparison of the different syntaxes here

thanks for the answers! It works now (see solution)

Hello I have no idea what happend but it turned out this expression in OH4.1 (windows11) does not work (anymore). I use the inline script “rule DSL”.

if (items.getItem('Wetterstation_Dach_Rain').state == 'Nein') {
  items.getItem('KNX_Device_Aktor_A3_Kuche_Licht_Bar').sendCommand('OFF');
}

I made the same with blockly which gives me this working (!) code:

if (items.getItem('Wetterstation_Dach_Rain').state == 'Nein') {
  items.getItem('KNX_Device_Aktor_A3_Kuche_Licht_Bar').sendCommand('OFF');
}

The error message for the first snipped is:

if (items.getItem('Wetterstation_Dach_Rain').state == 'Nein') {
  items.getItem('KNX_Device_Aktor_A3_Kuche_Licht_Bar').sendCommand('OFF');  
}
 
   The method or field items is undefined; line 1, column 229, length 5

What is wrong with the “if”?

Thanks!

RulesDSL is not the same as Blockly. Blockly translates into Javascript, not rulesdsl.

The rulesdsl version of the above code is

if (Wetterstation_Dach_Rain.state == "Nein") {
  KNX_Device_Aktor_A3_Kuche_Licht_Bar.sendCommand(OFF)
}

Thanks a lot - it works now (again)!!!

:slight_smile: