Script execution of rule with UID 'xxxxxxx' failed: Could not cast 10 W to org.openhab.core.library.types.DecimalType; line 1, column 23, length 40

I now it has something to do with the breaking change of 3.4.0 (just upgraded…), but don’t know how to fix it.

The items which are used to do some calculations are

Number:Power                mqtt_sofar_pv1Power                 "PV1 Power"                 (sofarsolar)    ["Status","Energy"]    {channel="mqtt:topic:mosquitto2:sofar:pv1Power"}
Number:Power                mqtt_sofar_pv2Power                 "PV2 Power"                 (sofarsolar)    ["Status","Energy"]    {channel="mqtt:topic:mosquitto2:sofar:pv2Power"}

and in the rule I always used started with this:

var Number pv1Power = mqtt_sofar_pv1Power.state as DecimalType
var Number pv2Power = mqtt_sofar_pv2Power.state as DecimalType

But this gives the error: Script execution of rule with UID 'xxxxxxx' failed: Could not cast 10 W to org.openhab.core.library.types.DecimalType; line 1, column 23, length 40

loginfo of item: mqtt_sofar_pv1Power (Type=NumberItem, State=10 W, Label=PV1 Power, Category=null, Tags=[Status, Energy], Groups=[sofarsolar])

output of API

{
  "link": "https://xxxxxxxxx.com/rest/items/mqtt_sofar_pv1Power",
  "state": "0 W",
  "stateDescription": {
    "step": 1,
    "pattern": "%s %unit%",
    "readOnly": true,
    "options": []
  },
  "editable": true,
  "type": "Number:Power",
  "name": "mqtt_sofar_pv1Power",
  "label": "PV1 Power",
  "tags": [
    "Status",
    "Energy"
  ],
  "groupNames": [
    "sofarsolar"
  ]
}

I managed to solve this by changing the item from Number:Power to Number, but is this the way-to-go?

If I use numbers with units…

(item.state as Number).floatValue

This is working too.

(item.state as QuantityType<Number>).doubleValue

Here I can do math and such nice things :slightly_smiling_face:
Greets

Number:Power UoM-typed numbers are QuantityType<Power> of data type so you cannot cast them to a DecimalType.
Use as Number. Avoid .floatValue that’ll create huge processing delays. But it’s a very complex topic, you need to go understand the implications of using UoM, Units of Measurement, read the docs on that.

So my 2 examples should work without the floatValue?
Greets

Depends on context, as I said you have to understand the implications of using UoM. You have to find out yourself.

Ok, I changed my items back to Number:Power and modified my rules to item.state as Number.
Is working!

1 Like