How can I have a thermostat in openhab?

I just installed openhab and I managed to configure 4 xiaomi temp sensors which are connected to a raspbery pi. I was not able to found in docs I read how can I create a thermostat
Basically I have 1 temp sensor in a room and I want to be able to set up a target temperature. Based on the value of the temp I would like to send a mqtt message to an esp32 which open/close a relay which control heat circuit.
Any advice?
Thank you

i have been working on something similar

//multi sensor 1
Group ms1_gdtstat “Dummy TStat 1” {ga=“Thermostat” [ useFahrenheit=true, modes=“off,heat,eco”]}
String ms1_mdtstat “Dummy TStat Mode 1” (ms1_gdtstat,mdt_gmode,tstat) {ga=“thermostatMode”}
Number ms1_dsetpoint “Dummy TStat Setpoint 1” (ms1_gdtstat,setpoint,tstat) {ga=“thermostatTemperatureSetpoint”}
Number:Temperature ms1_temp “Temperature” (temp,ms1_gdtstat,tstat) {channel=“zwave:device:57f93123:node25:sensor_temperature”, homekit=“TemperatureSensor”, ga=“thermostatTemperatureAmbient”}
Number ms1_humid “Humidity [%s]” (humid,ms1_gdtstat,tstat) {channel=“zwave:device:57f93123:node25:sensor_relhumidity”, homekit=“HumiditySensor”, ga=“thermostatHumidityAmbient”}


Selection item=ms1_mdtstat mappings=[off=Off,heat=Heat,eco=eco]
Setpoint item=ms1_dsetpoint
Selection item=ms2_mdtstat mappings=[off=Off,heat=Heat,eco=eco]

the rule would look something like this. i havent tried it yet so Im not sure if itll work
rule “setpoint=actual”
ms1_temp received update
if( ms1_temp.state!=ms1_dsetpoint.state ) {
//turn on heat
} else {
//turn off heat

1 Like

I am not sure how and where to put your code…
I found this meanwhile but again i have no idea how to install it…


I’m using this and it works really well, if you already have an old thermostat in your room then you can upgrade it with a wifi switch like a sonoff basic and control your heating this way.

I don t have old thermostat and i do not want physicals one. I have already for all 3 distribuitors esp32 which close/open valves by mqtt receiving commands and will connect it to openhab (this is next step).
So basically I need to install this thermostat first in openhab.
Will read carefully your link

Ok, so you basically need a set point on your virtual thermostat, then make a rule to turn on the switch when the temperature falls below that setpoint.

Here’s my virtual thermostat to turn my fireplace and plug-in AC on and off based on comparison of indoor temperature to outdoor temperature. I mostly control it through Google Assistant. I’m sure it could be better, but it works just fine for me.

You’ll need to modify it to have multiple locations and remove the cooling mode.


//Google Assistant thermostat (Google requires a mode, even if you manually set it up in openHAB)
Group Thermostat_Google "Thermostat" { ga="Thermostat" [ modes="off,heat,cool,on" ] }
String Thermostat_Google_Mode "Thermostat" <heating> (Group_Persist, Thermostat_Google) { ga="thermostatMode" }
Number:Temperature Thermostat_Google_Setpoint "Setpoint [%.1f %unit%]" <heating> (Group_Persist, Thermostat_Google) { ga="thermostatTemperatureSetpoint" }
Number:Temperature Sensor_Temperature_Zooz "Indoor Temperature [%.1f %unit%]" <temperature_indoor> (Group_Persist, Thermostat_Google) { channel="zwave:device:398644a6:node8:sensor_temperature", ga="thermostatTemperatureAmbient" }
Number Sensor_Humidity_Zooz "Indoor Humidity [%.1f%%]" <humidity> (Group_Persist, Thermostat_Google) { ga="thermostatHumidityAmbient" }


rule "HVAC Control"
    Item Sensor_Temperature_Zooz changed or
    Item Thermostat_Google_Mode changed or
    Item Thermostat_Google_Setpoint changed
    var OutdoorTemp = openweather_Temperature.state as Number
    var IndoorTemp = Thermostat_Temperature.state as Number
    var TargetTemp = Thermostat_Google_Setpoint.state as Number

    switch (Thermostat_Google_Mode.state) {
        case "on" :
            if (OutdoorTemp < IndoorTemp) { Thermostat_Google_Mode.sendCommand("heat") }
            if (OutdoorTemp > IndoorTemp) { Thermostat_Google_Mode.sendCommand("cool") }
        case "heat" :
            // Turn the fireplace on if current temperature is below target temperature
            if (IndoorTemp < TargetTemp && Maker_Fireplace.state == OFF) { Maker_Fireplace.sendCommand(ON) }
            // Turn the fireplace off if current temperature is above target temperature
            if (IndoorTemp >= TargetTemp && Maker_Fireplace.state == ON)  { Maker_Fireplace.sendCommand(OFF) }
        case "cool" :
            // Turn the air conditioner on if current temperature is above target temperature
            if (IndoorTemp > TargetTemp && Outlet_Air_Conditioner.state == OFF) { Outlet_Air_Conditioner.sendCommand(ON) }
            // Turn the air conditioner off if current temperature is below target temperature
            if (IndoorTemp <= TargetTemp && Outlet_Air_Conditioner.state == ON)  { Outlet_Air_Conditioner.sendCommand(OFF) }
        case "off" :
            // Turn the thermostat, fireplace, and air conditioner off
            // if (Thermostat_Google_Mode.state == "on") { Thermostat_Google_Mode.sendCommand("off") }
            if (Maker_Fireplace.state == ON) { Maker_Fireplace.sendCommand(OFF) }
            if (Outlet_Air_Conditioner.state == ON) { Outlet_Air_Conditioner.sendCommand(OFF) }
1 Like