Assuming you have a little knowledge of inheritance and polymorphism…
All Items get injected into Rules DSL as type Item.
The method Item.getState()
returns an Object of type State
.
Therefore, the call to Temperatur.state
returns an Object of type State
(org.openhab.core.types.State
to be specific).
A State
doesn’t support math or comparisons to other types on it’s own.
However, if your Item is a Number
Item, we know that the State
is a DecimalType
. DecimalType
inherits from State
and it also inherits from java.lang.Number
. Therefore we can do math with the state from a Number
Item (assuming it’s not UnDefType
, i.e. NULL
or UNDEF
).
Remember above where I said:
One other trick with Rules DSL and type is that the type of the first operand is used as the type for the operations
In the expression if(Temperatur.state < 1.0)
. The first operand is a State
and the second one is a Number
. So the type fixed for the operation is State
and there is no method implemented anywhere that allows for a State < Number
comparison.
However, you know that the Item is a Number
Item and therefore that State
is actually a DecimalType
which is also a Number
. Therefore you can tell Rules DSL “no, I want this operation to work with Number
s” by making the first operand be the type you want to work with. There are two ways you can do that as demonstrated above: swap the operation so the constant is the first operand, or cast the State
to a version of itself that is usable.
if(1 > Temperatur.state)
if(Temperatur.state as Number < 1)
Rules DSL often punishes you when you try to force the types of things except when it can’t figure out types on it’s own in which case you as the programmer need to know the types of everything to work around it. Are you sure JS is more idiotic than Rules DSL? In comparison I find it way more consistent than Rules DSL and it supports all the standard programming concepts like functions, libraries, etc.
Which version of JS? If JS Scripting, did you try rule builder?
I only create rules in the UI so all I ever really see is the actual JS code anyway. But I find the rule builder to create pretty reasonable rules code.
rules
.when()
.item('HallLight').changed().to("300,100,100")
.then((event) => {
// code goes here
}).build("Description of rule);