How to use jinja template in automation?

I’m trying to build an automation that sets the Ecobee thermostat preset_mode based on some other conditions. I’m getting this error:

Error: Preset mode {{preset_mode}} is not valid. Valid preset modes are: Wake up, away, home, TOU peak, sleep, away_indefinitely

For debugging, the minimal automation is below. The variable works fine in the notification. If I try removing the quotes around {{preset_mode}} then it won’t let me save:

Message malformed: not a valid value for dictionary value @ data[‘type’]

alias: variable debug
description: ""
trigger: []
condition: []
action:
  - variables:
      preset_mode: sleep
  - service: notify.persistent_notification
    data:
      message: |
        Changing Ecobee preset to {{preset_mode}}
      title: AC solar adjustment
  - device_id: b5ba72bc79d271b2f021431c7c5ea7fb
    domain: climate
    entity_id: 100431ae8e4e62dc801359688c6a2047
    type: set_preset_mode
    preset_mode: "{{preset_mode}}"
mode: single

I do. ot think device actions support templates. You should avoid device actions for several other reasons too.

Better to use a service climate.set_preset_mode instead, service calls always support templates.

1 Like

Thank you! It works as a service, and that’s good advice to avoid device_ids.

2 Likes