Problem with rule after OH upgrade from 4.1.1 to 4.2.3

After OH upgrade I have a problem with error in the log :

   1. Type mismatch: cannot convert from State to String; line 15, column 632, length 66
   2. Type mismatch: cannot convert from State to String; line 20, column 1116, length 94
   3. Type mismatch: cannot convert from State to String; line 21, column 1314, length 67
   4. Cannot cast from String to Number; line 15, column 703, length 6
   5. Cannot cast from String to Number; line 20, column 1215, length 6
   6. Cannot cast from String to Number; line 21, column 1386, length 6

and rule:

question is what changed, that rule is no longer working.
Is problem delta since ?
Or?

thank you
Alex

Try casting the state
Line 15:

……deltaSince() as Number + …… deltaSince() as Number

super, that was a problem
I had as Number on the end. Before it was ok, but after upgrade must be as Number after each.

I can read the code from a screen shot (in the future please post the actual code in code fences).

There really isn’t anything that’s changed between 4.1 and 4.2 that should have broken this, but Rules DSL 's type system is kind of broken. Maybe the update to the Xtend library hit you.

As a rule, when performing operations in Rules DSL, the type of the first operands is used as the base and Rules DSL tries to coerse the second operand to match the first. But it only looks up the type hierarchy, never down. So if you have the following:

5 + MyNumber.state

Rules DSL will fail with an error along the lines of “cannot convert State to Number” or an even more cryptic null exception. .state is always going to be if type State.

That’s why you need the as Number. Since Rules DSL doesn’t look down, you have to help it by telling it what subtype the state is.

Other “fun”.

logInfo("foo", MyItem.state + " is an item state")

this line fails.

logInfo("foo", "The Item's state is " + MyItem.state)

this line succeeds.

It’s frustrating and the main reason I no longer recommend new development of rules in Rules DSL. I’m not recommending chasing existing rules, but consider one of the other options for new rules.

1 Like

ok
For the future, what is recommended development language for rules ?
When I have time, I will also try rework my old rules to the new language.
Thank you

Any of them are good. If you are not an experienced programmer I recommend Blockly. JS Scripting is probably the most popular with jRuby close behind. Maybe try a few and see which one(s) you like best. I use JS Scripting but that’s mainly because it was ready to support UI rules before the others.

But pay close attention to my wording. you didn’t have to rewrite what you already have working unless you want to. I only recommend a different language for new rules development. Don’t set yourself up for a job bigger than you want to do.

ok, as default I have only choise DSL or Blockly, so for the future I will use a Blockly if it’s more recommended than DSL.

What version of OH are you running? I don’t think you should have Blockly as a default any more.

In the future you won’t have anything as a default. You will have to choose and install the langauge you need as an add-on. Blockly comes with JS Scripting.

Few days ago I did a upgrade from 4.1.1. to 4.2.3