I was thinking previously about this but never really concluded the point about how are the automations, templates and triggers managed with the events on entity changes.
Mostly also how does the number of entities or number of automations increase the overall load complexity.
I can think of few cases how to manage automations properly:
-
Each time there is an event, regardless if which one, python checks all triggers from all automations. If it is entity trigger, then it checks if entity matches, if it is area or label as a target, then it extracts the area/label and checks if the entity is inside or if the device that holds the entity belongs to this area/label. That would have a direct linear growth of number of entities and another linear growth on number of automations, making it likely
O(n^2). This also means that all template sensors must be reevaluated on each entity change, except those that have a special trigger defined? -
Each automation, when loaded, attaches itself to the specific entities or devices. When the target is area/label, the change of the entity must check the area/label of this entity and then check if there are any triggers attached to this area/label. This can use the "pointers" and linked list of attached elements to the sensor entity in the state machine.
-
When using templates, when template is generated, it must extract the list of entities mentioned in the templates (so there is a templates "compiler" somewhere?) to attach itself to specific list of entities. When entity state changes, the entity holds the list of attached triggers or other template sensors that need reevaluation.
Does such logic make sense? I guess point nr.1 is not used (altho the easiest to implement and maintain), while the points nr.2 and 3 don't impact the event manager performances when automations and entities grow.
Is there a good docs to read about this how the number of automations/sensors impacts the performances?