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

(Christoph B) #1

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


(lipp_markus) #2

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…


(Christoph B) #3

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


(lipp_markus) #4

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)


(Markus Storm) #5

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

1 Like

(lipp_markus) #6

@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

(Christoph B) #7

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!