[SOLVED] Could not cast wattage to DecimalType or Number for comparison?

Hi there,

If there is one thing I only have problems with then it’s variable types in rules.
I wish there was a simple inspect method that shoes the variable type.

Can anyone tell me how to properly cast this to something that I can compare (<=, etc)?

var pwr = mydevice.state as DecimalType
Could not cast 94.12 W to org.eclipse.smarthome.core.library.types.DecimalType; line 251, column 13, length 76

I thought that at some point I got a debug output saying it’s a NumberItem, if I use as Number then I get:
An error occurred during the script execution: index=1, size=1

Not sure you are giving enough info…for example, what is your item definition for mydevice, it may also help to post your complete rule file, information about your system, etc (see here: How to ask a good question / Help Us Help You).
But one thing sticks out, mydevice.state is reported as 94.12 W this looks like you are using the QuantityType feature or your state is simply a string…

It’s a homematic power socket, I have no idea how to get the complete definition if I haven’t defined the Item in the textfiles.
Wireless Switch Actuator with power measurement Wireless Switch Actuator with power measurement (HM-ES-PMSw1-Pl)

I also tried:
var pwr = homematic_HG_HM_ES_PMSw1_Pl_17b78d3e_LEQ0928492_2_POWER.state as QuantityType<Number>

But then I get:
An error occurred during the script execution: index=1, size=1

PaperUI can give you info about your Item definition. For example is it defined as Number Item?

This rather looks like you may have another issue with your file…as the position refers to the very first character in the file. With some editors there are issues about invisible characters. The easiest way to check is to re-type in something that does not do that (e.g., notepad in windows, vim or nano in linux)

It’s a QuantityType. Try
var Number pwr = (mydevice.state as QuantityType<Number>).doubleValue

1 Like

@Christoph_B just another thought as you still have not posted your whole rule…do you have a logInfo statement with only one argument? logInfo needs two strings and I have seen this error in response to the missing second argument.

1 Like

i just tried again, the error was - as you stated - something else in the script :frowning:
The above version with QuantityType is working!

Thanks a lot for your help!

Hi I try to convert OH2 rules to OH3
Same problem was with casting to DecimalType
I changed to command:
var Number dec = (DailyEnergyConsumption.state as QuantityType).doubleValue
But got an error:
Could not cast 0.00145346 to org.openhab.core.library.types.QuantityType; line 50, column 31, length 52 in power

Any ideas?

don’t reopen old threads but create new ones, please

Technically, you would need to tell OH which QuantityType to cast to such as QuantityType<Number> but your problem is that you totally misunderstood what you need to cast where.
DailyEnergyConsumption is (probably, you failed to tell us) a QuantityType already so you must not cast it into one. You must cast it into a Number because that’s what dec is.