Calculate temperature real feel

  • Platform information:
    • Hardware: i5/12GB/HDD
    • OS: Windows 10 64bit
    • Java Runtime Environment: 17.0.9 (Zulu17.46+19-CA)
    • openHAB version: 4.0.3
  • Issue of the topic: Hello, I use script from another topic to calculate temperature real feel. Unfortunately it returns an error:
Type mismatch: cannot convert from BigDecimal to int; line 15, column 678, length 4
  • Rules code related to the issue:
var Number Temp = (Weather_Station_Outdoor_Temperature.state as QuantityType<Number>).doubleValue //as DecimalType
    var Number Humity = (Weather_Station_Humidity_Outside.state as QuantityType<Number>).doubleValue //as DecimalType

    var Number T = Temp * 9/5 + 32
    var Number RH = Humity * 100
    var Number HI = 0;

    if(T <= 40.0) {
        HI = T
    } else {
        HI = -42.379 + 2.04901523*T + 10.14333127*RH - 0.22475541*T*RH - 0.00683783*T*T - 0.05481717*RH*RH + 0.00122874*T*T*RH + 0.00085282*T*RH*RH - 0.00000199*T*T*RH*RH

        if (RH < 13 && T >= 80 && T <= 112) {
            var adjust = ((13-RH)/4) * Math.sqrt(17-Math.abs(T-95)/17)
            HI = HI - adjust
        } else if (RH > 85 && T >= 80 && T <= 87) {
            var adjust = ((RH-85)/10) * ((87-T)/5)
            HI = HI + adjust
        } else if (T < 80){
            HI = 0.5 * (T + 61.0 + ((T-68.0)*1.2) + (RH*0.094))
        }
    }

    var Number FeelTemp = (HI - 32) * 5/9

    postUpdate(PocitovaTeplota,FeelTemp)

First of all, I think Weather Calculations [4.0.0;4.2.0) can handle this for you. You may not need this rule at all.

Don’t force the types of variables unless and until absolutely necessary. One of the places where it is necessary is when calling Math. functions which all require primitives.

    var Temp = (Weather_Station_Outdoor_Temperature.state as QuantityType<Number>).doubleValue 
    var Humity = (Weather_Station_Humidity_Outside.state as QuantityType<Number>).doubleValue 

    var T = Temp * 9/5 + 32
    var RH = Humity * 100
    var HI = 0;

    if(T <= 40.0) {
        HI = T
    } else {
        HI = -42.379 + 2.04901523*T + 10.14333127*RH - 0.22475541*T*RH - 0.00683783*T*T - 0.05481717*RH*RH + 0.00122874*T*T*RH + 0.00085282*T*RH*RH - 0.00000199*T*T*RH*RH

        if (RH < 13 && T >= 80 && T <= 112) {
            var adjust = ((13-RH)/4) * Math.sqrt((17-Math.abs(T-95)/17).doubleValue)
            HI = HI - adjust
        } else if (RH > 85 && T >= 80 && T <= 87) {
            var adjust = ((RH-85)/10) * ((87-T)/5)
            HI = HI + adjust
        } else if (T < 80){
            HI = 0.5 * (T + 61.0 + ((T-68.0)*1.2) + (RH*0.094))
        }
    }

    var FeelTemp = (HI - 32) * 5/9

    PocitovaTeplota.postUpdate(FeelTemp)

Some other things of note:

  • Why are you converting to °F manually? Assuming Weather_Station_Outdoor_Temperature is a Number:Temperature, the whole point of units is so that you don’t have to do these sorts of conversions.

    var T = (Weather_Station_Outdoor_Temperature.state as QuantityType).toUnit(‘°F’).doubleValue

  • Even better, set the unit of the Item to °F and the value will be converted before the Item is even updated. That’s kind of the point of units in the first place.

  • Why are you multiplying the humidity by 100? Assuming Weather_Station_Humidity_Outside is a Number:Dimensionless, given this Item represents a percent the unit metadata should be set to %. Then the value will be between 0-100, not 0.0 and 1.0. And of course you can force it in your rule.

    var RH = (Weather_Station_Humidity_Outside.state as QuantityType).toUnit(‘%’)

  • If you’d rather not deal with units in the first place, then don’t use them in the first place. Change your Items to be just Number and you won’t have to worry about units. Of course it will be up to you do handle conversions and such yourself if you do that.