This thread is not asking for help, more of a best practices discussion. I have a rule running on my system that at system start or 5 secs after midnight every day sets an unbound item to the day of the week. Other rules use this unbound item to make decisions, everything seems to work well.
rule "WhatDayofWeek"
when
System started or
Time cron "5 0 0 ? * * *"
then
DayOfWeek.sendCommand(now.getDayOfWeek)
end
Restarted the system OpenHAB runs on this morning and notice an error in the log during start up
08:14:18.263 [ERROR] [untime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'IsTheWeekend': Could not cast NULL to java.lang.Number; line 133, column 15, length 25
Easy enough to figure out, the item DayOfWeek was NULL when the rule attempted to set a variable to itās value. Here is the rule in question
rule "IsTheWeekend"
when
System started or
Item DayOfWeek changed
then
var daynum = DayOfWeek.state as Number
if (daynum > 5){
IsWeekend.sendCommand(ON)
}
else {
IsWeekend.sendCommand(OFF)
}
end
As I considered this error, I realized this was something that occurred because the system was starting and may not be of much concern. The item had not been set to any value when the rule file got loaded. Looking at the log in fact reveal that a mere 26 thousands of a second later the item got set
08:14:18.286 [INFO ] [smarthome.event.ItemCommandEvent ] - Item 'DayOfWeek' received command 7
08:14:18.296 [INFO ] [smarthome.event.ItemStateChangedEvent] - DayOfWeek changed from NULL to 7
(btw system is PC running Deb i3 4gb 1 tb) These two events are actually successive in the log. The two rules are also one right after the other in the rules file. Obviously the first rule above runs now.getDayOfWeek, the next rule listed runs immediately after this one and throws the error because the system is going to require 26 thousands of a second more time to be able to set the item so at that moment it is NULL
I donāt want to get into a discussion about fixing the architecture of the program or how to monkey with loading rules at start up. It is what it is.
Even thou I considered this error a ānonā problem, I still figured Iād see if I could craft a simple way to eliminate it. This has led me to rethink (and question) my strategy. My first thought was to test for NULL and perhaps set the value to a ānon-nominalā valueā¦ like zero. (1 = Monday, 7 =Sunday) That way the error on start goes away and a rule needing this value could be made to know 0 means the value is not yet set and re-trigger whatever mechanism that sets the value. After a minute that idea started to smell.
My next thought was test for NULL and use return; to bail out of the rule early if the value was not yet set. Then it occurred to me that maybe a better strategy may be to just not run the second rule at start up.
rule "WhatDayofWeek"
when
System started or
Time cron "5 0 0 ? * * *"
then
DayOfWeek.sendCommand(now.getDayOfWeek)
end
rule "IsTheWeekend"
when
//System started or
Item DayOfWeek changed
then
var daynum = DayOfWeek.state as Number
if (daynum > 5){
IsWeekend.sendCommand(ON)
}
else {
IsWeekend.sendCommand(OFF)
}
end
This seems the best solution for setting unbound items in a start up process. After all, with the System started line removed from the second rule, 26 thousands of a second later, the item gets set, theoretically that should trigger the second rule and all is good. Although start up is an edge case, the use of unbound objects as part of rules decision making requires these unbound objects to be set to something at start up.
Also, I have influxDB set up and grafana and a working persistence scheme to restore on start but for some reason, these unbound items (I use a few) donāt seem to get recorded in the DB and donāt get restored on start up. Still working on that (Iām a noob and have lots to learn)
Thoughts?