[SOLVED] Some Noobie Syntax Questions

It doesn’t matter. In the Rules DSL syntax, if you have a function call that doesn’t take arguments (i.e. has ()) the () are optional.

The white space has no meaning to the language so use spaces in a way that makes it easier to read the code. I prefer the version with spaces.

That’s because you have a stray . after “state”.

This version is correct:

if(Presence_Status_Bedtime.state == ON)

Compared to what you have:

if(Presence_Status_Bedtime.state. == ON)

Numbers are a challenge in Rules DSL. Without going too deep into things, when doing math or comparisons, you need to tell the Rules DSL that the Item’s state is a Number.

if((HallLightSensor_Illuminance.state as Number) < 60) {

It becomes event more complicated if HallLightSensor_Illuminance is more than just a Number, e.g. Number:Illuminance. In that case for comparisons you need to tell what units you are using.

if(HallLightSensor_Illuminance.state < 60|lux){

That would not be an appropriate use of the ===, though I think it will work. Typically though you only want to use === or !== when you literally have null on either side. In all other cases you want to use == and !=.

See Trrying to turn a device off after a certain amount of time - #6 by rlkoshak for a post I made where I showed the difference with an example.

I think it is better to convert the constant to a UoM rather than converting the state in a comparison like this. That way the units are right there in your code, you have the option of mixing your units (e.g. the Item may be storing °C but you want to compare it with 60|°F). It isn’t always apparent what units an Item’s state is actually stored in and when you just call intValue you get the default units and you need to figure out what those units are and potentially do the math yourself to convert your constant to the right units, which defeats the purpose of UoM in the first place.

Finally, I’m not sure just calling intValue() will always work. Rules DSL has a lot of problems recognizing Number type States. You may need something along the lines of

if((HallLightSensor_Illuminance.state as QuantityType<Number>).intValue() < 60)
1 Like