OpenHAB3 Start Up Issues - Race Condition? How to Sequence Startup?

For the benefit of others that are fighting the OH3 startup timing / race issue here is what I found works on my 2 OH3 Windows 10 installations:

.rules
var Number GreenFlag = 0

//01 - StartUp Initializations =====================================================================================================
rule "Startup_Initializations"
	when
		System started
	then
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)
		Thread::sleep(5000)

		GreenFlag = 0
	
		logInfo("Startup_Initializations", ">>>>> ENTERED RULE = Startup_Initializations *****************************************************************") 

blah blah blah initialization stuff

		GreenFlag = 1

		Initialized_Time = now.toInstant.toEpochMilli 
		
		Date.postUpdate(new DateTimeType())
		Rule_TimeStamp4SI = Date.state.format("%tD") + " @ " + Date.state.format("%tr")
		
		logInfo("Startup_Initializations", ">>>>> Startup_Initializations GreenFlag set to 1 at " + Rule_TimeStamp4SI)
		
	
	
		logInfo("Startup_Initializations", ">>>>>>>>>>>>> RULE = Startup_Initializations.  FINAL CHECKING mqtt ACTIONS BEFORE PUBLISHING ***************")
		OHOB_WM_String.postUpdate(Rule_TimeStamp4SI + " >" + "\nMsgStamp:0;" + "\n" + "EOS<")
		mqttActions = getActions("mqtt","mqtt:broker:hsbroker")
		logInfo("Startup_Initializations", ">>>>>>>>>>>>> RULE = Startup_Initializations.  mqttActions = " + mqttActions)
		logInfo("Startup_Initializations", ">>>>>>>>>>>>> RULE = Startup_Initializations.  Ready to publish to topic '/WatchMan/OHOB_WM_String'")
		mqttActions.publishMQTT("/WatchMan/OHOB_WM_String", OHOB_WM_String.state.toString)
		logInfo("Startup_Initializations", ">>>>>>>>>>>>> RULE = Startup_Initializations.  mqtt FINAL CHECK WAS A SUCCESS ******************************")


end


rule "Geo_Presence" //=====================================================================================================
when
	Item OHC_Presence_Robert changed or
	Item OHC_Presence_Janet  changed or
	Item OHC_Presence_Peter  changed or
	Item OHC_Presence_Hannah changed or
	Item OHC_Presence_Guest  changed
	
then	if (GreenFlag == 0) return;

blah blah blah

end

I have read many times to avoid sleeping for long periods of time. However, I can find no other way, or anything I can test for in a loop. In fact, I have found the loop counter will fail / error on Startup.

var ii = 1
someSystemCondition = ? // like startup level = 100?
while ((ii=ii+1) < 60) && (someSystemCondition)) {
	Thread::sleep(1000)
	someSystemCondition = ?
}

I found that 12-15 Thread::sleep(5000) usually worked, so I added 100% margin and made it 24. (and I have had it still fail on 1st time launch on a new installation.)

Any parting advice or comments will be appreciated before I close this thread.

…thanks

1 Like