Unexpected value returned from rule with '>'

I want to upgrade my rule which turns off my bathroom ventilation when the outdoor humidity is higher then the bathroom’s:

rule "Badkamer ventilatie"
when
        Item gHumidityBadkamer changed
then
        // 1. Check to see if the Rule has to run at all, if not exit.
        if(gHumidityBadkamer.state == NULL ){
            logWarn("Ventilation Function", gHumidityBadkamer.name.toString + gHumidityBadkamer.state.toString)
            return;
        }
        
        // 2. If Humid is above 65% turn on Ventilation at highest setting
        if(gHumidityBadkamer.state == UNDEF ){
            logWarn("Ventilation Function", gHumidityBadkamer.name.toString + gHumidityBadkamer.state.toString)
            VentilatieBadkamer.sendCommand(OFF)
        } else if(gHumidityBadkamer.state <= 60 && VentilatieBadkamer.state == OFF) {
            logInfo("Ventilation Function", "b1. Humidity Badkamer is " + gHumidityBadkamer.state.toString + " and " + "ventilation = " + VentilatieBadkamer.state.toString + ". No action needed.")
        } else if(gHumidityBadkamer.state <= 60 && VentilatieBadkamer.state == ON) {
            logInfo("Ventilation Function", "b2. Humidity Badkamer is " + gHumidityBadkamer.state.toString + " and " + "ventilation = " + VentilatieBadkamer.state.toString + ". Turning ventilation OFF.")
            VentilatieBadkamer.sendCommand(OFF)
        //} else if (gHumidityBadkamer.state >= 70 && VentilatieBadkamer.state != ON) {
        } else if ((gHumidityBadkamer.state as Number) > (WeatherAndForecastCurrentHumidity.state as Number) && VentilatieBadkamer.state != ON) {
            logInfo("Ventilation Function", "b3a. Humidity Badkamer is " + gHumidityBadkamer.state.toString + " and " + "ventilation = " + VentilatieBadkamer.state.toString + ". Turning ventilation ON.")
            logInfo("Ventilation Function", "b3b. Humidity outside is " + WeatherAndForecastCurrentHumidity.state.toString)
            VentilatieBadkamer.sendCommand(ON)
        } else if (gHumidityBadkamer.state >= 65 && VentilatieBadkamer.state == ON && WeatherAndForecastCurrentHumidity.state >= 65) {
            logInfo("Ventilation Function", "b4. Humidity Badkamer is " + gHumidityBadkamer.state.toString + " and " + "ventilation = " + VentilatieBadkamer.state.toString + " WeatherAndForecastCurrentHumidity is: " + WeatherAndForecastCurrentHumidity.state.toString + ". Turning ventilation OFF.")
            VentilatieBadkamer.sendCommand(OFF)
        }
end

This results in:

[INFO ] [me.model.script.Ventilation Function] - b3a. Humidity Badkamer is 82.12 and ventilation = OFF. Turning ventilation ON.
[INFO ] [me.model.script.Ventilation Function] - b3b. Humidity outside is 93 %

So it is saying 82.12 is greater then 93?

I’ve tried it without as Number and on openHAB 2.4.0 and 2.5.0.M3.

There’s the clue. You didn’t ask for a % sign in your logInfo - it has come from the Item WeatherAndForecastCurrentHumidity
That Item has Units of Measurement.
(check your Item definitions)
While your gHumidityBadkamer does not have any units.
So a comparison will be apples and oranges, not like for like.

1 Like

I would suggest to first calculate the absolute humidity for outside and indoors and compare those values, because the comparison of the relative humidity doesn‘t really tell you what you want to know.

1 Like

Thank you @rossko57, I missed that.

I will look in to that @Syn.

Not quite there yet. I changed my code to:

} else if ((gHumidityBadkamer.state as Number).intValue > (WeatherAndForecastCurrentHumidity.state as QuantityType<Number>).intValue && VentilatieBadkamer.state != ON) {
    logInfo("Ventilation Function", "b3a. Humidity Badkamer is " + gHumidityBadkamer.state.toString + " and " + "ventilation = " + VentilatieBadkamer.state.toString + ". Turning ventilation ON.")
    logInfo("Ventilation Function", "b3b. Humidity outside is " + WeatherAndForecastCurrentHumidity.state.toString)
    VentilatieBadkamer.sendCommand(ON)

But the rule doesn’t trigger anymore. There are no errors in the log.