Template
The Template condition checks whether a template renders true right now. Use it when no other condition can check what you need. For example, a template can compare two sensors with each other, or check the data of the trigger that started the automation.
If another condition can do the same check, such as State or Numeric state, that condition is easier to set up and to read later.
Using this condition from the user interface
If you prefer building automations visually, Home Assistant walks you through this condition step by step. You pick what to check, tweak a few options, and save. No YAML knowledge required.
To use this condition in an automation:
- Go to Settings > Automations & scenes.
- Open an existing automation, or select Create automation > Create new automation.
- In the And if section, select Add condition.
- From the search box, search for and select Template.
- In Value template, enter a template that renders
truewhen the automation should continue.- To try your template first, use the template editor.
- Select Save.
Options in the UI
Using this condition in YAML
If you work directly in YAML, or you want to know exactly what Home Assistant does under the hood, this section has the technical reference. It lists the field names you use in YAML, their types, and which ones are required.
In YAML, use condition: template. A basic example looks like this:
condition: template
value_template: >
{{ state_attr('climate.living_room', 'temperature') | float(0) > 20 }}
This passes when the target temperature of the living room thermostat is above 20.
Options in YAML
The options in YAML are the same as in the UI.
The template to check. The condition passes if the template renders true.
Shorthand notation
In YAML, you can also write a template condition as the template only, without condition and value_template. The following example does the same as the basic example:
conditions:
- "{{ state_attr('climate.living_room', 'temperature') | float(0) > 20 }}"
You can use the shorthand everywhere Home Assistant accepts a condition. This includes:
- The
conditionsof an automation, alone or in a list with other conditions - The
conditionsof the And, Or, and Not blocks - The
conditionsof a Choose option, and thewhileanduntilof a Repeat block - The
ifof an If-then block - A Condition step between the actions, written as
condition: "{{ ... }}"
The following example combines two shorthand templates with a State condition. All three must be met:
conditions:
- "{{ is_state('person.sam', 'home') }}"
- condition: state
entity_id: alarm_control_panel.home
state: "disarmed"
- "{{ now().hour < 22 }}"
Good to know
- The condition only passes if the template renders
true. Upper and lower case don’t matter, soTruealso passes. Other values, such as1,yes, oron, don’t pass. - Combining values with
andorordoesn’t always rendertrueorfalse. For example,{{ my_list is defined and my_list }}renders the list itself. The condition then doesn’t pass, even if the list isn’t empty. To gettrueorfalse, use a comparison, such as{{ my_list is defined and my_list | length > 0 }}. - If the template has an error, the condition fails with an error. The error is shown in the trace.
- If an entity in the template is unavailable or unknown,
floatandintcan’t convert its state, and the template fails with an error. Give them a default value, such asfloat(0), or check the entity first withhas_value(). - In an automation, the template can use the
triggervariable, which describes what started the automation. For details, refer to Available trigger data. - This condition checks the template right now. To start an automation when a template becomes true, use the Template trigger.
- For more about templates, refer to Templates in automation conditions.
Try it yourself
Ready to test this? Go to Settings > Automations & scenes, open an automation, and add this condition. Trigger the automation with and without the condition met, and watch whether it continues or stops.
More examples
Real scenarios where this condition gates an automation. Copy any example and adapt it to your setup. In the visual editor, select the UI options listed in the example or paste the YAML code.
You don’t need to edit YAML to use these examples. Copy a YAML snippet from this page, open the visual automation editor in Home Assistant, and press Ctrl+V (or Cmd+V on Mac). Home Assistant automatically converts the pasted YAML into the visual editor format, whether it’s a full automation, a single trigger, a condition, or an action.
Automation: suggest opening the windows when it’s cooler outside
At 18:00, this automation sends a notification, but only if it’s cooler outside than inside.
-
Trigger: Time
- At time: 18:00:00
-
Condition: Template
-
Value template:
{{ has_value('sensor.outdoor_temperature') and has_value('sensor.indoor_temperature') and states('sensor.outdoor_temperature') | float < states('sensor.indoor_temperature') | float }}
-
Value template:
-
Action: Send a notification message
-
Target: My Device (
notify.my_device)
-
Target: My Device (
YAML example for suggesting to open the windows
alias: "Suggest opening the windows"
triggers:
- trigger: time
at: "18:00:00"
conditions:
- condition: template
value_template: >
{{ has_value('sensor.outdoor_temperature')
and has_value('sensor.indoor_temperature')
and states('sensor.outdoor_temperature') | float
< states('sensor.indoor_temperature') | float }}
actions:
- action: notify.send_message
target:
entity_id: notify.my_device
data:
message: "It's cooler outside. Open the windows to cool down the house."
Automation: notify only about a big jump in power use
When the power use of the house changes, this automation sends a notification, but only if the power went up by more than 2000 W at once. The template uses the trigger variable to compare the old and the new value.
-
Trigger: State changed
-
Entity: House power (
sensor.house_power)
-
Entity: House power (
-
Condition: Template
-
Value template:
{{ trigger.from_state.state | is_number and trigger.to_state.state | float(0) - trigger.from_state.state | float > 2000 }}
-
Value template:
-
Action: Send a notification message
-
Target: My Device (
notify.my_device)
-
Target: My Device (
YAML example for a notification about a jump in power use
alias: "Notify about a big jump in power use"
triggers:
- trigger: state
entity_id: sensor.house_power
conditions:
- condition: template
value_template: >
{{ trigger.from_state.state | is_number
and trigger.to_state.state | float(0)
- trigger.from_state.state | float > 2000 }}
actions:
- action: notify.send_message
target:
entity_id: notify.my_device
data:
message: "The power use of the house just went up by more than 2000 W."
Still stuck?
The Home Assistant community is quick to help: join Discord for real-time chat, post on the community forum with the condition you’re using and what you expected to happen, or share on our subreddit /r/homeassistant.
AI assistants like ChatGPT or Claude can also explain conditions or suggest the right one when you describe what you want in plain language.
Related conditions
Explore related conditions:
-
State: Tests if an entity, or one of its attributes, has a specific state.
-
Numeric state: Tests if the number in the state of an entity, or in one of its attributes, is above or below a limit.