Good morning everybody.
I do have two components connected to my Openhab based on a raspberry 4. The Battery and photovoltaics on the roof and the modbus based water tank.
I am able to read and write everything and everything works like a charm.
So I wrote a Rule which does the following:
When the battery in my basement is >= 95% and the PV generates 2000 W more energy than my house needs for 15 minutes - tested every 30 Seconds - I want to rise the temperature of my water from 48°C (boost is 43°C) to 55°C (boost is 70°)
The default temp and boost temp is different but not neccesary for this scenario, as we have two states:
Default: 48°C Water + 43°C Boost
Rised temperature: 55°C Water + 70°C Boost
This values are provided by the manufacturer.
When my house uses more energy, than generated with my PV for 3 Minutes (tested 3x every minute) i want to bring the Default temperatures back to 48° and 43°
The code i have written is the following and I wanted to ask you guys if there are any improvements you could provide.
If you like my “template” then please use it, but i’d love to make it even better ![]()
rule "PV-Ueberschuss ins Wasser"
when
Item UG_Install_Batterie_EnergyProduction changed
then
try
{
var Number Produktion = UG_Install_Batterie_EnergyProduction.state as Number
var Number Verbrauch = UG_Install_Batterie_HouseConsumption.state as Number
var Number Ueberschuss = Produktion - Verbrauch
logInfo("Wassertemperatur", "Überschussregel beginnt")
logInfo("Wassertemperatur", "Überschuss beträgt " + Ueberschuss + " W")
//teste 15 Minuten lang und wenn das Ergebnis immer noch gleich ist, erhöhe die Wassertemperatur.
if ((Ueberschuss >= 2000|"W") && (C_Installation_Wasser_Heizstab_Temp.state < 70|"°C"))
{
logInfo("Wassertemperatur", "Überschuss der Stromproduktion ist größer 2000W, nämlich: " + Ueberschuss + " W")
logInfo("Wassertemperatur", "Wassertemperatur ist unter 70°C eingestellt. " + C_Installation_Wasser_Heizstab_Temp.state)
//Wenn Batteriespeicher voll
if (UG_Install_Batterie_BatteryFuelCharge.state >= 90|"%")
{
for (var i = 0; i < 30; i++)
{
// Istwerte von Verbrauch in Produktion überprüfen
var Number Produktion2 = UG_Install_Batterie_EnergyProduction.state as Number
var Number Verbrauch2 = UG_Install_Batterie_HouseConsumption.state as Number
var Number Ueberschuss2 = Produktion2 - Verbrauch2
var checkResult = (Ueberschuss2 >= 2000)
// If the check fails, break out of the loop
if (!checkResult)
{
logInfo("Wassertemperatur", "Produktion ist keine 15 Minuten lang größer als 2500W")
logInfo("Wassertemperatur", "Schleife wird abgebrochen")
break;
}
// Warte 30 Sekunden, bis die Schleife erneut startet
logInfo("Wassertemperatur", "30 Sekunden Warten beginnt jetzt: ")
Thread::sleep(30000)
logInfo("Wassertemperatur", "Durchgang " + (i+1))
logInfo("Wassertemperatur", "Überschuss der Stromproduktion ist immernoch größer 2500W: " + Ueberschuss2 + " W")
if (i == 29)
{
logInfo("Wassertemperatur", "Wassertemperatur wird auf 70°C eingestellt!")
sendBroadcastNotification("Wassertemperatur wird angehoben.", "water", "high")
//E-Heiz auf 70°C
C_Installation_Wasser_Heizstab_Temp.sendCommand(70)
//Wasser-Soll-Temp auf 55°C
C_Installation_Wasser_Temp_Soll.sendCommand(55)
}
}
}
}
else if (((Ueberschuss < 0|"W")) && (C_Installation_Wasser_Heizstab_Temp.state != 43|"°C") && (C_Installation_Wasser_Temp_Soll != 48|"°C"))
{
for (var f = 0; f < 3; f++)
{
logInfo("Wassertemperatur", "Durchgang " + (f+1))
logInfo("Wassertemperatur", "Stromververbrauch ist zu gering.")
logInfo("Wassertemperatur", "Warte 1 Minute")
Thread::sleep(60000)
// Istwerte von Verbrauch in Produktion überprüfen
var Number Produktion3 = UG_Install_Batterie_EnergyProduction.state as Number
var Number Verbrauch3 = UG_Install_Batterie_HouseConsumption.state as Number
var Number Ueberschuss3 = Produktion3 - Verbrauch3
var checkResult2 = (Ueberschuss3 <= 0)
// If the check fails, break out of the loop
if (!checkResult2)
{
logInfo("Wassertemperatur", "Wasser bleibt heiß")
logInfo("Wassertemperatur", "Schleife wird abgebrochen")
break;
}
if (f == 2)
{
logInfo("Wassertemperatur", "Voraussetzungen stimmen noch nicht.")
sendBroadcastNotification("Wassertemperatur wird gesenkt.", "water", "high")
//E-Heiz auf 43°C
C_Installation_Wasser_Heizstab_Temp.sendCommand(43)
//Wasser-Soll-Temp auf 48°C
C_Installation_Wasser_Temp_Soll.sendCommand(48)
}
}
}
}
finally
{
logInfo("Wassertemperatur", "Es passiert quasi nix.")
}
end
I hope you enjoy this and I will be very happy to see if and how this code can be improved.
Thank you very much and take care
Marcel ![]()
