Rule life cycle

Can anyone clarify on the HABApp Rule object life cycle?

So far I have created scripts defining Rule derived classes and instantiated an object of this class afterwards. Without looking into the complete source code, I assume the Rule class takes care to register the rule object somewhere and to schedule it for triggering.

Question 1: in case I reload the script during development, is this rule object properly removed so it doesn’t conflict with the next script version’s rule object?

Question 2: in case I create a rule dynamically from another rule… What is the proper way to do this? Just instantiate a rule object when handling a trigger callback? Will this rule object be registered like a “global” object (see Question 1) and do I need to take care it is cleaned up manually when the script is replaced by a new version?

I asked ChatGPT (like so many things during a day of work) and it mentioned two functions

from HABApp.core.wrapper import load_rule, unload_rule

to use. These functions however seem to have changed since the point in time ChatGPT has been trained. At least I can’t find them…

Thanks for your insights.

yes

The current premise is that when your script is loaded all rules have been created. Everything else is currently unsupported.
You can have complex logic, for loops, functions, classes etc. that create the rules but once the module is finished everything has to be there.

I for example create rules based on parameter files dynamically.

class RollladenAutomatik(Rule):
   ...

for name in HABApp.Parameter('rollladen').value:
    RollladenAutomatik(name)

I’m not sure why you would want to create rules dynamically, you can however create event listeners and jobs dynamically during the rule life cycle so it’s actually not really necessary.


They never existed, it’s just ChatGPT inventing stuff again.

1 Like

Well, the “dynamic” nature is probably less dynamic than I wrote, actually it is not a Rule that creates other rules but a plain object RoomVentilation. Furthermore, rule creation is done in its __init__() function, not a trigger. My wrong.

This is what I do after creating the class RoomVentilation.

ventilations = {
	RoomVentilation(u"Küche", "Kueche",
		ESP32_Weatherbase_Temperature,
		Heating_Thermostat_Wohnzimmer_Temperature, 21, 24,
		[Security_HomematicContact_Kueche_Contact, Security_HomematicContact_KuechenFenster_Contact]),
	RoomVentilation("Bad", None,
		ESP32_Weatherbase_Temperature,
		Heating_Thermostat_Bad_Temperature, 22, 26,
		Security_HomematicContact_Badezimmer_Contact),
	RoomVentilation("Wohnzimmer", None,
		ESP32_Weatherbase_Temperature,
		Heating_Thermostat_Wohnzimmer_Temperature, 21, 24,
		Security_HomematicContact_Terasse_Contact),
	RoomVentilation(u"Büro Monika", "BueroMonika",
		ESP32_Weatherbase_Temperature,
		None, 21, 24,
		Security_HomematicContact_Buero_Contact),
	RoomVentilation("Anbau Nord", "AnbauNord",
		ESP32_Weatherbase_Temperature,
		Heating_Thermostat_AnbauNord_Temperature, 16, 24, # wide range to reduce notifications
		Security_HomematicContact_AnbauNord_Contact),
	RoomVentilation(u"Anbau Süd", "AnbauSued",
		ESP32_Weatherbase_Temperature,
		Heating_Thermostat_AnbauSued_Temperature, 16, 24, # wide range to reduce notifications
		Security_HomematicContact_AnbauSued_Contact)
}

RoomVentilation kind of coordinates the ventilation in a room based on wind / door contacts, temperatures and some configuration.

Rather then discussing other approaches (as a computer scientist I’m well aware there are always many solutions, structures, algorithms), I really would like to understand the live cycle of the rule objects created…

With the information above (rules are created when Python executes the creation of ventilations) plus the information that the rule objects link the RoomVentilation object and vice versa, will reloading the script clean up the rules properly or is it required to explicitly unlink / release them?

Then why is RoomVentilation not a plain Rule?
There’s no benefit and no need to additionally wrap the rule.

It should work but without actually seeing your implementation it’s impossible to be sure. It’s always possible to do something dirty that kills the HABApp mechanism.

Just look at the HABApp log, there you see the rules loaded. The rule name is unique so if the counter ever increases it does not work.
Or even better: manually set the rule name and then you get an error on duplicate names.

O.k., I have refactored some rule creation, they are all instantiated on global level now.