Let’s say I define a imaginary binary sensor, and it’s used as condition in automation. When the condition triggers, will HA evaluate the template {{ states('sun.sun') == "above_horizon" }} in-place to determine the value of is_day_time, or will HA update the value of binary_sensor.is_day_time` in the background?
The reason I’m asking this is because I’ll define some template sensors that has complicated computation, thus evaluating the template everytime might be a waste.
- binary_sensor:
- name: Is day time
unique_id: is_day_time
icon: mdi:sun-angle
state: >
{{ states('sun.sun') == "above_horizon" }}
- id: auto_garage_light
alias: "Auto garage light"
trigger:
- platform: state
entity_id: binary_sensor.garage_human_presense_presence
from: "off"
to: "on"
condition:
condition: and
conditions:
- condition: state
entity_id: binary_sensor.is_day_time
state: "off"
- condition: state
entity_id: light.garage_light
state: "off"
action:
Then as Dave said, the template you create will only update when the entities you use in the template update. Not when you use the template entity in triggers or conditions.
Home Assistant keeps track of the states of entities in a state machine.
Any time you reference an entity’s state, HA obtains that state by looking it up in the state machine. The state is already there; it doesn’t get re-rendered upon lookup.
A state-based template sensor is re-rendered when the state of a referenced entity changes (or at the start of every minute if now() is used in the template).
A trigger-based template sensor is re-rendered when the trigger fires.
If the re-rendering of the template causes the state to change, then the state machine gets updated with that new state so that it is available for anything which references it.