Template

The Template trigger is useful when no single entity tells you what you want to react to. You write a templateA template is an automation definition that can include variables for the action or data from the trigger values. This allows automations to generate dynamic actions. [Learn more] that combines several entities or calculates a value, and the trigger reacts when the template changes from false to true. For example, it can react when your solar panels produce more power than your home uses, or when the power use of a washing machine drops to almost zero.

Using this trigger from the user interface

If you prefer building automations visually, Home Assistant walks you through this trigger step by step. You pick what to watch, tweak a few options, and save. No YAML knowledge required.

To use this trigger in an automation:

  1. Go to Settings > Automations & scenes.
  2. Open an existing automation, or select Create automation > Create new automation.
  3. In the When section, select Add trigger.
  4. Search for and select the Template trigger.
  5. In Value template, enter a template that renders true when the trigger should react.
    • For example: {{ is_state('device_tracker.paulus', 'home') }}
  6. Optional: In For at least, enter how long the template must stay true before the trigger reacts. Instead of a Duration, you can enter a Template.
  7. Select Save.

Options in the UI

Value template (Required)

The template to check. The trigger reacts when the template changes from false to true.

For at least (Optional)

How long the template must stay true before the trigger reacts. Default is 0 hours, 00 minutes, and 00 seconds (reacts immediately).

Using this trigger 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.

When this trigger starts an automation, it passes data about what happened to the automation. You can use this trigger data in the templates of your conditions and actions. To see which data is available, refer to Trigger data in templates.

In YAML, use trigger: template. A basic example looks like this:

TriggerA trigger is the part of an automation that makes the automation start when something specific happens, for example, when the sun sets, a door opens, or a button is pressed. [Learn more]
trigger: template
value_template: "{{ is_state('device_tracker.paulus', 'home') }}"
for: "00:01:00"

This runs when Paulus has been home for 1 minute.

Options in YAML

trigger string Required

The trigger type. For this trigger, use template.

value_template template Required

The template to check. The trigger reacts when the template changes from false to true.

for string | integer | map | template (Optional, default: 00:00:00)

How long the template must stay true before the trigger reacts. Accepts a duration string in HH:MM:SS format or a time period mapping in hours, minutes, and seconds. You can also use a template, which Home Assistant renders when value_template becomes true. For a list of all accepted formats and templates, refer to Durations in automations and scripts.

This example uses a template for for. The trigger reacts when Paulus has been home for as many minutes as input_number.minutes is set to:

TriggerA trigger is the part of an automation that makes the automation start when something specific happens, for example, when the sun sets, a door opens, or a button is pressed. [Learn more]
trigger: template
value_template: "{{ is_state('device_tracker.paulus', 'home') }}"
for:
  minutes: "{{ states('input_number.minutes') | int(0) }}"

Good to know

  • The trigger only reacts when the template changes from false to true. If the template is already true when Home Assistant sets up the trigger, the trigger only reacts after the template has been false first.
  • These results count as true: true, a number other than 0, and the strings yes, on, and enable. Any other result counts as false.
  • Home Assistant renders the template again when one of the entities in the template changes. You can also check an attribute, for example, with {{ is_state_attr('climate.living_room', 'preset_mode', 'away') }}.
  • A template that uses the current time, with now(), utcnow(), today_at, relative_time, time_since, or time_until, is rendered again at the start of every minute. For details, refer to Getting the current time. A template without entities that doesn’t use any of these is only rendered once, when Home Assistant sets up the trigger.
  • If you use For at least (for), the template must stay true for the entire time. If it becomes false before the time is up, the trigger doesn’t react. Use a template that renders true or false: if the result changes while it stays true, for example from 5 to 6, the timer stops, and the trigger only reacts after the template has been false first.
  • If you use For at least (for), the timer resets when Home Assistant restarts or automations reload. If that’s a problem for your automation, you can have the automation set a date and time helper instead, and use that helper in a Time trigger.
  • If the template can’t be rendered, for example, because of an error in the template, the trigger doesn’t react, and Home Assistant logs a warning.
  • For the data that this trigger passes to your conditions and actions, such as trigger.entity_id, refer to Template triggers.

Try it yourself

Ready to test this? Go to Settings > Automations & scenes, create a new automation, and add this trigger. Save the automation, then change the state of the targeted entity to watch the trigger fire on your actual entitiesAn entity represents a sensor, actor, or function in Home Assistant. Entities are used to monitor physical properties or to control other entities. An entity is usually part of a device or a service. [Learn more].

For this trigger, change one of the entities that the template uses, so that the template becomes true.

More examples

Real scenarios where this trigger fires in automations and scripts. Copy any example and adapt it to your setup. In the visual editor, select the UI options listed in the example when they are available, or paste the YAML code.

Tip

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: run the dishwasher on surplus solar power

When the solar panels produce more than 1.5 kW above what your home uses, for 10 minutes, turn on the dishwasher. In this example, both sensors report power in kW. The template only becomes true when both sensors have a value, so the dishwasher doesn’t start when one of the sensors is unavailable.

  • Trigger: Template
    • Value template: The template from the YAML example below
    • For at least: 10 minutes
  • Action: Turn on switch
    • Target: Dishwasher (switch.dishwasher)
YAML example for running the dishwasher on surplus solar power
AutomationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more]
alias: "Dishwasher on surplus solar"
triggers:
  - trigger: template
    value_template: >
      {{
        has_value('sensor.solar_power')
        and has_value('sensor.home_power_use')
        and states('sensor.solar_power') | float
          - states('sensor.home_power_use') | float > 1.5
      }}
    for:
      minutes: 10
actions:
  - action: switch.turn_on
    target:
      entity_id: switch.dishwasher

Automation: send a notification when the washing machine is done

When the power use of the washing machine stays below 5 W, but not at 0 W, for 3 minutes, send a notification to your phone. Waiting 3 minutes prevents a notification during short low-power moments in the middle of a cycle, such as when the machine starts or pauses to soak.

  • Trigger: Template
    • Value template: The template from the YAML example below
    • For at least: 3 minutes
  • Action: Send a notification message
    • Target: My Device (notify.my_device)
    • Message: The washing machine is done.
YAML example for sending a notification when the washing machine is done
AutomationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more]
alias: "Washing machine done"
triggers:
  - trigger: template
    value_template: >
      {{
        states('sensor.washing_machine_power') | float(0) < 5
        and states('sensor.washing_machine_power') | float(0) > 0
      }}
    for:
      minutes: 3
actions:
  - action: notify.send_message
    target:
      entity_id: notify.my_device
    data:
      message: "The washing machine is done."

Still stuck?

The Home Assistant community is quick to help: join Discord for real-time chat, post on the community forum with the trigger you’re using and what you expected to happen, or share on our subreddit /r/homeassistant.

Tip

AI assistants like ChatGPT or Claude can also explain triggers or suggest the right one when you describe what you want in plain language.

Related triggers

Explore related triggers: