Rule load, reload and shutdown

Are there any events that are triggered so the rules can tell the difference between initial startup and reload? (and shutdown)

When a rules is updated and say a timer is deleted from the source, will that timer continue to exist after the modified rule is loaded? What if it is running…later it will time out and do something unexpected…

Also when shutting down, some rules may need to restore things to a known state (say putting the thermostat fan back in AUTO mode, etc)

Tom

No. The rule will see both a system started and a reload as the same event. You cannot distinguish between the two through events.

When a rule file is loaded the entire context of that file is replaced. Any timers that were created will go away, and vars and vals will be reset to their defaults, and and System started rules will be executed. NOTE: This is based on observed behavior, not on an analysis of the code. I could be wrong.

Use a System shuts down trigger and put your code there. Do not create timers or use Thread::sleep with long sleep times within a shuts down triggered rule or OH may give up waiting and kill the rule before it is done running.

A suggestion for checking if rule is executed at initial startup or not is by making use of an Item for ProcesUptime for openhab itself out of the system info binding and then for execution in the rule to check if this item is greater then i.e. 60 (seconds), if it is less (or 0) you have initial startup else a reload. This way I protect my rules that are sensitive to system coming up timing.

3 Likes

Very nice! Thanks!

This is also based on observations only, but I am pretty sure I have had a few cases where running timers where still active after reload of a rule. As I remember, this caused some errors in the openhab log when the timers eventually expired. I am not sure, but I guess this could be caused by the fact that “the world” (context) has changed between the time the timer was created/started and the time is expired.

That is good to know. I often put a lot of checks in my Timers to just go away unless the state of the environment is just right so I may not have seen them going off after a restart. For example, I have a timer which sets a Present switch to OFF after five minutes of all my presence Items switching to OFF to avoid putting the house into away mode when I go to get the mail and the like. In this Timer I check to make sure that all the presence Items are still OFF before switching. If they are not the Timer silently goes away.

Now that I look at my setup, I’ve managed to simplify myself into only having two Timers left and both exhibit this behavior so perhaps I’m mistaken.

Could a rule unload event be created so the rules could handle it directly?

At least something to think about

Tom

I tried this and the rule does not seem to run at shutdown.

import org.openhab.model.script.actions.Timer
  val String PATTERN = "HH:mm "
 var Timer timerFanIdle = null
 var Timer timerFanCirculate = null
 var Integer ruleRunning = 0
 rule "CT100 Cleanup"
 when
    System shuts down
then
    sendCommand(HVAC_Fan_Mode, 0)
    logInfo("Motion", ts + "Shutting down, Turn Fan OFF (Auto)")
end

Nothing in the log and the fan stays ON

tom