[SOLVED] Rule for heating a water tank with resistors of a solar panel

I have written the following rule for a Sonoff Pow R2 to turn on the resistors of a water tank on a solar panel at 6 o’clock in the morning from Monday to Friday if the probe temperature of a sonoff th 16 is below 50ºC and that they turn off when the temperature is higher than 51ºC. The first part of the rule is carried out but the next one, the one to turn off the resistances does not do it. I do not know what I’m doing wrong. I imagine that the syntax will have some error that I do not see because I think it is more or less well written. If someone could help me, I would appreciate it. Thank you:)

  • Platform information:
Hardware: Raspberry PI 3B
OS: openHABian v1.4.1
Java Runtime Environment: openjdk version “1.8.0_152”
OpenJDK Runtime Environment (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 1.8 .0_152-b76)
OpenJDK Client VM (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 25.152-b76, m ixed mode, Evaluation)
openHAB version: openHAB 2

Issue of the topic:
Rule for some resistors turn on and heat the water tank of a solar panel at a certain temperature and time and turn off when they reach the indicated temperature.

  • Items configuration related to the issue
// SonoFF TH
Number TemperaturaAguaPlacaSolar_Temperatura_Temperature "Temperatura[%.1f °C]" <temp> (Home, SF_SegundaPlanta, SF_Terrado,SF_PlacaSolar, SF_SensoresTemperaturaTerrado)  { channel="mqtt:topic:681400fe:templacsolarsonoff:temperature" }
// Sonoff POW R2
Switch PotenciaPlacaSolar_Est_Power "Está[MAP(sonoffmotor.map):%s]" (Home, SF_SegundaPlanta, SF_Terrado, SF_PlacaSolar, SF_RelesTerrado) { channel="mqtt:topic:681400fe:powplacasolar:power" }
  • Rules code related to the issue
rule "Calentar tanque placa solar - Mañana"
when
   Time cron "0 0 6 ? * MON,TUE,WED,THU,FRI *"
then
   if ( TemperaturaAguaPlacaSolar_Temperatura_Temperature.state < 50 && PotenciaPlacaSolar_Est_Power.state != ON) {PotenciaPlacaSolar_Est_Power.sendCommand(ON)}
   else if ( TemperaturaAguaPlacaSolar_Temperatura_Temperature.state > 51 && PotenciaPlacaSolar_Est_Power.state != OFF) {PotenciaPlacaSolar_Est_Power.sendCommand(OFF)}
end
1 Like

This rule will trigger once a day, at 06:00:00 O’Clock, Monday to Friday. If at this time a specific Item state is below 50, some item is changed to ON, if at this time the item is above 51 it will change the other item to OFF. I guess, what you want is another rule:

rule "Calentar tanque placa solar - Mañana"
when
    Item TemperaturaAguaPlacaSolar_Temperatura_Temperature changed
then
    if(!(TemperaturaAguaPlacaSolar_Temperatura_Temperature.state instanceof Number)) {
        logWarn("solartemp","Temperature sensor state not of type Number!")
        return;
    }
    var newState = OFF
    val Number nTemp = TemperaturaAguaPlacaSolar_Temperatura_Temperature.state as Number
    if(now.getMinuteOfDay > 6*60 && now.getMinuteOfDay < 21*60 && now.getDayOfWeek < 6) { //between 6:00 and 21:00, Monday to Friday
        if(nTemp < 50)
            newState = ON
    }
    if(PotenciaPlacaSolar_Est_Power.state != newState)
        PotenciaPlacaSolar_Est_Power.sendCommand(newState)
end

But then, will it be connecting and disconnecting depending on the temperature value (50ºC) from 6:00 a.m. to 9:00 p.m.? I say it for the clarification “between 6:00 and 21:00, Monday to Friday”. I would like it to go from Monday to Friday at 6 o’clock in the morning, if the temperature is lower than 50ºC, then turn on resistances until it reaches 50-51ºC and stops and the next time it does is the next day.

Ah, I see. I misunderstood this part. Then you will need two rules:

rule "Calentar tanque placa solar - Mañana ON"
when
   Time cron "0 0 6 ? * MON,TUE,WED,THU,FRI *"
then
   if(TemperaturaAguaPlacaSolar_Temperatura_Temperature.state < 50 && PotenciaPlacaSolar_Est_Power.state != ON) 
       PotenciaPlacaSolar_Est_Power.sendCommand(ON)
end

rule "Calentar tanque placa solar - Mañana OFF"
when
    Item TemperaturaAguaPlacaSolar_Temperatura_Temperature changed
then
    if(TemperaturaAguaPlacaSolar_Temperatura_Temperature.state > 51 && PotenciaPlacaSolar_Est_Power.state != OFF) 
        PotenciaPlacaSolar_Est_Power.sendCommand(OFF)
end

Ok now I understand why it did not work, I wanted to put everything in one rule and I did not think of another additional rule. By the way, I take note of the previous one because it’s very good :slight_smile: Thank you very much for everything, really :slight_smile:

In case someone is interested in the rule, add “item” in the second rule Item TemperatureWaterPlacaSolar_Temperature_Temperature changed :slight_smile:

rule "Calentar tanque placa solar - Mañana ON"
when
   Time cron "0 0 6 ? * MON,TUE,WED,THU,FRI *"
then
   if(TemperaturaAguaPlacaSolar_Temperatura_Temperature.state < 50 && PotenciaPlacaSolar_Est_Power.state != ON) 
       PotenciaPlacaSolar_Est_Power.sendCommand(ON)
end

rule "Calentar tanque placa solar - Mañana OFF"
when
    Item TemperaturaAguaPlacaSolar_Temperatura_Temperature changed
then
    if(TemperaturaAguaPlacaSolar_Temperatura_Temperature.state > 51 && PotenciaPlacaSolar_Est_Power.state != OFF) 
        PotenciaPlacaSolar_Est_Power.sendCommand(OFF)
end

Jep, typical typo… :wink: Did the correction above.

Yes, I think that is the word that we all forget to write. It happens to me a few times, I assume that I wrote it and then even looking, I don’t realize it because my mind assumes that it is written, hehehehe :slight_smile:

Especially if copying Item names… :wink:

I write because I modified the rule a little to be able to activate it and deactivate it according to my needs, so I created an item switch in items:

// Activación-desactivación enchufe horario placa solar
Switch Horario_caldera "Está[MAP(horariocaldera.map):%s]" <switch> (Home, Escenas)

I’ve added it to the previous rule:

rule "Calentar tanque placa solar - Mañana encendido"
when
   Time cron "0 24 18 ? * MON,TUE,WED,THU,FRI,SAT,SUN *"
then
   if(Horario_placa_solar.state == ON) {
      (TemperaturaAguaPlacaSolar_Temperatura_Temperature.state < 62 && PotenciaPlacaSolar_Est_Power.state != ON) 
      PotenciaPlacaSolar_Est_Power.sendCommand(ON)
   }

end

rule "Calentar tanque placa solar - Mañana apagado"
when
    Item TemperaturaAguaPlacaSolar_Temperatura_Temperature changed
then
    if(Horario_placa_solar.state == ON) {
       (TemperaturaAguaPlacaSolar_Temperatura_Temperature.state > 65 && PotenciaPlacaSolar_Est_Power.state != OFF) 
        PotenciaPlacaSolar_Est_Power.sendCommand(OFF)
    }
end

And I have written in the sitemap the following:

Group item=Escenas {
Frame label="Horario placa solar"
            Default item=Horario_placa_solar
        }

The problem I have is that when I activate the item, the rule starts the first part (turns on the resistances) but just at the moment it turns them off again as you can see in the log:

2019-04-12 18:24:00.071 [ome.event.ItemCommandEvent] - Item 'PotenciaPlacaSolar_Est_Power' received command ON

2019-04-12 18:24:00.091 [nt.ItemStatePredictedEvent] - PotenciaPlacaSolar_Est_Power predicted to become ON

==> /var/log/openhab2/openhab.log <==

2019-04-12 18:24:00.147 [INFO ] [smarthome.model.script.notifications] - Sending notification via Telegram.

==> /var/log/openhab2/events.log <==

2019-04-12 18:24:00.162 [vent.ItemStateChangedEvent] - PotenciaPlacaSolar_Est_Power changed from OFF to ON

2019-04-12 18:24:02.283 [vent.ItemStateChangedEvent] - PotenciaPlacaSolar_Voltaje_Voltage changed from 0 to 220

2019-04-12 18:24:02.293 [vent.ItemStateChangedEvent] - PotenciaPlacaSolar_ConsumidoHoy_Today changed from 0.091 to 0.096

2019-04-12 18:24:02.296 [vent.ItemStateChangedEvent] - PotenciaPlacaSolar_CargaDeEnergA_POW_Load changed from 0 to 2144

2019-04-12 18:24:02.298 [vent.ItemStateChangedEvent] - PotenciaPlacaSolar_Actual_Current changed from 0.0 to 9.761
2019-04-12 18:24:10.906 [vent.ItemStateChangedEvent] - TemperaturaAguaPlacaSolar_PotenciaSeAlWifi changed from 62 to 68

2019-04-12 18:24:10.966 [vent.ItemStateChangedEvent] - TemperaturaAguaPlacaSolar_Temperatura_Temperature changed from 61.5 to 61.4

2019-04-12 18:24:11.006 [ome.event.ItemCommandEvent] - Item 'PotenciaPlacaSolar_Est_Power' received command OFF

2019-04-12 18:24:11.021 [nt.ItemStatePredictedEvent] - PotenciaPlacaSolar_Est_Power predicted to become OFF

2019-04-12 18:24:11.035 [vent.ItemStateChangedEvent] - PotenciaPlacaSolar_Est_Power changed from ON to OFF

==> /var/log/openhab2/openhab.log <==

2019-04-12 18:24:11.068 [INFO ] [smarthome.model.script.notifications] - Sending notification via Telegram.

==> /var/log/openhab2/events.log <==

2019-04-12 18:24:13.289 [vent.ItemStateChangedEvent] - PotenciaPlacaSolar_Voltaje_Voltage changed from 220 to 0

2019-04-12 18:24:13.295 [vent.ItemStateChangedEvent] - PotenciaPlacaSolar_ConsumidoHoy_Today changed from 0.096 to 0.101

2019-04-12 18:24:13.298 [vent.ItemStateChangedEvent] - PotenciaPlacaSolar_CargaDeEnergA_POW_Load changed from 2144 to 0

2019-04-12 18:24:13.315 [vent.ItemStateChangedEvent] - PotenciaPlacaSolar_Actual_Current changed from 9.761 to 0.0

2019-04-12 18:24:20.239 [vent.ItemStateChangedEvent] - PotenciaPlacaSolar_PotenciaSeAlWifi_RSSI changed from 78 to 74

I did the same process in another rule and it works well for me:

rule "Encender enchufe caldera"
when 
	//De lunes a viernes a las 6:45
	Time cron "0 45 6 ? * MON,TUE,WED,THU,FRI *" or
	//De lunes a viernes y domingo a las 19:00
	Time cron "0 0 19 ? * MON,TUE,WED,THU,FRI,SUN *" or
	//Sábado a las 8:00
	Time cron "0 0 8 ? * SAT *"
then
    if(Horario_caldera.state == ON) {
	sendCommand(EnchufeCalderaPatioDeLuces_Switch, "ON")
	}
end

rule "Apagar enchufe caldera"
when 
	//De lunes a viernes a las 7:45
	Time cron "0 45 7 ? * MON,TUE,WED,THU,FRI *" or
	//De lunes a viernes y domingo a las 20:45
	Time cron "0 45 20 ? * MON,TUE,WED,THU,FRI,SUN *" or
	//Sábado a las 9:00
	Time cron "0 0 9 ? * SAT *"
then
    if(Horario_caldera.state == ON) {
	sendCommand(EnchufeCalderaPatioDeLuces_Switch, "OFF")
	}
end

I’m sure I’ve screwed up something but I can not see it …

It’s solved with this version:)

rule "Calentar tanque placa solar - Mañana encendido"
when
   Time cron "0 49 19 ? * MON,TUE,WED,THU,FRI,SAT,SUN *"
then
   if(Horario_placa_solar.state == ON) {
      (TemperaturaAguaPlacaSolar_Temperatura_Temperature.state < 64 && PotenciaPlacaSolar_Est_Power.state != ON) 
      PotenciaPlacaSolar_Est_Power.sendCommand(ON)
   }

end

rule "Calentar tanque placa solar - Mañana apagado"
when
    Item TemperaturaAguaPlacaSolar_Temperatura_Temperature changed
then
    if(TemperaturaAguaPlacaSolar_Temperatura_Temperature.state > 64 && PotenciaPlacaSolar_Est_Power.state != OFF) 
        PotenciaPlacaSolar_Est_Power.sendCommand(OFF)
end

The switch only has to activate or deactivate the first part of the rule. As I said before, I could not see it and I was sure it was foolishness:)

Both rules are wrong :slight_smile:

This part makes no sense:

 if(Horario_placa_solar.state == ON) {
      (TemperaturaAguaPlacaSolar_Temperatura_Temperature.state < 64 && PotenciaPlacaSolar_Est_Power.state != ON) 
      PotenciaPlacaSolar_Est_Power.sendCommand(ON)
   }

The key word is missing…

 if(Horario_placa_solar.state == ON) {
      if(TemperaturaAguaPlacaSolar_Temperatura_Temperature.state < 64 && PotenciaPlacaSolar_Est_Power.state != ON) 
          PotenciaPlacaSolar_Est_Power.sendCommand(ON)
   }

Or simply add another boolean and:

 if(Horario_placa_solar.state == ON && TemperaturaAguaPlacaSolar_Temperatura_Temperature.state < 64 && PotenciaPlacaSolar_Est_Power.state != ON) 
     PotenciaPlacaSolar_Est_Power.sendCommand(ON)

This applies to both rules, of course.

Thank you. The truth is that I have learned a lot and thanks to your help even more. But I do not stop committing imprudence when writing, hehehehe :slight_smile:
Finally my rule will be:

rule "Calentar tanque placa solar - Mañana encendido"
when
   //De lunes a viernes a las 6:00
   Time cron "0 0 6 ? * MON,TUE,WED,THU,FRI *" or
   //De sábado a domingo a las 8:00
	Time cron "0 0 8 ? * SAT,SUN *"
then
   if(Horario_placa_solar.state == ON) {
      if(TemperaturaAguaPlacaSolar_Temperatura_Temperature.state < 55 && PotenciaPlacaSolar_Est_Power.state != ON) 
         PotenciaPlacaSolar_Est_Power.sendCommand(ON)
   }

end

rule "Calentar tanque placa solar - Mañana apagado"
when
    Item TemperaturaAguaPlacaSolar_Temperatura_Temperature changed
then
    if(Horario_placa_solar.state == ON) {
      if(TemperaturaAguaPlacaSolar_Temperatura_Temperature.state > 55 && PotenciaPlacaSolar_Est_Power.state != OFF) 
         PotenciaPlacaSolar_Est_Power.sendCommand(OFF)
   }
end

While your rule looks like it would work you don’t have much of a hysteresis in there. How big is the tank of water?
You should turn it off at 56 and on at 54. Or 55.5 / 55. Choose values which will work for you.

Depending on the inertia of the water it might not be a problem. If its small then your relay could be turned on and off quickly which could cause problems.

Hi Crispin,
It is a tank of 150 liters. It is located in the highest part of the house. From there, the hot water goes down through the pipe to the first floor and the ground floor. On the first floor there is also an electric boiler for support when the tank of the solar panel does not have hot water. This is the tank:


In fact, it has me crazy because I live in Almeria, Andalusia, one of the places in Europe where there are more hours of sun and I only have hot water from April to October. The rest of months I have to use the electric boiler. From what I have observed in the graphs that I have, I think the water from the electric boiler mixes with the water from the solar panel tank but it should not happen because I have some keys to cut the water flow of the electric boiler and use only the solar panel without having any communication between them.
When I look at the graphs, I see that the water in the solar panel tank rises to a normal temperature but it is not normal for it to rise at night. It goes up when the electric boiler has hot water. I imagine it will be a pipeline and communications problem that I will have to look at.
I will follow your advice and change the temperatures in the rule.
Thank you so much for everything.