Looking for a simple temp control for a water heater. (Sonoff/Mqtt)

Hi guys,
after a full day of tinkering, I’v got OH2 up and running on a RPI.
Now I need a simple control for a water heater like temp <50C Power on / >70C Power Off.
A Temp setting in the Webui would be great also.
What I have so far:
A “Sonoff TH16” connected via MQTT, which displays the proper Temp. in OH2
A “switch” in OH2 which also works, so I can turn on/off the Sonoff TH16
home.items:

Number SonoffTemp "Aktuelle Temperatur [%.1f °C]" <temperature> {mqtt="<[broker:tele/sonoff-TH16/SENSOR:state:JSONPATH($.DS18B20.Temperature)]"}
Switch Schalter "Ein/Aus" <light> (LR,gLight){ mqtt=">[broker:cmnd/sonoff-TH16/POWER:command:*:default],<[broker:stat/sonoff-TH16/POWER:state:default]" }

home.sitemap

sitemap home label=“TEST”
{
Frame label=“TH16”
{
Text item=SonoffTemp
Switch item=Schalter icon=“light”
}
}

So I think, I need some kind of rule, which triggers/reads the temp and switches the Sonoff on/off?
I tried some code snippets from here and there, but none of them worked as expected.
Any help (understandable for a non-coder) please…

untested

rule "xxx"
when
   Item SonoffTemp changed
then
   if ( SonoffTemp.state < 50 && Schalter.state != ON) {Schalter.sendCommand(ON)}
   else if ( SonoffTemp.state > 70 && Schalter.state != OFF) {Schalter.sendCommand(OFF)}
end

Show us what you have tried
Write a rule from the snippets, it probably won’t work but we’ll help you make it work as show you why it was not working
We won’t write a rule for you but we’ll help make your work. It’s the best way to learn
If we just give you a rule that you then copy and paste, the next time you need another rule to do something else you’ll be just as stuck.
So have a go, you can’t really break anything

Seems, I was just to impatient, because my Sonoff sends MQTT states only every 5 minutes, so I just had to wait…
I guess @Harry is also correct, since his rule is almost identical.
Thanks so far: :+1:
Now to the next step…any ideas how to realize the Temperature settings via Webgui?
BTW: I’ve used this rule:

rule “Water too cold”
when
Item SonoffTemp changed
then
if (SonoffTemp.state < 30) {
Schalter.sendCommand(ON)
}
if (SonoffTemp.state > 33) {
Schalter.sendCommand(OFF)
}
end

What do you mean?

something like this.


But after a quick look into the code, I think, I will probably skip that idea…

It’s very easy
You need two items
KomfortTemp and SparTemp

Then use (KomfortTemp.state as Number) in your rule to use their values

https://www.openhab.org/docs/configuration/sitemaps.html#element-type-setpoint
add to your items and sitemap

Setpoint item=WunschTemp minValue=18 maxValue=24 step=0.5

and rule

rule "xxx"
when
   Item SonoffTemp changed or
   Item WunschTemp changed
then
   if ( SonoffTemp.state < (WunschTemp.state -1) && Schalter.state != ON) {Schalter.sendCommand(ON)}
   else if ( SonoffTemp.state > (WunschTemp.state +1) && Schalter.state != OFF) {Schalter.sendCommand(OFF)}
end

Ok, it’s in the Webui and I can change the values up and down.
I used the rules above, but the Sonoff does not switch ON/OFF
home.rules

rule "xxx"
when
   Item SonoffTemp changed or
   Item WunschTemp changed
then
   if ( SonoffTemp.state < (WunschTemp.state -1) && Schalter.state != ON) {Schalter.sendCommand(ON)}
   else if ( SonoffTemp.state > (WunschTemp.state +1) && Schalter.state != OFF) {Schalter.sendCommand(OFF)}
end

.sitemap:

Setpoint item=WunschTemp label="Komfort Temperatur" icon="heating" minValue=25 maxValue=40 step=0.5

.items:

Number WunschTemp "WunschTemp [%.1f]"

rule "xxx"
when
    Item SonoffTemp changed or
    Item WunschTemp changed
then
    if ( SonoffTemp.state < ((WunschTemp.state as Number) - 0.5) && Schalter.state != ON) {Schalter.sendCommand(ON)}
    else if ( SonoffTemp.state > ((WunschTemp.state as Number) + 0.5) && Schalter.state != OFF) {Schalter.sendCommand(OFF)}
end

That works great!
Thank you very much.
Tomorrow I will try to implement a “min/max temperature” solution.
I think, with this working examples now, it shouldn’t be a major issue…otherwise :grinning:

As I said in post #7
Remember to use as Number to get the state of an item number to a Number Type

(WunschTemp.state as Number)

Hi
I’ve just set something like this up for a client.

I used a slider in their UI for the target temperature and another slider to set the Hystisis (they opted for a +/- of 4°c in the real world and a target of 50°c, the image below is from my development setup)