You have a fundamental miss understanding on how things are working under the hood.
When you create a trigger, it generates 1 or (more listeners depending on the trigger) for your trigger. In your hypothetical situation you have 1 template trigger with 37 entities being used.
This creates 1 listener that pays attention to 37 entities. With each state change on 37 entities the template is executed. If you have a state change or attribute change every 27 milliseconds, your listener is executing the template every 27 milliseconds. Then once your template resolves from false
to true
, your trigger then causes the sensor to resolve your state template. You are not bypassing the 27 milliseconds state changes, you are merely hiding them.
In your original question, it’s evident that you will end up needing a second trigger to capture the on
→ off
template change. This would create a second listener, and an additional template resolution when that trigger fires. To recap, you are now at 2 listeners listening to the same 37 entities firing every 27 milliseconds that when triggered cause the template entity to execute an additional template.
Now, if you forgo the trigger for the template entity, you actually reduce the number of template executions and the number of listeners. It goes from: 2 listeners executing templates every 27 ms triggering sensor template executions, down to: 1 listener executing a template every 27 ms.
You literally reduce the HA overhead by a factor of 2*x + 2.
Now lets look at other simple templates that you probably have. Lets say you have a simple template {{ is_state('sensor.a', 'foo') and is_state(sensor.b', 'bar') }}
. Without triggers, this would have 1 listener. If you add a state change trigger for each entity, you now have 2 listeners, effectively doubling the load again. It’s very easy to actually triple or quadruple the load when you add multiple triggers.
So the take away from this, you should utilize template entities without triggers inn most cases. At a maximum, templates will generate 2 listeners: 1 for time (if you use one of the datetime functions) and 1 for state changes (if you access the state machine) 1 listener for both time and state changes. So if you have 3 or more triggers on a template, it will likely create more load. There are exceptions to this, however I will not cover them here.
The take away from my post is that you should not use triggers for all template entities just because you think that it reduces the load or increases performance.
No, a single listener.
separate triggers → separate listeners.
single template → at most 2 listeners. 1 for time, 1 for entities. 1 listener.
You have to understand that templates have been optimized under the hood to reduce load. There’s even throttles in place to ensure your calculations don’t bog down your system.
EDIT: It’s been awhile since I’ve been in that part of the code. I just checked it out and templates only generate a single listener.