Using Templates in Automations
Templates are a powerful feature in Home Assistant automations, letting you use logic and dynamic values to make your automations more flexible and intelligent. If you want your automation to do different things depending on time, sensor readings, or even text - templates are the key!
What is a Template?
A template in Home Assistant is a piece of code that gets turned into a value when your automation runs. Home Assistant uses a language called Jinja2 for its templates, which is a simple yet powerful templating language. Think of it as a way to "calculate" or "look up" the right value for your automation, on the fly. For example, templates could be used to:
- Say "Good morning" or "Good evening" depending on the time.
- Use the actual temperature from a sensor in your message.
Where Can You Use Templates?
Templates can be used in many places throughout Home Assistant automations and scripts whenever you want a value to be calculated or chosen automatically, instead of entering a fixed value. Some of the most common places you'll use templates are:
- Action fields: For example, setting the text of a notification to include the current temperature, or adjusting the brightness of a light based on the time of day.
- Conditions: To make decisions based on the state or value of a sensor, such as turning on a fan only if the temperature is above a certain level.
- Entity IDs or targets: To choose which device or entity to control based on logic, like turning on different lights depending on where motion is detected.
Usually, you can use a template in any field where you would normally enter a value - such as a message, number, or entity ID. If a template isn't allowed in a specific field, Home Assistant will show an error when you try to save or run the automation.
Template Syntax: The Basics
Home Assistant uses Jinja2 templates, which have three kinds of special "curly braces":
-
– For outputting a value or result.
Example:{{ states('sensor.temperature') }} -
– For logic or control statements like iforforloops.
Example:{% if ... %} ... {% endif %} {# ... #}– For comments. Home Assistant ignores anything inside these.
Tip: Use {{ ... }} for anything that should show up as a value (like in notifications or conditions). Use {% ... %} for things like if statements, and only inside larger template blocks (not on their own).
Common Mistake: Don't put an if statement or loop inside {{ ... }} - it won't work! Only use {{ ... }} for simple values or math.
Real-World Examples
1. Send a Notification with the Current Temperature
action:
- service: notify.mobile_app_yourphone
data:
message: "The current temperature is {{ states('sensor.living_room_temp') }}°C."
Explanation: states('sensor.living_room_temp') gets the current value of the sensor. Whatever that sensor reads, the message will include it.
2. Turn On a Fan If It's Too Warm
condition:
- condition: template
value_template: "{{ states('sensor.living_room_temp') | float > 22 }}"
Explanation: This checks if the living room temperature is above 22°C. The | float turns the value into a number so it can be compared.
3. Greet Based on Time of Day
action:
- service: notify.mobile_app_yourphone
data:
message: >
{% if now().hour < 12 %}
Good morning!
{% elif now().hour < 18 %}
Good afternoon!
{% else %}
Good evening!
{% endif %} Explanation: This uses an if-elif-else block to choose the right greeting based on the current hour.
4. Choose Which Light to Turn On Based on a Sensor
action:
- service: light.turn_on
target:
entity_id: >
{% if is_state('binary_sensor.motion_kitchen', 'on') %}
light.kitchen
{% else %}
light.hallway
{% endif %} Explanation: If motion is detected in the kitchen, turn on the kitchen light. Otherwise, turn on the hallway light.
Common Jinja2 Functions and Filters
states('entity_id')- Get the value of a sensor or entity as a stringis_state('entity_id', 'value')- Check if an entity matches a specific valuenow()- Get the current date and time| intor| float- Turn a string into a number| round(1)- Round a number (e.g. to 1 decimal place)| lower- Make text lowercase
Tips for Beginners
- Try out your templates in Developer Tools > Template before using them in automations. It's a safe way to test!
- Use out Template Tool to help you write and validate templates. It features syntax highlighting, autocomplete suggestions, cheat-sheets & snippets, and beginner-friendly explanations for many common errors/warnings.
- If you're stuck, check the Home Assistant documentation or community forums - there are tons of examples to learn from.
- Remember, YAML is picky about indentation - copy carefully!
Summary
Templates unlock a new level of flexibility in your Home Assistant automations, allowing you to respond to real-world data in smart ways. With just a few lines of Jinja2 code, you can make your smart home react exactly how you want - no coding background required!