Problem with my rule

Hello,

something is wrong with my rule, sometimes the rule work well an after a restart, the rule don’t work.

rule “auto Heizung”
when
Time cron “0 0/30 * * * ?” or
Item home changed

then

	if (home.state == ON && now.getHourOfDay >= 5 && now.getHourOfDay < 6) { 
		
		sendCommand (alex_soll_temp, 16)
		sendCommand (kacontrolan_aus, ON)
		Thread::sleep(2000)
		sendCommand (kacontrolbetriebsart, 5)
		Thread::sleep(2000)
		sendCommand (kacontroltemp, 21)
		Thread::sleep(2000)
		sendCommand (kacontrolvendilatorstufe, 6)
		sendCommand (kueche_soll_temp, 21)
		sendCommand (wc_soll_temp, 16)
		sendCommand (schlafzimmer_soll_temp, 18)
		sendCommand (marlene_soll_temp, 18)
		sendCommand (bad_soll_temp, 21)
		sendCommand (moritz_soll_temp, 18)
		sendCommand (anna_soll_temp, 18)
		sendPushoverMessage(pushoverBuilder("Heizung 5 - 6Uhr" ))
		}
	else if (home.state == ON && now.getHourOfDay >= 6 && now.getHourOfDay < 8) { 
		sendCommand (alex_soll_temp, 16)
		sendCommand (kacontrolan_aus, ON)
		Thread::sleep(2000)
		sendCommand (kacontrolbetriebsart, 5)
		Thread::sleep(2000)
		sendCommand (kacontroltemp, 21)
		Thread::sleep(2000)
		sendCommand (kacontrolvendilatorstufe, 6)
		sendCommand (kueche_soll_temp, 21)
		sendCommand (wc_soll_temp, 18)
		sendCommand (schlafzimmer_soll_temp, 18)
		sendCommand (marlene_soll_temp, 18)
		sendCommand (bad_soll_temp, 21)
		sendCommand (moritz_soll_temp, 18)
		sendCommand (anna_soll_temp, 20)
		sendPushoverMessage(pushoverBuilder("Heizung 6 -8 Uhr" ))
		}
	else if (home.state == ON && now.getHourOfDay >= 8 && now.getHourOfDay < 17) { 
		sendCommand (alex_soll_temp, 18)
		sendCommand (kacontrolan_aus, ON)
		Thread::sleep(2000)
		sendCommand (kacontrolbetriebsart, 5)
		Thread::sleep(2000)
		sendCommand (kacontroltemp, 20)
		Thread::sleep(2000)
		sendCommand (kacontrolvendilatorstufe, 6)
		sendCommand (kueche_soll_temp, 20) 
		sendCommand (wc_soll_temp, 20)
		sendCommand (schlafzimmer_soll_temp, 18)
		sendCommand (marlene_soll_temp, 20)
		sendCommand (bad_soll_temp, 20)
		sendCommand (moritz_soll_temp, 20)
		sendCommand (anna_soll_temp, 20)
		sendPushoverMessage(pushoverBuilder("Heizung 8-17 Uhr" ))
		}	
	else if (home.state == ON && now.getHourOfDay >= 17 && now.getHourOfDay < 19) { 
		sendCommand (alex_soll_temp, 16)
		sendCommand (kacontrolan_aus, ON)
		Thread::sleep(2000)
		sendCommand (kacontrolbetriebsart, 5)
		Thread::sleep(2000)
		sendCommand (kacontroltemp, 20)
		Thread::sleep(2000)
		sendCommand (kacontrolvendilatorstufe, 6)
		sendCommand (kueche_soll_temp, 20)
		sendCommand (wc_soll_temp, 20)
		sendCommand (schlafzimmer_soll_temp, 20)
		sendCommand (marlene_soll_temp, 19)
		sendCommand (bad_soll_temp, 19)
		sendCommand (moritz_soll_temp, 18)
		sendCommand (anna_soll_temp, 18)
		sendPushoverMessage(pushoverBuilder("Heizung 17 19 Uhr" ))
		}
	else if (home.state == ON && now.getHourOfDay >= 19 && now.getHourOfDay < 20) { 
		sendCommand (alex_soll_temp, 16)
		sendCommand (kacontrolan_aus, ON)
		Thread::sleep(2000)
		sendCommand (kacontrolbetriebsart, 5)
		Thread::sleep(2000)
		sendCommand (kacontroltemp, 19)
		Thread::sleep(2000)
		sendCommand (kacontrolvendilatorstufe, 6)
		sendCommand (kueche_soll_temp, 19)
		sendCommand (wc_soll_temp, 18)
		sendCommand (schlafzimmer_soll_temp, 20)
		sendCommand (marlene_soll_temp, 18)
		sendCommand (bad_soll_temp, 20)
		sendCommand (moritz_soll_temp, 18)
		sendCommand (anna_soll_temp, 18)
		sendPushoverMessage(pushoverBuilder("Heizung 19-20Uhr" ))
		}
	else if (home.state == ON && now.getHourOfDay >= 20 && now.getHourOfDay < 21) { 
		sendCommand (alex_soll_temp, 16)
		sendCommand (kacontrolan_aus, ON)
		Thread::sleep(2000)
		sendCommand (kacontrolbetriebsart, 5)
		Thread::sleep(2000)
		sendCommand (kacontroltemp, 19)
		Thread::sleep(2000)
		sendCommand (kacontrolvendilatorstufe, 0)
		sendCommand (kueche_soll_temp, 19)
		sendCommand (wc_soll_temp, 18)
		sendCommand (schlafzimmer_soll_temp, 20)
		sendCommand (marlene_soll_temp, 18)
		sendCommand (bad_soll_temp, 17)
		sendCommand (moritz_soll_temp, 18)
		sendCommand (anna_soll_temp, 18)
		sendPushoverMessage(pushoverBuilder("Heizung 20 - 21Uhr" ))
		}
	else if (home.state == ON && now.getHourOfDay >= 21 && now.getHourOfDay < 5) { 
		sendCommand (alex_soll_temp, 16)
		sendCommand (kacontrolan_aus, ON)
		Thread::sleep(2000)
		sendCommand (kacontrolbetriebsart, 5)
		Thread::sleep(2000)
		sendCommand (kacontroltemp, 19)
		Thread::sleep(2000)
		sendCommand (kacontrolvendilatorstufe, 0)
		sendCommand (kueche_soll_temp, 19)
		sendCommand (wc_soll_temp, 17)
		sendCommand (schlafzimmer_soll_temp, 18)
		sendCommand (marlene_soll_temp, 18)
		sendCommand (bad_soll_temp, 17)
		sendCommand (moritz_soll_temp, 20)
		sendCommand (moritz_soll_temp, 18)
		sendCommand (anna_soll_temp, 18)
		sendPushoverMessage(pushoverBuilder("Heizung 21-5 Uhr" ))
		}
	else if (home.state == OFF ) { 
		sendCommand (alex_soll_temp, 16)
		sendCommand (kacontrolan_aus, OFF)
		sendCommand (wc_soll_temp, 16)
		sendCommand (schlafzimmer_soll_temp, 18)
		sendCommand (marlene_soll_temp, 18)
		sendCommand (bad_soll_temp, 16)
		sendCommand (moritz_soll_temp, 18)
		sendCommand (anna_soll_temp, 18)
		sendPushoverMessage(pushoverBuilder("Heizung OFF" ))
		}

end

The Pushover Message ist just a test

Can everybody help me with my problem

Thanks Alex

?? Bit more detail ??

A guess, if home.state is NULL what might happen?

First tip
Change ALL your sendCommand (alex_soll_temp, 16) syntax to:

alex_soll_temp.sendCommand(16)

It’s more efficient and does give the rule parser and interpreter as much work.

Second tip:
Split your rule:

rule “auto Heizung at home”
when
    Time cron “0 0/30 * * * ?” or
    Item home changed to ON
then
    if (now.getHourOfDay >= 5 && now.getHourOfDay < 6) { 
...
rule “auto Heizung away”
when
    Time cron “0 0/30 * * * ?” or
    Item home changed to OFF
then
    sendCommand (alex_soll_temp, 16)
    sendCommand (kacontrolan_aus, OFF)
    sendCommand (wc_soll_temp, 16)
    ....

Third tip
Use spaces instead of tabs for indenting. It’s more consistent accross platforms

Fourth tip
In the at home rule use a “Time of day” item

Item

String TimeOfDay
rule "time of day"
when
    Time cron “0 0/30 * * * ?”
then
    if (now.getHourOfDay >= 5 && now.getHourOfDay < 6) TimeOfDay.postUpdate("EARLYMORNING")
    else if (now.getHourOfDay >= 6 && now.getHourOfDay < 8) TimeOfDay.postUpdate("BREAKFASTTIME")
    ....
end

rule “auto Heizung at home”
    Item home changed to ON or
    Item TimeOfDay changed
then
    if TimeOfDay.state == "EARLYMORNING" {
        ....
    } else if TimeOfDay.state == "BREAKFASTTIME" {
        ....
end
...

You can then use this TimeOfDay item in other rules across openHAB

And last tip and probably the most important one. Avoid these long Thread::sleep statements, use timers instead

Thank you for your answer

The home.state Item is a Switch, it is on or off if anybody is at home or not.

The Rule work fine an after a restart it is doesn´t work, after a other restart works fine, it is stange!!

By Alex

I rebuild my rules like the tips from vincent,

rule “time of day”

when

Time cron “0 0/30 * * * ?”

then

if (now.getHourOfDay < 5) TimeOfDay.postUpdate("NIGHT")

else if (now.getHourOfDay >= 5 && now.getHourOfDay < 6) TimeOfDay.postUpdate("EARLYMORNING")

else if (now.getHourOfDay >= 6 && now.getHourOfDay < 8) TimeOfDay.postUpdate("BREAKFASTTIME")

else if (now.getHourOfDay >= 8 && now.getHourOfDay < 17) TimeOfDay.postUpdate("DAYTIME")

else if (now.getHourOfDay >= 17 && now.getHourOfDay < 19) TimeOfDay.postUpdate("DINNERTIME")

else if (now.getHourOfDay >= 19 && now.getHourOfDay < 20) TimeOfDay.postUpdate("EVENINGTIME")

else if (now.getHourOfDay >= 20 && now.getHourOfDay < 21) TimeOfDay.postUpdate("TVTIME")

else if (now.getHourOfDay >= 21 ) TimeOfDay.postUpdate("NIGHT")

end

rule “auto Heizung at home”

Item home changed to ON or

Item TimeOfDay changed

then

if TimeOfDay.state == "EARLYMORNING" {

    alex_soll_temp.sendCommand (16)

        sendCommand (kacontrolan_aus, ON)

        createTimer(now.plusseconds(2)) 

                        [|

                        kacontrolbetriebsart.sendCommand (5)

                        createTimer(now.plusseconds(2)) 

                                [|

                                kacontroltemp.sendCommand (21)

                                createTimer(now.plusseconds(2)) 

                                      [|

                                      kacontrolvendilatorstufe.sendCommand (6)

                                      kueche_soll_temp.sendCommand (21)

                                      wc_soll_temp.sendCommand (16)

                                      schlafzimmer_soll_temp.sendCommand (18)

                                      marlene_soll_temp.sendCommand (18)

                                      bad_soll_temp.sendCommand  (21)

                                      moritz_soll_temp.sendCommand (18)

                                      anna_soll_temp.sendCommand (18)

                                      sendPushoverMessage(pushoverBuilder("Heizung 5 - 6Uhr" ))

        

        ]

        ]

        ]

        }

        

else if TimeOfDay.state == "BREAKFASTTIME" { .........

end

rule “auto Heizung away”

when

Time cron “0 0/30 * * * ?” or

Item home changed to OFF

then

alex_soll_temp.sendCommand (16)

kacontrolan_aus.sendCommand (OFF)

wc_soll_temp.sendCommand (16)

schlafzimmer_soll_temp.sendCommand (18)

marlene_soll_temp.sendCommand (18)

bad_soll_temp.sendCommand (16)

moritz_soll_temp.sendCommand (18)

anna_soll_temp.sendCommand (18)

sendPushoverMessage(pushoverBuilder("Heizung OFF" ))

end

Is this correct?
I read the Thread::sleep is ok if the timeslot is short 2 seconds is ok.

By Alex

2 seconds is not short. It’s a lifetime for a computer.
And you have several in a row in one if statement
Much better now

Thank you, i will try it on evening

By Alex

I wonder what its state is after a restart.

The State is after a restart On, this is the true value.
The Rule sometimes doesn´t works after a restart or 1 hour later or 1 day later.
After a rule again the rule works fine, the restore after resart is not the problem i think.

I had rebuild and split the rules and will test it on evening

We only know what you tell us.

What “not work” means is still unknown to us, if your new rule is still problematic - tell us a bit more about what does/doesn’t happen, and why you expected it to happen.

OK, sorry
The Rule do nothing, but in the Karaf Console i see it is loaded
It´s like the rule dosn´t exist

Use logInfo() to find out what your rule is doing. For example, one as a first instruction will tell you if your rule has triggered at all.

Oh, yes this is probartly a little more elagant as my pushover.
I will try for debug