OpenHAB Start-up Issues

I love OpenHAB, and I use it extensively, however the start-up issues have always plagued me. I wish I had more experience to fix our issues, and I’m reaching out to see if I am missing something. Will I always have to implement these work-arounds? I appreciate your support.

I am using 2.5 M1

I have 2 main issues.
1.) Rules load while persistence loads, which can trigger the rules. My work-around below works 100% of the time.
2.) Persistence values don’t fully load. I’ve have to reload the bundle, but recently I implemented the start-levels below, and I haven’t had the issue re-occur yet. Is there a better way to ensure persistence works at startup? I read a comment by @Kai that said start-levels should never be changed by the user.

For the rules (issue 1), my workaround is this code at startup. SystemStartingFirstTime is not saved in the mapdb persistence database

rule "Startup"
    when 
        System started
    then
		if (!(SystemStartingFirstTime.state != OFF || SystemStartingFirstTime.state != ON))  // only turn on if SystemStartingFirstTime is undefined
		{
			logInfo("rules", "Rules Execution is delayed")
			SystemStartingFirstTimeDateTime.postUpdate(new DateTimeType())  // Update the time
			postUpdate(SystemStartingFirstTime,ON)
		}
end

rule "Every minute"
when
	Time cron "	55 0/1 * 1/1 * ? *"
then
	if (FirstRunEveryMinute == 1)
	{
		FirstRunEveryMinute = FirstRunEveryMinute + 1
	}
	else
	{
		if (now.isAfter((new DateTime((SystemStartingFirstTimeDateTime.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli)).plusMinutes(2)) && SystemStartingFirstTime.state != OFF)
		{
			logInfo("rules", "System Started (Every Minute rule probably because it was a fresh reboot)")	
			postUpdate(SystemStartingFirstTime,OFF)
		}
end

rule "Update Clouds_Inside_Lights_On"
when
	Item Current_Cloud_Cover_Darksky changed or
	Item Clouds_Inside_Lights_On_Trigger changed
then
	if (SystemStartingFirstTime.state != ON)
	{
		if ((Current_Cloud_Cover_Darksky.state as Number).floatValue >= (Clouds_Inside_Lights_On_Trigger.state as Number).floatValue )
		{
			if (Clouds_Inside_Lights_On.state != ON)
			{
				postUpdate(Clouds_Inside_Lights_On,ON)
			}
		} else
		{
			if (Clouds_Inside_Lights_On.state != OFF)
			{
				postUpdate(Clouds_Inside_Lights_On,OFF)
			}			
		}

	}
end		

My persistence issue is that not all my items get loaded with the values. I simply just reload the bundle, but what frustrates me is that there might be a time such as during a power outage that I won’t be around to reload the bundle.

I have tried to fix the start-up issues also with a start-up script located in /var/lib/openhab2/etc/scripts/ I found this on the forum, but I can’t locate the post at the moment.

name: start-level.script

//bundle:start-level org.openhab.core.model.thing 81
//bundle:start-level org.openhab.core.model.thing.ide 81
//bundle:start-level org.openhab.core.model.thing.runtime 81
//bundle:start-level org.openhab.core.model.item 82
//bundle:start-level org.openhab.core.model.item.ide 82
//bundle:start-level org.openhab.core.model.item.runtime 82
//bundle:start-level org.openhab.core.model.sitemap 88
//bundle:start-level org.openhab.core.model.sitemap.ide 88
//bundle:start-level org.openhab.core.model.sitemap.runtime 88
//bundle:start-level org.openhab.core.model.rule 90
//bundle:start-level org.openhab.core.model.rule.ide 90
//bundle:start-level org.openhab.core.model.rule.runtime 90
//bundle:start-level org.openhab.core.model.persistence 90
//bundle:start-level org.openhab.core.model.persistence.ide 90
//bundle:start-level org.openhab.core.model.persistence.runtime 90
bundle:start-level org.eclipse.smarthome.model.thing 81
bundle:start-level org.eclipse.smarthome.model.thing.ide 81
bundle:start-level org.eclipse.smarthome.model.thing.runtime 81
bundle:start-level org.eclipse.smarthome.model.item 82
bundle:start-level org.eclipse.smarthome.model.item.ide 82
bundle:start-level org.eclipse.smarthome.model.item.runtime 82
bundle:start-level org.eclipse.smarthome.model.sitemap 88
bundle:start-level org.eclipse.smarthome.model.sitemap.ide 88
bundle:start-level org.eclipse.smarthome.model.sitemap.runtime 88
bundle:start-level org.eclipse.smarthome.model.rule 90
bundle:start-level org.eclipse.smarthome.model.rule.ide 90
bundle:start-level org.eclipse.smarthome.model.rule.runtime 90
bundle:start-level org.eclipse.smarthome.model.persistence 85
bundle:start-level org.eclipse.smarthome.model.persistence.ide 85
bundle:start-level org.eclipse.smarthome.model.persistence.runtime 85

System:
OpenHAB 2.5 M1
ESXI VM with 3 cores, Xeon 3.2 GHz, 4 GB memory. Most of the load average is 0.0 with 30% memory utilization. Zulu 8 on Ubuntu 18.04
Approximately 1800 items, 22000 lines of rules (No Thread::sleep, all timers, no locks), 20 z-wave devices, 4000 lines of sitemap code
Bindings: z-wave, http, tcp, mail
Persistence: mapdb (default), influxdb (5% of items)

Try the solution from this thread. It’s also implemented in openHABian.