Automatically switch heating & cooling modes based on temperature

I found an automation for switching the HVAC mode based on the temperature of the room, but it doesn’t seem to be automatically running. If I manually run the script, it works perfect. The climate & sensor are in the description line.

Any ideas?

alias: BR Climate Auto Mode
description: sensor.br_th_temperature climate.br_climate
trigger:
  - platform: template
    value_template: >
      {% set mode = states('climate.br_climate') %}
      {% set trg = state_attr('climate.br_climate', 'temperature')|float %}
      {% set cur = states('sensor.br_th_temperature')|float %}
      {{ mode == 'heat' and trg <= cur - 2 or mode == 'cool' and trg >= cur + 2 }}
action:
  - service: climate.set_hvac_mode
    entity_id: climate.br_climate
    data_template:
      hvac_mode: >
        {% set mode = states('climate.br_climate') %}
        {{ 'heat' if mode ==  'cool' else 'cool' }}
mode: single