Assumed timing issues with rule

Quite a newbie here with openhab2 (and java) and as I try my first rules I see unexpected results.
I don’t have much actors currently, 2 hue items and a number of Brennenstuhl switchable plugs which I drive with raspberry-remote/send.
From a sitemap with dedicated switches this works fine and reliable.

Now, to establish a cycle of daily phases, I created 8 state Buttons and a rule file with event handlers for each state

Switch      StateBtn_early            "Frühlicht"              <"light">       (gStateBtn)
Switch      StateBtn_morning          "Morgenlicht"            <"light">       (gStateBtn)
Switch      StateBtn_day              "Taglicht"               <"light">       (gStateBtn)
Switch      StateBtn_away             "Weglicht"               <"light">       (gStateBtn)
Switch      StateBtn_evening          "Abendlicht"             <"light">       (gStateBtn)
Switch      StateBtn_late             "Spätlicht"              <"light">       (gStateBtn)
Switch      StateBtn_goodnight        "GuteNachtlicht"         <"light">       (gStateBtn)
Switch      StateBtn_night            "Nachtlicht"             <"light">       (gStateBtn)

There is another frame in that sitemap with all my stateBtn lined up. I can click a button there, the update (or command, or change) is detected, the handler gets called, some things are done. This is where the problems start.

Among the things to do there hue actions, change the HSB of the items, and those work reliable.

Then there are plugs to switch, and those actions happen, or they do not. I suspect the raspberry-remote/send construction is challenged when a sequence of commands come in short succession. I tried to insert a wait() but the RuleEngine erred “cannot invoke method”

And finally, there is a eventhandler for updates to a String item holding the current state which is supposed to clear the ui, set all switches to off exceept the current one. It works, but only in half of the times. The other times apparently nothing happens, or some switches are cleared while others stay in visual on position.

All in all it feels like I’m trying to ride the horse backwards so any advice how to approach this would be most appreciated.

var String currentPhase
var String ColorHue
var String[] statesArray = newArrayList("early","morning","day","away","evening","late","goodnight","night")


// StateBtn_early
rule "rule triggered by StateBtn_early"

when 
	Item StateBtn_early received update
then
	if (StateBtn_early.state == ON) {
			
			currentPhase="early"
			ColorHue="270,100,3"
			hue_0210_001788a718e6_1_color.sendCommand(ColorHue)
			hue_0210_001788a718e6_2_color.sendCommand(ColorHue)
			// redlight aus
			steckdose08Switch.sendCommand("OFF")

			Current_BtnState.postUpdate("early")
	}
end

// StateBtn_morning 
rule "rule triggered by StateBtn_morning"

when 
	Item StateBtn_morning received update
then
	if (StateBtn_morning.state == ON) {

			currentPhase="morning"
			ColorHue="45,100,23"
			hue_0210_001788a718e6_1_color.sendCommand(ColorHue)
			hue_0210_001788a718e6_2_color.sendCommand(ColorHue)

			Current_BtnState.postUpdate("morning")
	}

end


// StateBtn_day
rule "rule triggered by StateBtn_day"

when 
	Item StateBtn_day received update
then
	if (StateBtn_day.state == ON) {

			currentPhase="day"
			ColorHue="90,100,33"
			hue_0210_001788a718e6_1_color.sendCommand(ColorHue)
			hue_0210_001788a718e6_2_color.sendCommand(ColorHue)

			Current_BtnState.postUpdate("day")
	}

end

// StateBtn_away
rule "rule triggered by StateBtn_away"

when 
	Item StateBtn_away received update
then
	if (StateBtn_away.state == ON) {
			
			currentPhase="away"
			ColorHue="135,100,1"
			hue_0210_001788a718e6_1_color.sendCommand(ColorHue)
			hue_0210_001788a718e6_2_color.sendCommand(ColorHue)

			Current_BtnState.postUpdate("away")
	}

end

// StateBtn_evening
rule "rule triggered by StateBtn_evening"

when 
	Item StateBtn_evening received update
then
	if (StateBtn_evening.state == ON) {
			
			currentPhase="evening"
			ColorHue="18,100,43"
			hue_0210_001788a718e6_1_color.sendCommand(ColorHue)
			hue_0210_001788a718e6_2_color.sendCommand(ColorHue)
			steckdose16Switch.sendCommand("ON")

			Current_BtnState.postUpdate("evening")
	}

end

// StateBtn_late
rule "rule triggered by StateBtn_late"

when 
	Item StateBtn_late received update
then
	if (StateBtn_late.state == ON) {
			
			currentPhase="late"
			ColorHue="358,100,7"
			hue_0210_001788a718e6_1_color.sendCommand(ColorHue)
			hue_0210_001788a718e6_2_color.sendCommand(ColorHue)
			steckdose16Switch.sendCommand("ON")
			steckdose02Switch.sendCommand("ON")

			Current_BtnState.postUpdate("late")
	}

end


// StateBtn_goodnight
rule "rule triggered by StateBtn_goodnight"

when 
	Item StateBtn_goodnight received update
then
	if (StateBtn_goodnight.state == ON) {
			
			currentPhase="goodnight"
			ColorHue="7,100,13"
			hue_0210_001788a718e6_1_color.sendCommand(ColorHue)
			ColorHue="352,100,13"
			hue_0210_001788a718e6_2_color.sendCommand(ColorHue)
			// balkon licht an
			steckdose03Switch.sendCommand("ON")
			steckdose08Switch.sendCommand("ON")
			steckdose01Switch.sendCommand("ON")

			Current_BtnState.postUpdate("goodnight")
			
	}

end

// StateBtn_night
rule "rule triggered by StateBtn_night"

when 
	Item StateBtn_night received update
then
	if (StateBtn_night.state == ON) {
			
			
			currentPhase="night"
			
			// office aus
			steckdose16Switch.sendCommand("OFF")
			//wait(2)
			steckdose03Switch.sendCommand("OFF")
			steckdose08Switch.sendCommand("OFF")
			steckdose01Switch.sendCommand("OFF")

			Current_BtnState.postUpdate("night")

			ColorHue="315,100,3"
			hue_0210_001788a718e6_1_color.sendCommand(ColorHue)
			hue_0210_001788a718e6_2_color.sendCommand(ColorHue)
	}

end



// Current_BtnState received update
rule "rule triggered by Current_BtnState"

when 
	Item Current_BtnState received update
then
	
	var String curState = Current_BtnState.state.toString()

	for ( aState : statesArray) {
		if (aState==curState) {
			//nix?
		} else {
			
			postUpdate("StateBtn_"+aState,"OFF")
			
		}
	}


end


// if(Astro_Night_State.state != OFF)
rule "rule triggered by Astro_Night_State changed"

when
	Item Astro_Night_State changed
then
	if (Astro_Night_State == OFF) {
		StateBtn_early.sendCommand("ON")
	} else {
		StateBtn_evening.sendCommand("ON")
	}
end

Using the Design Pattern: Time Of Day couple make this a lot simpler for you.

Where have you seen a wait() in the docs or Rules examples? There is no such call. To cause the rule to stop for a set amount of time use Thread::sleep(milliseconds) where milliseconds is the number of milliseconds to wait. If you need something longer than a few hundred milliseconds you would be better off using a Timer, but make sure it works with the sleep first.

Some thoughts

  • use received command to trigger your rules in this case and use sendCommand to turn ON/OFF your time of day switches

  • add lots of logging to verify your rules are executing and where they run into problems

  • strongly consider adopting the Time of Day DP, I think it will greatly simplify your rules and make your approach a little easier to use elsewhere in your rules.

Dear Rich,

Thanks for your thoughts and input, I figured java knows how to wait(no pun intended) so I simply tried.
But using correct syntax certainly helps :wink:

With Thread::sleep(milliseconds) as you advised, and going up to 300 with the exec-bound calls (and sleep(50) for the ui- stuff) I do see a difference.

In fact, at first sight both issues look good now, so I give a “solved”.

Thanks for the link to the Time-based DP as well, triggering phase-change was not my current focus. I was thinking of calendars actually as I don’t live alone here, it is a well-known metapher for layman’s input and we have different rhythms. I.e. my daughter’s “night” - state triggers my “late”.

Again, thanks for your help,
regards
Daniel