Evaluate Template
ha_eval_templateDebug and test Jinja2 template expressions for Home Assistant automations with real-time evaluation using current states and functions.
Instructions
Evaluate Jinja2 templates using Home Assistant's template engine.
This tool allows testing and debugging of Jinja2 template expressions that are commonly used in Home Assistant automations, scripts, and configurations. It provides real-time evaluation with access to all Home Assistant states, functions, and template variables.
When NOT to use this for automation/script logic:
Templates have legitimate uses (notification bodies, dynamic data.* values,
debugging existing templates), but condition: / trigger: positions and
action service names are better expressed as native HA constructs:
native constructs are schema-validated at config load and surface
structural errors loudly, whereas equivalent template logic only errors
at runtime — and a template that renders a non-truthy value is silently
treated as false.
Prefer:
condition: numeric_stateover{{ states('x') | float > N }}condition: stateover{{ is_state(...) }}condition: time/condition: sunovernow().hour/is_state('sun.sun', ...)Native
for:field on state/numeric_state triggers and state conditions over{{ now() - X.last_changed > timedelta(...) }}duration mathchooseaction over templatedservice:/action:strings Seeha_get_skill_guide(best-practices skill) for the full anti-pattern list.
When to use (reach for this tool, don't compute it yourself):
Any one-shot question whose answer is DERIVED from current HA state — an
average/sum/min/max across sensors, a count of entities matching a
condition, a boolean comparison, or a rendered message with live values.
One render call beats fetching N states and doing the math yourself, and
it is the canonical way to test a template before embedding it. This is
for one-shot answers and template testing only — NOT for putting templates
into automation logic; for condition: / trigger: positions native
constructs win.
"average temperature across the bedroom sensors" ->
{{ ([states('sensor.a'), states('sensor.b')] | map('float', 0) | sum) / 2 }}"how many lights are on" ->
{{ states.light | selectattr('state', 'eq', 'on') | list | count }}NOT for a plain single-entity value ("what's the state of X") — that isha_get_state/ha_search; rendering{{ states('X') }}there is over-use.
Parameters:
template: The Jinja2 template string to evaluate
timeout: Maximum evaluation time in seconds (default: 3)
report_errors: Whether to return detailed error information (default: True)
Common Template Functions:
State Access:
{{ states('sensor.temperature') }} # Get entity state value
{{ states.sensor.temperature.state }} # Alternative syntax
{{ state_attr('light.bedroom', 'brightness') }} # Get entity attribute
{{ is_state('light.living_room', 'on') }} # Check if entity has specific stateNumeric Operations:
{{ states('sensor.temperature') | float(0) }} # Convert to float with default
{{ states('sensor.humidity') | int(0) }} # Convert to integer with default
{{ (states('sensor.temp') | float(0) + 5) | round(1) }} # Math operationsTime and Date:
{{ now() }} # Current datetime
{{ now().strftime('%H:%M:%S') }} # Format current time
{{ as_timestamp(now()) }} # Convert to Unix timestamp
{{ now().hour }} # Current hour (0-23)
{{ now().weekday() }} # Day of week (0=Monday)Conditional Logic (for display strings — not for condition: positions):
{{ 'Day' if now().hour < 18 else 'Night' }} # Ternary operator
{% if is_state('alarm_control_panel.home', 'armed_away') %}
Alarm is armed
{% else %}
Alarm is disarmed
{% endif %}Lists and Loops:
{% for entity in states.light %}
{{ entity.entity_id }}: {{ entity.state }}
{% endfor %}
{{ states.light | selectattr('state', 'eq', 'on') | list | count }} # Count on lightsString Operations:
{{ states('sensor.weather') | title }} # Title case
{{ 'Hello ' + states('input_text.name') }} # String concatenation
{{ states('sensor.data') | regex_replace('pattern', 'replacement') }}Device and Area Functions:
{{ device_entities('device_id_here') }} # Get entities for device
{{ area_entities('living_room') }} # Get entities in area
{{ device_id('light.bedroom') }} # Get device ID for entityCommon Use Cases (legitimate template positions):
Dynamic Service Data:
# Dynamic brightness based on time
{{ 255 if now().hour < 22 else 50 }}
# Message with current values
"Temperature is {{ states('sensor.temp') }}°C, humidity {{ states('sensor.humidity') }}%"Examples:
Test basic state access:
ha_eval_template("{{ states('light.living_room') }}")Test a string expression (e.g. for a notification body):
ha_eval_template("{{ 'Day' if now().hour < 18 else 'Night' }}")Test mathematical operations:
ha_eval_template("{{ (states('sensor.temperature') | float(0) + 5) | round(1) }}")Test entity counting:
ha_eval_template("{{ states.light | selectattr('state', 'eq', 'on') | list | count }}")IMPORTANT NOTES:
Templates have access to all current Home Assistant states and attributes
Use this tool to test templates before using them in automations or scripts
Template evaluation respects Home Assistant's security model and timeouts
Complex templates may affect Home Assistant performance - keep them efficient
Use default values (e.g.,
| float(0)) to handle missing or invalid states
For template documentation: https://www.home-assistant.io/docs/configuration/templating/
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| timeout | No | ||
| template | Yes | ||
| report_errors | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||