Sooner or later you need a value that no device reports: the average of two room thermometers, a "washing machine running" flag derived from a smart plug's power reading, or a temperature that should keep its last good value when the sensor drops off the network. Home Assistant's Template integration creates such entities from a short Jinja2 expression. You can build most of them in the UI, without touching configuration.yaml.
The part that trips people up isn't the syntax but what happens when a source entity is unknown or unavailable, and when the template actually re-evaluates. This guide covers both.
State-based and trigger-based templates
Home Assistant has two kinds of template entities.
- State-based entities re-render whenever an entity referenced in the template changes. If a template averages two sensors, it updates as soon as either source does. These are the ones you can create in the UI.
- Trigger-based entities update only when one of their triggers fires. Triggers work exactly like automation triggers: a time pattern, a state change, a webhook, an event. Referenced entities don't cause updates on their own. Trigger-based entities exist only in YAML, and their state, including attributes, is restored after a restart (except buttons, images and update entities).
Start with a state-based helper. Switch to a trigger-based entity when you need control over timing or want to keep a value across restarts.
Test the template before you create anything
Open Settings > Tools > Template. The editor renders the template as you type, shows errors above the result, displays the result type and lists which entities the template listens to. That last line answers most "why doesn't it update?" questions before you ask them.
Two facts from the documentation are worth remembering while you test:
- Entity states are always text, even when they look like numbers.
{{ states('sensor.outdoor_temperature') | typeof }}returnsstr. Convert withfloatorintbefore doing math or comparing values. Compared as text,'6' < '10'is false. - A template that references no entity and no time function (such as
now()) runs once and never again. One that usesnow()re-renders at the start of every minute.
Create a template sensor in the UI
As an example, take the average of a bedroom and a kitchen thermometer.
-
Go to Settings > Devices & services > Helpers and select Create helper.
-
Search for Template and pick Sensor.
-
Enter a Name, for example
Average temperature. -
Paste the template into State:
{% set bedroom = states('sensor.bedroom_temperature') | float %} {% set kitchen = states('sensor.kitchen_temperature') | float %} {{ ((bedroom + kitchen) / 2) | round(1) }} -
Set Unit of measurement to
°C, Device class to temperature and State class to measurement. The state class is what makes Home Assistant keep long-term statistics for the sensor, so statistics cards and long history graphs work. -
Optionally pick a Device to attach the helper to, so it shows up next to related entities.
-
Under Additional options, fill in Availability (see the next section), then select Submit.
You can change any of this later: open the helper in Settings > Devices & services > Helpers and select Settings.
The UI offers a subset of what YAML can do, but it already covers 20 entity types, from sensors and binary sensors to switches, lights, locks, climate entities and weather. For most derived values that's enough.
Unavailable sources: availability instead of float(0)
When a thermometer's battery dies, states('sensor.kitchen_temperature') returns unavailable. A plain | float without a default can't convert that, raises an error and the template fails. The usual advice, repeated in the documentation, is | float(0). It does make the error go away, but your "average" then becomes half the bedroom temperature, and that number lands in your long-term statistics.
The documentation has stricter rules for numeric sensors. If a sensor has a unit of measurement or a state class, its state template must render a number or none, never a string such as unknown or unavailable. The intended tool is the availability template. When it renders false, the entity becomes unavailable and the state template isn't rendered at all, so the plain | float in it never sees a bad value. For the average:
{{ has_value('sensor.bedroom_temperature') and has_value('sensor.kitchen_temperature') }}
has_value returns false when the entity doesn't exist or its state is unknown or unavailable. If a source may report something non-numeric, is_number(states('sensor.x')) is the stricter check, and it's the one the official unit-conversion example uses.
Keep float(0) for cases where zero is actually the correct fallback, such as power from a plug that is switched off.
One more startup detail: write states('sensor.moon') rather than states.sensor.moon.state. While the entity doesn't exist yet, the first form returns unknown and the second raises an error, which can leave the template sensor unknown. The same applies to is_state('switch.source', 'on') versus comparing states.switch.source.state.
Binary sensors with a delay: the washing machine example
A washing machine's power reading jumps around and often falls to zero mid-cycle, so a simple "power above zero" check flickers between on and off. The template binary sensor has Delay on and Delay off options (in the UI under Additional options) for exactly this.
Create a helper of type Template > Binary sensor, set State to:
{{ states('sensor.washing_machine_power') | float(0) > 0 }}
and set Delay off to 5 minutes. The sensor turns off only after five minutes without any activity, and an automation can send a "laundry finished" notification on that transition. Here float(0) is reasonable: a missing reading counts as "not running", and the delay hides short gaps.
The same pattern works for a dishwasher or a tumble dryer. Only the threshold and the delay change, and both are easier to tune with real data from the plug's history graph.
When YAML is worth it
Some things are available only in YAML under the template: key.
Keep the last valid value. A trigger-based sensor with a condition updates only when the source has a number and otherwise keeps its state:
template:
- triggers:
- trigger: state
entity_id: sensor.outside_temperature
conditions:
- condition: template
value_template: "{{ is_number(states('sensor.outside_temperature')) }}"
sensor:
- name: Outside temperature last known value
unique_id: outside_temperature_last_known
unit_of_measurement: "°C"
device_class: temperature
state: "{{ states('sensor.outside_temperature') }}"
Because it's trigger-based, the value also survives a restart.
Control the update schedule. A time_pattern trigger can recalculate a value once a night instead of on every state change.
Store webhook data. A webhook trigger lets an external script or device post JSON straight into template sensors, with values available as trigger.json.
Avoid rate limits. A template that loops over every state in the system is limited to one update per minute, and one that loops over a whole domain such as states.sensor to one per second. Templates that reference only specific entities have no limit. Defining a trigger gives you full control.
Give YAML entities a unique_id. Without it you can't change the name, icon or entity ID from the UI. After editing, reload template entities in Settings > Tools > YAML instead of restarting Home Assistant.
If you still have an old configuration with platform: template under sensor:, it is no longer supported. Home Assistant shows a repair for it, and the entities have to move to the template: format.
Mistakes that keep coming back
- A trigger on the entity itself. A trigger-based entity that triggers on its own state, or on an entity calculated from it, can loop endlessly and make Home Assistant unresponsive.
- Testing
triggerorthisin the editor. These variables don't exist there. Define them at the top with{% set %}while testing, then remove those lines. - Expecting
for:to survive a restart. In a template trigger,for:resets when Home Assistant restarts or automations reload. To keep a time target, store it in aninput_datetimehelper. - Comparing text with numbers. Convert both sides with
floatorintfirst. - Python habits. Templates are Jinja2: there's no
print(), no f-strings and nowhileloops, and tests are lowercase (is none, notis None).