You probably have to cast at least previousState, potentially both that and Nook_Power.state to a Number. The Rules DSL has a hard time figuring this out on its own with DecimalType.
Also, you must reference static methods and data members using ::
, not .
.
As a general rule the sendCommand/postUpdate methods will work better for you than the Actions. The Actions only accept Strings whereas the methods are better able to process commands of different types without doing a lot of background parsing and conversions. For example postUpdate(MyNumberItem, 5)
will probably fail but MyNumberItem.postUpdate(5)
will work.
if(Math::abs(Nook_Power.state as Number - previousState as Number) > 1.5)
Kitchen_NookLight.sendCommand(RefreshType.REFRESH)
Finally, even if you donāt use ESH Designer to do day to day coding it should be the first place you go for any and all syntax errors you encounter in OH. Even if it smells like a syntax error, load it up in ESH Designer and see what it tells you. You will save a ton of time in the long run.
For example, I just pasted my suggested code above into ESH and discovered it wonāt work. By default, the Rules engine puts all calculations into a BigDecimal type but Math::abs needs a primitive. So you must use:
if(Math::abs(Nook_Power.state as Number - previousState as Number).floatValue > 1.5)
Kitchen_NookLight.sendCommand(RefreshType.REFRESH)
The error on the line said something about not being able to convert BigDecimal to int which gave me the clue I needed to find and fix this problem faster than would have been possible with a save, run, watch the logs, interpret the error, find the line in my rule, loop.
ESH Designer also has the usual Eclipse key combos like <ctrl><space>
for automatic line completion. This is really handy for learning about the language as you type.
It is now part of the official docs here.
Iām surprised the Math::abs is working. Maybe that method isnāt static or maybe there has been a change recently.
Yes. See my code above and the link above. That example is the most roundabout way to do it Iāve ever seen.
NOTE: all of the above assumes your Item is a Number Item.