Problems with conversion to integer in a rule -SOLVED

I believe there is an easy solution to conversion a var to integer in a rule but I haven’t figured it out yet. So, my rule is:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: IV1_puhallin_nopeus
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >
        var iv1_Nopeus = IV1_puhallin_nopeus.state as Number

        var iv1_Ilmamaara = (-0.0005*iv1_Nopeus*iv1_Nopeus+0.1036*iv1_Nopeus-2.9167)/2.15*114.3

        IV1_Tulo_ilmamaara.sendCommand(iv1_Ilmamaara)
    type: script.ScriptAction

This works but I would like to convert var iv1_Ilmamaara to integer. So, I tried with code:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: IV1_puhallin_nopeus
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >
        var iv1_Nopeus = IV1_puhallin_nopeus.state as Number

        var Integer iv1_Ilmamaara = (-0.0005*iv1_Nopeus*iv1_Nopeus+0.1036*iv1_Nopeus-2.9167)/2.15*114.3

        IV1_Tulo_ilmamaara.sendCommand(iv1_Ilmamaara)
    type: script.ScriptAction

I just added Integer in the 2nd line in the script. This didn’t work so next trial was:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: IV1_puhallin_nopeus
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >
        var iv1_Nopeus = IV1_puhallin_nopeus.state as Number

        var iv1_Ilmamaara = (-0.0005*iv1_Nopeus*iv1_Nopeus+0.1036*iv1_Nopeus-2.9167)/2.15*114.3

       IV1_Tulo_ilmamaara.sendCommand(iv1_Ilmamaara.state as Integer)
    type: script.ScriptAction

In this case I modified the 3rd line n the script. Didn’t work either. In the last case I get an error:

The method or field state is undefined for the type double; line 3, column 186, length 5
2022-01-13 06:19:26.461 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '3d92ee6a62' failed: var iv1_Nopeus = IV1_puhallin_nopeus.state as Number
var iv1_Ilmamaara = (-0.0005*iv1_Nopeus*iv1_Nopeus+0.1036*iv1_Nopeus-2.9167)/2.15*114.3
IV1_Tulo_ilmamaara.sendCommand(iv1_Ilmamaara.state as Integer)

   The method or field state is undefined for the type double; line 3, column 186, length 5

So, what I’m missing?

The result of your calculation isn’t an integer (nor Integer nor int) so you can’t just say “you’re an Integer now”. You can’t change the type of an Object to another type unless it is already of that type. For example, this is largely nonsense:

val Integer myInt = "5";
val myInt = "5" as Integer;

A String isn’t an Integer. Using as or defining the variable as an Integer doesn’t change the underlying nature of the Object. It is a way to say “I know that this Object is a Command, State, and PercentType but in this case I want to work with is only as a PercentType.” It doesn’t change it into a PercentType. It has to be a PercentType in the first place.

The result of this calculation isn’t an Integer. It’s a BigDecimal. (which is itself a type of Number). To get to an integer, you have to transform the BigDecimal to an integer value. Luckily for you, there’s a method that does that.

iv1_Ilmamaara.intValue

Many thanks. This works. OK, I understand now that trials were wrong.