'System shuts down' trigger?

I found some references to System shuts down trigger in the forums, but the current documentation doesn’t seem to mention it: Rules | openHAB

Has this trigger been removed?

System shuts down Is mentioned in the docs for OH2.5, but no longer in OH3.0, so I guess this trigger was removed.
Never made much sense from my point of view.

I don’t know how practical this can be to detect system closedown, but (some?) scripts can be set up to run something when a module is unloaded.

Until we needed to send an MQTT LWT Offline on an orderly shutdown… :slight_smile:

Yes I’m aware of this one. It calls the script / callback function when the script is unloaded, which can be due to a reload of that script, and not just when openhab itself is shutting down.

So it is not called at sytem shutdown?

I think it was in the docs but never actually implemented, to be honest. It was dropped as even a goal in OH 3 I think.

I think the only viable solution to that will be in the binding. There are all sorts of timing and other situations where using a rule for this won’t work. For example, when the system is shutting down, the Things are shutting down too. It’d be a race between the rule sending the message before the Thing goes OFFLINE.

The Thing can go OFFLINE normally on its own for other reasons too, such as a reinstall of the binding and the like. A rule would never see those cases.

The closes we can come is a scriptUnloaded function but I’d expect it to work only occasionally depending on the timing.

This needs to be in the binding.

I agree that sending the LWT should ideally be in the MQTT binding.

Regarding the race before the thing goes offline - at the moment it’s a moot point because there is no “shut down” trigger. If one were to be implemented, it could be executed before putting things/items offline, so that rules can still do things with things/items as the final task before they go offline. Then after rules are stopped / unloaded, openhab can make sure that the event bus is empty before shutting down things/items so that all the commands sent by rules get sent out.

… easily available to rules.

The MQTT binding knows, and does a tidy shutdown including unsubscribes, before disconnectiing.

That’s not how it would work though. As with everything in OH, all parts of OH get the shutdown event at the same time and they start shutting down at the same time. You would need to fundamentally rearchitect the OH start-up/shutdown process… again (assuming it’s even possible since we are limited to what Karaf can provide in this regard) , to make it so the shutdown rules run before the Things start to go offline. And all of this to solve a problem that should be fixed in the binding in the first place. I think that approach is a non-starter.

No doubt there are other situations where some user-defined prequel to system shutdown would be useful.

I think this would have to take the form of an “intercept”. i.e. when openHAB gets a “shutdown” directive, it first calls an optional user script. Only when the script exits does shutdown get passed to everyone else.
Not having this already, despite ‘system shutdown’ triggers being considered in at least two generations of openHAB, probably reflects that it’s not easy to do. Having a simple rule trigger isn’t going to be much help as the rule engine closes down around you.