Best practices for a template based trigger?

Hi all,

I want to create an automation that turns on a ceiling fan when a thermostat is reporting a lower temperature than a second thermostat. My initial thought is to use a trigger like this:

triggers:
  - trigger: template
    value_template: >-
        {{  states('climate.great_room_ecobee') == 'heat' and 
        (state_attr('climate.great_room_ecobee', 'current_temperature') <  
        state_attr('climate.2nd_floor_heat_ecobee', 'current_temperature')) and
        states('fan.great_room') == 'off' }}

But then it occurred to me that it might be better for the trigger to just focus on the states that I truly care about, and have the other checks in a conditional block:

triggers:
  - trigger: template
    value_template: >-
      {{ 
      (state_attr('climate.great_room_ecobee', 'current_temperature') <
      state_attr('climate.2nd_floor_heat_ecobee', 'current_temperature')) }}
conditions:
  - condition: state
    entity_id: climate.great_room_ecobee
    state: heat
  - condition: state
    entity_id: fan.great_room
    state: "off"

Is there any significant difference between these (other than cosmetic, etc)? Is this even the right approach to take for something like this? I’m a bit curious as to how HA even determines when to trigger this automation. Is it going to constantly evaluate that template for the final value, or is it smart enough to know what attributes I’ve specified and only evaluates the conditional when one of them changes?

The latter.

Don’t underestimate ‘cosmetic’ differences.

Your whole trigger can be rewritten to use state triggers and conditions. That’s much more readable, and, in the long term, much more likely to avoid headaches when debugging or making changes.

1 Like

Can you give me a pointer on how to do this? When I tried to create a state trigger it would only let me specify min/max values but not let me do a comparison against another state value. That’s why I went with the template approach…

Your less-than comparison of state attributes I’d probably leave as a template condition, to be fair to your original approach.

Your current template includes two other conditions, state-based ones, which can be written with state-based conditions–and you’ve done that in your alternative approach. I should have been more explicit in recognizing this.

But my message, should you heed it, remains: don’t underestimate ‘cosmetic’ differences. I’d strongly prefer the approach with conditions as put forth in your original post.