Items with or without ".state"?

Rookie question…
I have seen many different item configurations using “if” with or without .state:
if(SwitchBath == ON) {…
if(SwitchBath.state == ON
if(StringTemp == “25”) {
if(StringTemp.state == “25”) {

What is the proper syntax for the different item types (Switch, String, Number, …?)

State comparison/checks must be always with .state

Thanks for your quick response.
When I use a string as a return value of a script:

var guardmode_tmp = executeCommandLine("/etc/openhab/configurations/scripts/readTeleEyeParam.sh", 5000)
if (guardmode_tmp == "1") {...

I get an error highlighted in OH Designer.
I your point above valid for strings as well?

What is the error?

Try explicitly defining guardmode_tmp as a String

var String guardmode_tmp = executeCommandLine(...

Also, this is a nit but if guardmode_tmp is never reassigned a new value it should be a ‘val’, not a ‘var’. This isn’t the cause of your error but a good coding practice that will save you some heartache later on.

This is working for me

var b = "test"
if (b == "test") logInfo("", "")

When you go over the error symbol (with the mouse) you will see the cause of the error.

All,

the error message for the guardmode_tmp == “1” above is:
Multiple markers at this line

  • Couldn’t resolve reference to JvmIdentifiableElement ‘==’.
  • Couldn’t resolve reference to JvmIdentifiableElement ‘state’.

Without .state it’s ok, but I wonder when a .state is mandatory.
For anything else then a string!?

I will check my entire syntax for var vs. val.
Thanks for the hint @rlkoshak

Whenever you want to read or use the “value” of an Item you always have to use .state. Sometimes, particularly with DateTime Items and Number Items you have to cast the State to a more specific state. For example, if you want to do math with a Number Item’s value you need to do "val Number myNum = MyNum.state as DecimalType.

BUT, you only have to do this when you are referencing Items. Not for locally defined vars ad vals.

Alright, thanks. I will keep this in mind.
BTW: Happy New Year :slight_smile: