[SOLVED] Sonoff TH16 auto thermostat - tasmota rules and passing variables from openhab

Hello all,
I have recently purchased a Sonoff TH16 unit, intended to use manage a single baseboard heater in a section of my shop. My shop usually has OK wifi connection, but occasionally this isn’t fully reliable.

What I am hoping to be able to do is have the sonoff manage the temperature via its’ internal rules, but have openhab modify the target temperature. I only want it to target 20 C ish when I’m expecting to work in the shop, and otherwise keep it to 10 C. If I depended on openhab to manage the on/off state, I may have the heater left on when wifi is not working in the shop despite the temperature.

I suppose an interim option is to have the sonoff TH internal rules turn off the heater if the temperature is over 21C and turn it on if the temp is below 10C, and rely on a good connection to openhab to turn it on.

My question: Is there a way to pass a target heat variable to Tasmota, and then rely on the internal tasmota rules to use this variable to target the given temperature?
Any other suggestions?

I don’t know much control you have, but you might use a timed fallback process. If Tasmota receives no new temperature target for N hours, revert to 10C.

Yes you can, but I think you’ll have to compile your own binaries see: https://tasmota.github.io/docs/#/Subscribe-&-Unsubscribe
Ask the tasmota forum

Yes, that’s easy
Again, tasmota forum or https://tasmota.github.io/docs/#/Rule-Cookbook

Thanks Vincent - I hadn’t found that website yet.
shows that via MQTT, it can be set up to expose / run rules for:


  • Mem1 0 <- thermostat status: 0-off 1-enabled - View or set by MQTT cmnd/mqttTopic/mem1
  • Mem2 25 <- setpoint Temp upper limit - View or set by MQTT cmnd/mqttTopic/mem2
  • Mem3 23 <- setpoint Temp lower limit - View or set by MQTT cmnd/mqttTopic/mem3
  • Var1 0 <- thermostat actual status: 1-OK 0-NOT READY - View by MQTT cmnd/mqttTopic/var1

Now to set this up, tinker with, then test it.

And this is working exactly as I wish. It took me a little while to realize that mem1 is a switch to enable or disable the thermostat rule, so I had to create an entry for it in openhab (or manually enable it via the tasmota console)

The solution, if someone is going to blindly replicate me (turning the heater on if the temp is below 15, turning it off if above 19 - to be modified later via openhab), AND you happen to have the same temperature sensor (SI7021). If you have a different temp sensor, adjust appropriately.

from tasmota console:

backlog SwitchMode1 3; Rule 1; Rule 4; TelePeriod 60; SetOption26 1; SetOption0 0; poweronstate 0; mem1 0; mem2 19; mem3 15; var1 0

then set up the rule

Rule1 on system#boot do RuleTimer1 70 endon on Switch1#State do event toggling1=%mem1% endon on event#toggling1=0 do mem1 1 endon on event#toggling1=1 do mem1 0 endon on Rules#Timer=1 do backlog var1 0; RuleTimer1 70; power1 0 endon on tele-SI7021#temperature do backlog var1 1; RuleTimer1 70; event ctrl_ready=1; event temp_demand=%value% endon on event#ctrl_ready>%mem1% do var1 0 endon on event#temp_demand>%mem2% do power1 0 endon on event#temp_demand<%mem3% do power1 %var1% endon

If you want to understand the rules more, look at the rules cookbook thermostat example in detail, linked in my previous post. In short, the rule only takes action if the rule is engaged (mem1), and if the thermostat is actually reading correctly. If the temp is below mem3, it turns the heater on, and if the temp is above mem2, it turns the heater off.

Then, assuming you’re familliar with mqtt in openhab, configure the channels via your favourite method. I used the paperui interface. Adjust the channel to reflect your device (rather than my CNCRoom)

Upper temp limit:
state: cmnd/CNCSonoff/mem2
command: cmnd/CNCSonoff/mem2

Lower temp limit:
state: cmnd/CNCSonoff/mem3
command: cmnd/CNCSonoff/mem3

state: cmnd/CNCSonoff/mem1
command: cmnd/CNCSonoff/mem1

Manual Heater Power
state: stat/CNCSonoff/POWER
command: cmnd/CNCSonoff/POWER

Raw Sensor Output
 -- gives output that will need to be parsed, such as: {"Time":"2019-12-15T00:48:27","SI7021":{"Temperature":19.9,"Humidity":43.6},"TempUnit":"C"}

You then will have mqtt items exposed that you use in rules as you see fit. I will have a switch for if I’m using the shop. If this is changed, it will adjust the temperature limits accordingly. I’m suitably content this won’t result in my shop over-heating and burning down (and the heater used really is quite safe even if it was left on). The biggest risk will be if wifi goes out while I’m using the shop, and the tasmota doesn’t get the command to reduce the temperatures when I’m done. Given that I have persistence enabled, I suppose I could write some rules to notify me about loss of communication.

Anyway, thanks for your direction @vzorglub and @rossko57

@bdm be careful doing this, these devices may claim to be able to handle these loads but they are cheaply made. If you are in north America then your heater is probably running on 220V and a 20A breaker or more, none of the Sonoff devices can handle that kind of power!

@bdm, can you change the title of the thread to something like sonoff TH16 auto thermostat
Ans change you tick the solution post? Thanks

1 Like

@CrazyIvan359 I had the same considerations, so I measured it. This is for a single baseboard heater that has up until now been running on a 110v plug-in socket, rated at 15amps pulling about 12 amps at the peak if dialed all the way up. I’ve dialed it down to less than 10 amps with the dial on the heater, and purchased a 16a rated device (given it’s already pulled apart to flash the device, it’s easy to confirm the relay is an HKE V6-S-DC5v relay, rated16a 250vac. datasheet: Here

Given the programming in tasmota, the heat will never get remotely close to the 105c level at which the current rating reduces to 10a, so I feel comfortable enough as it is. There are multiple safety’s here, both within the tasmota, the sonoff, and the heater itself. The heater also doesn’t have any fire hazards nearby, and the sonoff will be mounted in a safe location. My risk is then mainly financial, ‘heating the neigbourhood’. This is my first forray into sonoff, though, and if I run into any unexpected challenges, you can bet that I will post about it here.

[what makes me a bit more nervous is the induction ceiling-mounted blower/heater that I could possibly use an over-rated HVAC contactor but am still nervous about arcing within the device. I may end up using a servo to tilt a traditional mercury-containing tube that could then handle all the arcing and current that was thrown at it - but that may be over-kill. If I do that, I’ll probably intentionally go over the top and display it prominently with some background lighting.]

That doesn’t mean the PCB tracks are rated 16A


@bdm I second this, you might want to add solder to the traces to make sure they don’t overheat. As I’m sure you know this heater is a resistive load, so no fear of arcing from it, just current capacity.

As for your inductive heater, I would suggest an SSR (solid state relay). No chance of arcing with those. Though if the blower is operated by a thermostat on the device then you might be worried for nothing as the heater in it will be resistive.

1 Like