Rule stop working because an item becomes undefined:but why?

I have a working openhab setup that executes a rule every 30 seconds, controlling the power of 5 smart switches: this setup has been working since this summer relatively well.

I’m gradually adding new rules and new bindings and the startup time is getting longer but everything still works.
Recently I found that the rule stops working and the openhab log is full of

2016-11-04 18:36:30.130 [INFO ] [.o.m.s.P.state Not initialized] -  
2016-11-04 18:37:00.454 [INFO ] [.o.m.s.P.state Not initialized] -

everytime the rule is invoked.I think that this is due to the fact that an item whose state I test at the begininng of the rule becomes undefined: but why does it happen? looking at the logs, after several hours the undefined error disappeared, but the , several hours after, reappeared. Do you have some suggestions on how I can further debug this strange issue?
Thank you,

What is the rule?

What is the Item definition?

Have you considered using persistence with restoreOnStartup?

Items that are not configured with persistence and restoreOnStartup are initialized to Undefined (NULL) whenever OH restarts or there is a change to a .items file forcing a reload.

@rlkoshak, thanks for your fast response. I will use the persistance for the states of the items, as you suggest. You are right, my question was generic: let me add here the relevant part of the item, sitemap and rule definitions.

Number PowerStatus "Power Usage Status [%.1f]"
Switch PowerControl "Load shread [MAP(] "  <energy>   (All)

//sitemap entry
	Switch item=PowerControl icon="energy"

and here are the rules

rule "Start System"
		System started  
rule "Check Switch"
	    Time cron "0/30 * * * * ?”"

	var String tmp="Default1"
	if (PowerControl.state == Uninitialized ) { // I added this further check
		tmp=" "
			logInfo("PowerControl.state Not initialized",tmp)
		if (PowerControl.state == ON ) if (PowerStatus.state == Uninitialized) {
			tmp=" "
			logInfo("PowerStatus.state Not initialized",tmp)
			// Code that implements the control logic
                       // and updates the PowerStatus.state

I was thinking that the “Start System” rule would always be executed before the “Check Switch”, therefore the two items PowerControl and PowerStatus should never be undefined.
Maybe I made some modification to the item file and the “start system” rule has not been executed. Probably it will be safer in the future, to stop openhab, perform modifications and the restart openhab. But looking at the logs, the items became undefined at a time in which I did not make any modifications, therefore I’m not fully convinced that this is what really happened.
In any case, I hope that using the persistence on startup this behavior will never happen again. Otherwise, I will come back here.
Again, thank you for your suggestions,

If the timing is just right it is possible for that error to run before the system started rule runs. I’ve noticed there can be a significant delay from the time a rules file is loaded to the System started rule executes.

I wouldn’t stop OH to modify the rules. It is far better to write the rules to deal with the Undefined or use Persistence to avoid the Undefined. Stopping OH to work on errors will take longer and leave your errors in a more brittle state.

1 Like

Thank you for your further suggestions. I’ve implemented the persistence and everything is working well so far.