Will checking a template binary sensor causing HA to re-render the template everytime?

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:

The template is being re-evaluated when sun.sun changes state, irrespective of your automation condition.

1 Like

Why go to all the trouble of creating a template binary sensor to do this

When you can just do:

      - condition: state
        entity_id: sun.sun
        state: "below_horizon"
1 Like

The sun was just an example. The actual template sensor I’ll create is to calculate the precipitation of next 24 hour plus some weather condition.

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.

To expand even further (from the states docs):

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.

1 Like