Bootup errors in rules


I have problem with few rules at startup. I have tried to check that all variables needed are available before proceeding in order to avoid trying to operate on nulls. However, I also have the same problem with rules where I use “builtin” data from OpenHAB, such as “now”. I am doing this wrong, or am I supposed to check that everything, even “now”, is not null before I try to use it?

One example:

// Update auto_glightsOutside_on when Astro updated or auto_glightsOutside_minBeforeSunset changed or once hourly
rule “lights_outside_evening_auto_updateTurnOnTime” when Item astro_sun_set_start changed or Item auto_glightsOutside_minBeforeSunset changed or Time cron “0 0 0/1 1/1 * ? *” then
// Default turn on at 19:00
var onTime = new DateTime(now.toString.split(“T”).get(0) + “T19:00:00+02:00”)
// Use astro & setpoint values if available
// Check that both values are set
if (astro_sun_set_start.state !== NULL && auto_glightsOutside_minBeforeSunset.state !== NULL) {
// Calculate turn on time
onTime = new DateTime(astro_sun_set_start.state.toString)
onTime = onTime.minusMinutes((auto_glightsOutside_minBeforeSunset.state as Number).intValue)

However, this results in an error:

2020-10-06 07:26:51.292 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘lights_outside_evening_auto_updateTurnOnTime’: ‘toString’ is not a member of ‘null’; line 189, column 28, length 12
Line 189 is the one with now.toString.

Please use code fences… you have used quotes. You can go back and edit your post :slightly_smiling_face:.

If you are trying to create a DateTime for 7pm today, use…

var onTime = now.withTime(19, 0, 0, 0)

This can happen under some circumstances. openHAB modules load sequence is pretty poorly controlled, and rules can end up running before all resources are ready.
This invariably shows up after an upgrade, where the system cache is cleared. For many users, this gets better after a second reboot, with the cache rebuilt.

Note that if you are playing with OH in a non-24hour development setting - the cache contents time out while the system is powered off, and you will run into this again at next play.

You can test if that is what happened by reloading your rules file - make a small edit perhaps - and see if startup rules run okay then.

There is an easy “delay rules” workaround for openhabian users.

@5iver will tell you that new OH3 rules system does not suffer from this problem, and if you are starting out you should in any case consider learning that instead.

1 Like

Thank you for your replies! I updated my post with the code fencing.

I will try the proposed way. I use OpenHAB 24/7, so I experience these errors only at boot. I am happy to hear it will be solved in future releases, as I feel it adds alot of unnecessary overhead into the rules. :slight_smile: