OH3 DSL-rule comparing Number-items: type mismatch

While migrating an old OH2 rule to OH3, the following throws an Type-Mismatch error:

if (Astro_SunAzimuth.getState() > 235 && Astro_SunAzimuth.getState() < 245) {
...OR...
if (Astro_SunAzimuth.state > 235 && Astro_SunAzimuth.state < 245) {

in the logs:

2021-05-26 08:50:41.303 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'WebcamNachmittag' failed: if (Astro_SunAzimuth.getState() > 235 && Astro_SunElevation.getState() < 245) {
(...)
}

   Type mismatch: cannot convert from int to String; line 4, column 190, length 1

the item-Definition is:

{
  "link": "http://192.168.xx.yy:8080/rest/items/Astro_SunAzimuth",
  "state": "91.23218728191107",
  "stateDescription": {
    "pattern": "%.0f °",
    "readOnly": true,
    "options": []
  },
  "editable": false,
  "type": "Number",
  "name": "Astro_SunAzimuth",
  "label": "Azimuth Sonne",
  "category": "sun",
  "tags": [],
  "groupNames": [
    "gAstro"
  ]
}

even if I add an “.toInt()” - there’s still an error. What is the correct notation for comparing item-states?

try typecasting: (xxx.state as Number) < 245

if ((Astro_SunAzimuth.state as Number) > 235 && (Astro_SunAzimuth.state as Number) < 245) {

=>

   Type mismatch: cannot convert from int to String; line 4, column 202, length 1

unfortunately no luck, either…

you sure this is the line the error pertains to ? It says column 202 your line is not that long

1 Like

oh shi…
yeah. sorry. :grimacing:

var String preset = 1
has to be
var String preset = "1"

the very next line - next time I’ll be sure to count the lines exactly

Have a coffee first :slight_smile:

1 Like

I know your type is just Number, and the state contents appear to be just a number (no units)
but
Astro binding azimuth channel is a Number:Angle quantity type (it was in OH2 as well).

1 Like

good to know. Presently I’m migrating the rules (as they throw errors all and about after migrating to OH3) and if I’m done with that, I’ll migrate from item-files to GUI-configuration, and then that’s very handy to know!

Yes, this will rear it’s head then. File configs allow you to force, uhh, mis-matches. GUI is more insistent about doing it right.
Such type changes will affect your rules.

1 Like

Most text editors have the ability to tell you which line and column number the cursor is currently on.

image

That from VSCode and it’s at the bottom right.

Many also have the ability to turn on showing the line numbers.

That’s from VSCode.

This is a very useful tool and I recommend all users of OH to enable showing line numbers. Don’t count lines by hand.

Also useful is the ability to jump to a given line number. In VSCode type CTRL + G and type in the line number. To go to line number and column add a comma.

In MainUI, the line numbers are shown to you by default (I don’t think you can turn that off actually). But there isn’t a jump to line number feature that I’m aware of. But UI text blocks will tend to be really short anyway so that’s not too big of a deal.

1 Like