Problems with casting variables in Rules

  • Platform information:
    • Hardware: i5 8Gb
    • OS: Linux Mint 19
    • Java Runtime Environment: 10.0.2
    • openHAB version: 2.5.0 m3

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import java.lang.Integer.*
import java.lang.Double.*
import java.lang.String.*
import java.lang.Math.*
rule “dp”
when
Item T changed or
Item H changed
then
var T = T.state
var H = H.state

var Double T2 = T as Double
var Double H2 = H as Double

dp.postUpdate(Math.log(T2 + H2))
end

and this gives me e.g. Rule ‘dp’: Could not cast 19.58 to java.lang.Double;

I have tried lots of other ‘copy./pastes’ from other peoples scripts that i have googled, but i can’t seem to get anything to work

Any help greatly appreciated

Try:

var T2 = (T as DecimalType).doubleValue

or

var T2 = (T as QuantityType<Number>).doubleValue
1 Like

1st one worked, thanks!

I notice you are running Java 10. OpenHAB is designed to run on Java 8. Anything newer has compatibility issues.

1 Like

BTW you don’t need all these import statements at the to of your rule
If you need them it’s probably because as mentionned above you are not running the recommended correct java version:

3 Likes

Sorry only just saw that i had other messages. Thanks for that, I didn’t realise.