More Rules issues

I have 2 Pi’s one running a temperature/humidity sensor that controls a dehumidifier using a python script, and the second running OpenHab. Using Mosequitto I am passing the temperature and humidity data to the second pi running my OpenHab environment.

The result I am after is to try and turn on/off a dummy switch if the humidity rises above or drops below a certain level.

In my items I have

Number LivingroomHumidity "Humidity [%.1f %%]" (GF_Living) {mqtt="<[mymosquitto:home/bedroom/humidity:state:default]"} Switch Dehymidifier_GF_Living "Dehumidifier" (GF_Living)

The data is real but the switch is a dummy so wont actually activate anything.
in my rules I have

import org.openhab.core.library.types.*
import org.openhab.core.library.items.SwitchItem

rule "LivingRoomDehumidifierOn"
when
Item LivingroomHumidity received update
then
val temp = LivingroomHumidity.state
val oldstate = Dehymidifier_GF_Living.last.state as OnOffType
if(temp > 52.0 && oldstate=OFF){

    sendCommand(Dehymidifier_GF_Living,ON)
}
if(temp < 48.0 && oldstate=ON){
    sendCommand(Dehymidifier_GF_Living,OFF)
}

end

As the switch is not being activated I am doing something wrong.
Any tips welcome.

1st: val temp = LivingroomHumidity.state as DecimalType
2nd: val oldstate = Dehymidifier_GF_Living.historicState(now.minusSeconds(2)).state as OnOffType

I’m not sure, if this is the correct way :slightly_smiling:
You could use logInfo("myrule","value of temp is {} and value of oldstate is {}",temp,oldstate.toString)
to see if you are getting the correct states.

Ah. You don’t need historicState for Dehymidifier… you can use just Dehymidifier_GF_Living.state as OnOffType

Thanks that helped a lot.
It told me that the dummy switch was in a state of ’ Uninitialized’ so adding a rule that took care of that scenario got the switch to turn on.

With the debug information I also noticed that once turned off the switch would not turn on again which was due to me using a single = rather than a double.

So having changed my rules every thing is now working

rule "LivingRoomDehumidifierOn"
when
Item LivingroomHumidity received update
then
val temp = LivingroomHumidity.state
val oldstate = Dehymidifier_GF_Living.state

    logInfo("myrule","value of temp is {} and value of oldstate is {}",temp,oldstate.toString)

    if(temp > 52.0 && oldstate==Uninitialized){
            sendCommand(Dehymidifier_GF_Living,ON)
    }
    if(temp > 52.0 && oldstate==OFF ){

            sendCommand(Dehymidifier_GF_Living,ON)
    }
    if(temp < 48.0 && oldstate==ON){
            sendCommand(Dehymidifier_GF_Living,OFF)
    }

end