- Platform information: openHABian
- Hardware: _Raspberry Pi 3
- openHAB version: 2.4.0
- Issue of the topic: rule for temperature control of brewing fermenter
Hi Guys,
Hoping to get some help with this rule I’m trying to implement to control both heating and cooling of my brewing fermenter. The fermenter sits in a fridge which does the cooling and there is also a heater inside the fridge for the heating. I plan on wiring both the fridge and heater to a Sonoff Dual with a DS18B20 temperature sensor attached. The sensor will sit in a thermowell in the center of the fermenter to record the temperature of the beer.
I have largely copied code from other examples on this forum and modified it for my own use, but haven’t quite been able to get it to work. If the target temperature is above the current temperature the heating function works ok, however if the target is below the current temperature the cooling function doesn’t come on. I think it may be down to the use of brackets, but I’m not sure. Any help would be most appreciated.
I have the following items:
Switch Sonoffdual02P1 "Fermenter Heat" [ "Switchable" ] {mqtt=">[broker:garage/cmnd/sonoffdual-02/POWER1:command:*:default], <[broker:garage/stat/sonoffdual-02/POWER1:state:default]", autoupdate="false" }
Switch Sonoffdual02P2 "Fermenter Cool" [ "Switchable" ] {mqtt=">[broker:garage/cmnd/sonoffdual-02/POWER2:command:*:default], <[broker:garage/stat/sonoffdual-02/POWER2:state:default]", autoupdate="false" }
Number Temperature_Setpoint_Mode
Number Temperature_Setpoint "Temperature [%.1f °C]" <temperature> { mqtt=">[broker:garage/cmnd/sonoffdual-02/set_point:command:*:default]" }
I have the following on my sitemap;
Text item=Sonoffdual02_temp label="Fermenter Temperature"
Switch item=Temperature_Setpoint_Mode label="Fermenter Control" mappings=[1="OFF", 2="ON"]
Slider item=Temperature_Setpoint label="Fermenter Setpoint [%.1f °C]" icon="temperature"
and the following rule file;
//This is the rules file for the brewery fermenter
//Ferementer Heat and Cool
rule "Fermenter Control"
when
Item Sonoffdual02_temp changed or
// triggers whenever temp changes
Item Temperature_Setpoint_Mode changed
// also triggers when mode changes for immediate action
// else nothing would happen until temp changed, if it ever did
then
// first we check if the temp reading is valid
if ( Sonoffdual02_temp.state != NULL &&
Sonoffdual02_temp.state != UNDEF &&
Sonoffdual02_temp.state > 0) {
// that != means "not equal" in an if(condition)
// while the && means "and"
// temp sensor is valid, so
// let's set up a target temperature
var Number temp = 0
if (Temperature_Setpoint_Mode.state == 1) {
// off
temp = 0
Sonoffdual02P1.sendCommand(OFF)
Sonoffdual02P2.sendCommand(OFF)
} else if (Temperature_Setpoint_Mode.state == 2) {
// on
temp = Temperature_Setpoint.state as DecimalType
}
//HEAT
// now let's check if the temperature is too low
if (Sonoffdual02_temp.state < (temp - 0.5)) {
// we allow 0.5 degree hysteresis
// It's colder than target
// so turn heater on if it isn't already
if (Sonoffdual02P1.state != ON) {
Sonoffdual02P1.sendCommand(ON)
}
// else lets check if we're at the target
} else if (Sonoffdual02P1.state > temp) {
// reached target temp
// (our target could be 0 if mode is off)
// so turn heater off if it isn't already
if (Sonoffdual02P1.state != OFF) {
Sonoffdual02P1.sendCommand(OFF)
}
// COOL
// now let's check if the temperature is too high
if (Sonoffdual02_temp.state > (temp + 0.5)) {
// we allow 0.5 degree hysteresis
// It's hotter than target
// so turn fridge on if it isn't already
if (Sonoffdual02P2.state != ON) {
Sonoffdual02P2.sendCommand(ON)
}
// else lets check if we're at the target
} else if (Sonoffdual02P2.state < temp) {
// reached target temp
// (our target could be 0 if mode is off)
// so turn fridge off if it isn't already
if (Sonoffdual02P2.state != OFF) {
Sonoffdual02P2.sendCommand(OFF)
}
} // else we do nothing here
// temp could be within 0.5 degree of target
// and the heater / fridge will continue doing what it is doing
// That's the hysteresis
} else { // this else belongs to the valid temp reading check
Sonoffdual02P1.sendCommand(OFF)
Sonoffdual02P2.sendCommand(OFF)
// temp sensor broken, turn off heater for safety
}}
end
Thanks