Variable initialization problems during startup

I have following rule (OpenHab 2.3):

import java.util.Map
import java.util.List
import org.joda.time.DateTime

var Map<String,String> sunriseTimes = newHashMap
var Map<String,String>  sunsetTimes = newHashMap

// initializacja danych niezbednych do reguly
rule "Light_startup"
	when
		System started		
	then
..........
	sunriseTimes = newHashMap  // initialize once again
	sunsetTimes = newHashMap
.........
end

Sometimes during startup such errors what stop execution of rule

2018-09-26 11:17:04.257 [INFO ] [.eclipse.smarthome.model.script.FILE] - Light_startup:Initialize sunrise/sunset hashmap
2018-09-26 11:17:04.266 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Sunrise_sunset_ext_light': cannot invoke method public abstract java.lang.Object java.util.Map.get(java.lang.Object) on null

What is strange sometimes everything is executed properly :frowning: Any ideas what is wrong?
I have to say that starting OH system where I have many rules is most problematic situation, usually I have many false error messages and after few mintutes situation is stable. But sometimes some variable are not correctly set and rules do not work correctly.

Michal Szymanski

This is normal
The start up of openhab happens in no particular order that I know off
I sometimes get transformation errors because they are not loaded yet
Or rules errors because the items are not loaded yet
And so on

I try not to rely on system started triggers

In this example all objects are only in one rule and order of execution is not a problem, but you are right order of execution is random and in many cases is a big pain (in some situation some item can be set to unpredictable values)

Check for NULL value in your rules and use restoreOnStartup in your persistence service

Saying it is normal isn’t how I would describe it. I’d say it’s expected.

@Michal_Szymanski, there is a known issue in OH right now where Rules can start to execute before they are ready to process. For example, they can start running before all the Items in .items files have been loaded and populated or event before the whole .rules file where that one Rule lives is done being loaded.

There is an issue open to address this problem but this sort of thing is often very difficult to fix.

In this case what is likely happening is either the System started Rule triggered before the Maps were initialized, or the value you were passing to the Map was not a String.

But one has to ask, why the Maps in the first place? Why not just put the times in Items or use the Astro Action?

Right now my workaround is that I analyse logfile after each startup and in case of problem simply restart OH. In the same time I minimize restart using UPS in my installation :slight_smile:

Regardnig AstroAction, I think it was not in OH2.1 and I initially wrote rules for this version, but now I will try use Astro action.

One more question how to install this Astro Action I have no this action in PaperUI Action tab.

Michal

Hmmmm. The docs indicate that you need to have the Astro 1.x version of the Astro binding installed to use it. So you probably need to enable legacy bindings. Maybe it isn’t supported despite there being an entry in the docs for it.