Rules don't trigger unless persistence data already exists

I have an arduino sending MQTT messages to OpenHAB. A rule responds to messages by doing things like “turn the light up” or “turn the volume down” etc.

The rule stores the current light level into a variable, increments the variable, then sends the command.

However when I remove persistence data (e.g. upgrade OH) the rule doesn’t trigger at all, no error shown on console. I can get the rule to trigger again by removing line 5 of the below:

rule "ArduinoBedroomVector"
		Item Bedroomvector received update
		var Number lightcircuit1level = BedroomLights.members.get(0).state as DecimalType
		case "light_1_up" : {
			lightcircuit1level = lightcircuit1level + 3
			if(lightcircuit1level>100) lightcircuit1level = 100
			Light_LG_Bedroom_Ceiling.sendCommand(lightcircuit1level); // send command to light

Of course, this stops the rule working, and it gives an error, but at least I know it triggers.

In order to make the rule work, and allow my light switches to control the light again, I have to go through all lights in the sitemap and “touch” the settings to give them a value, presumably this creates the persistence data.

Can anyone tell me why? And how best to resolve this? I don’t want to set all light levels at startup every time.

I have the workaround, but it happens quite often when I set up new items and change them etc.

var Number lightcircuit1level = BedroomLights.members.get(0).state as DecimalType

What’s going to happen here at first run? I’m guessing that without persistence it’ll be undefined. Undefined+3 is still undefined.

What is the objection to using persistence as the fix? If that is not acceptable, then you will have to define initial values at start up, or, restructure your rules so that they deal sensibly with undefined values.

@rossko57 Thanks - I have created a master “reset” rule. I had been wracking my brain why some other settings weren’t working (e.g. lighting scenes) then realised I’d previously (a year ago) set up rules that depended on other rules… now I have a rule that just triggers them all as a one-off for when I upgrade. All sorted now!