I’m hoping a core event processing guru can help me understand how to design Automations that remain efficient under high-frequency event load (>1000/min).
Background
I’m a former developer and have been doing home automation and multi-platform integration for 20+ years, but I’m new to Home Assistant. I’m coming from a fairly complex Universal Devices IoX environment, with Insteon/Z-Wave plus multiple PG3 Node Servers, including a couple I hacked together to close integration gaps.
Home Assistant has been a huge step change for me. Its modern architecture, breadth of integrations, consistently strong documentation, and community are all outstanding. I’ve closed integration gaps that had been open for years in a matter of minutes. What used to be “I’ll code around that someday” is now: find the integration, read the docs, test, done. I’m genuinely grateful for the work that’s gone into this project.
Context
I built an Automation that can trigger up to ~1000 times per minute when downstream elements are active. As a concrete example, a dimming hold event can generate 20–40 events/sec while active.
As you can imagine, I’d like to minimize processing overhead as much as possible for Automations like this one:
I’ve done a fair amount of reading, including threads like this one:
I’m trying to build a mental model for how Automations are executed internally, and I have a few questions based on that.
Questions
-
When defining a block of convenience variables in an Automation, are those templates executed eagerly when the Automation run starts, or are they evaluated lazily as needed?
For example, if I have an early “gross filter” condition that yeets ~95% of events before they reach more complex logic, will the templates for variables defined later already have been evaluated, or only for the events that pass the filter?
-
Is there any performance advantage to moving the bulk of action logic into a script, and using a smaller “router” Automation that filters the event stream and only calls the script for qualifying events?
In other words, can this reduce load on the event processing path, or does the cost of the Automation run still dominate either way?
-
For conditional branches that rely on templates, are those templates evaluated on demand in execution order, or are they all resolved up front when the Automation run begins?
This is slightly different from #1. I’m trying to understand execution behavior for conditional evaluation, not just variable assignment.
Working assumption
My current assumption is that:
- events enter the core processing pipeline
- Automations with matching triggers are evaluated
- for each matching Automation, templates are all rendered before the run
- then the action sequence executes
Note: I assume that enqueued Automatons don’t evaluate templates as they’re queued, only when they actually execute.
If that’s roughly correct, then it seems like pushing larger blocks of templating and branching into scripts might help reduce Jinja2 overhead in high-frequency scenarios, assuming that scripts are not evaluated unless called from an Automation.
I’d appreciate any guidance or corrections to the model in my head.
Thanks again. I’m already feeling very at home in this community.
