Need help with automation action (if else)

I’ve created an automation for my thermostat to turn back on when someone gets home and I’m stuck with creating if else in my data to tell the thermostat operation mode turn on heat or cold based on current temperature.
here is what I created but it doesn’t work

- id: '1561589408139'
  alias: Thermostat Home Mode
  trigger:
  - entity_id: group.all_devices
    platform: state
    to: home
  condition: []
  action:
    entity_id: climate.thermostat
    service: climate.set_operation_mode
    data:
      operation_mode: >-
        {% if 'states.climate.thermostat.attributes.current_temperature' <= '75' %}heat
        {% elif 'states.climate.thermostat.attributes.current_temperature' > '75' %}cool
        {% else %}auto{% endif %}

when I use the operation mode declaration in template editor it returns the correct value (cold or heat) but not working in automation.
could anyone please help me on this?

You shouldn’t be quoting like you are. And there’s really no point for the elif the way you have it written. Oh, and you need to use data_template instead of data. How about:

- id: '1561589408139'
  alias: Thermostat Home Mode
  trigger:
  - entity_id: group.all_devices
    platform: state
    to: home
  condition: []
  action:
    entity_id: climate.thermostat
    service: climate.set_operation_mode
    data_template:
      operation_mode: >
        {% if state_attr('climate.thermostat', 'current_temperature')|float <= 75 %}
          heat
        {% else %}
          cool
        {% endif %}
2 Likes