Rule syntax: correctness?

I have this rule, which works, but designer says:
Type Mismatch: cannot convert from state to number.

    if ((Battery_SOC.state <= spp_GridSupportSocLevelMin.state) && (spp_GridSupportManual.state == OFF)) {
    ...
    }

However, both Battery_SOC and spp_GridSupportSocLevelMin are defined as number items.

It I remove the .state part on the first two, Designer then complains: <= cannot be resolved.

Should I care? Well, I’d like to.
What is wrong?

The answer is:

    val Number minSoc = spp_GridSupportSocLevelMin.state as DecimalType
    if ((Battery_SOC.state <= minSoc) && (spp_GridSupportManual.state == OFF)) { ... }

The Rules engine has a hard time with DecimalType/Number conversion. Little problems like these pop up for me all the time. I have started to just always cast the state of a NumberItem to Number by default everywhere I use it and the problems stop popping up.

This is essentially what you did in a round about way. You could have also just done:

val minSoc - spp_GridSupportSocLevelMin.state as Number
2 Likes

Designer doesn’t like Item.state as Number - but that can be ignored

1 Like

Thank you Rich!
I want to explicitly thank you for your effort you are putting into the user community. As far as I am concerned, I would have given OH the boot a long time ago. It is with articles like this sendCommand() Documentation which provide very useful information and clarity to me and others.
THANK YOU!

2 Likes

:flushed: I’m happy to help. I just wish I could focus more and turn all of these adhoc postings into a coherent document to add to the docs. I just don’t know how yet, nor have I the time.

I’m really good at answering specific questions. But put a blank screen in front of me and say “write” and I’m stymied.

I’m really glad you find my postings useful and if they keep just one OH user from giving up they are worth while.

2 Likes

Don’t stress about ‘not being able to do better’… I would suggest, if you ever write up a concise version, make a book out of it. I can see it becoming THE bible for OH for Dummies :smiley: