Humidex: Cannot cast org.openhab.core.types.UnDefType to void

Hello,
i try to create HUMIDEX but i have this problem:
[ERROR] [m.r.internal.engine.RuleEngine] - Error during the execution of startup rule ‘Calcolo Humidex’: Cannot cast org.openhab.core.types.UnDefType to void

This is the item:
Number Humidex_miniapp “Temp.apparente [%.1f °C]” (Weather)

This is the rules:
rule "Calcolo Humidex"
when
Item Fibaro_Motion_1_Temp changed or
Item Aeon_motion_humidity changed or
System started
then
var Number T = Fibaro_Motion_1_Temp.state as DecimalType
var Number H = Aeon_motion_humidity.state as DecimalType
var Number x = 7.5 * T/(237.7 + T)
var Number e = 6.112 * Math::pow(10, x.doubleValue) * H/100
var Number humidex = T + (new Double(5) / new Double(9)) * (e - 10)
postUpdate(Humidex_miniapp, humidex);
end

Don’t understand where is the error :frowning:

Thanks in advance!

One or more of your Items are Undefined. They have not been set to a value yet so they are of UnDefType. You will get an error when you try to cast an UnDefType to a DecimalType (or any other type).

You can add a check at the beginning of your rule to make sure that both Items have a State before running the rule:

if(Fibaro_Motion_1_Temp.state != NULL && Aeon_motion_humidity.state != NULL) { // openHAB 2

if(Fibaro_Motion_1_Temp.state != UNDEFINED && Aeon_motion_humidity.state != UNDEFINED) { // openHAB 1.x