Automation

Home Assistant Template Sensor: Beginner Guide with Examples

On this page
    TL;DR: A Home Assistant template sensor is a sensor you make yourself. Its value comes from a short template (a formula in {{ }}) that reads other entities, so you can add up power readings, count open doors or turn a temperature into plain text. Create one in the UI under Settings > Devices & services > Helpers > Create helper > Template > Sensor, or in YAML under template: with - sensor:.

    Updated November 2026 for Home Assistant 2026.10 and 2026.11. All YAML uses the current template: format. The old platform: template style is no longer supported.

    A Home Assistant template sensor lets you create a new sensor from data you already have. Your devices give you raw numbers: watts from each smart plug, on or off from each door sensor. A template sensor combines or reshapes those numbers into something more useful, like “total power in the house” or “2 doors open”.

    This guide is for beginners. We explain what a template sensor is, how to create one in the UI and in YAML, five practical examples you can copy, and the mistakes that trip most people up.

    What is a template sensor?

    A few terms first, in plain words:

    • Entity: one thing Home Assistant tracks, like sensor.fridge_power or binary_sensor.front_door.
    • State: the current value of an entity, like 42.5 or on. States are always stored as text.
    • Template: a small formula written in Jinja, a simple template language. Anything inside {{ }} is calculated, and anything inside {% %} is logic like if and else.
    • Helper: an entity you create yourself in the UI instead of getting it from a device.

    A template sensor is a helper whose state is the result of a template. Every time one of the entities in the template changes, Home Assistant recalculates it. You can show it on a dashboard, graph it, and use it in automations, just like a sensor from a real device.

    Template sensors are read only. They show a value, they do not control anything. If you want something you can switch on and off, look at a virtual switch or a template switch instead.

    AliExpress deals
    What you needZigbee smart plug with energy monitoringLocal Zigbee plug that reports watts and kWh, and doubles as a mesh repeater.Check price on AliExpressAqara Door and Window Sensor (Zigbee)Tiny contact sensor for alarms, "door left open" alerts and HVAC rules.Check price on AliExpressShelly Pro 3EM / EM energy meterWhole-house energy monitoring with CT clamps, local, and made for the Energy dashboard.Check price on AliExpress

    Test your template first

    Before you create anything, try the template in the template editor. Go to Settings > Tools > Template (this used to be called Developer tools). Type a template on the left and the result appears on the right as you type. For example:

    {{ states('sensor.fridge_power') }}

    If you see a number, the entity ID is right. If you see unknown, check the spelling of the entity ID under Settings > Devices & services > Entities.

    How to create a template sensor in the UI

    The UI is the easiest way for most people, and the result is the same as YAML.

    1. Go to Settings > Devices & services and open the Helpers tab.
    2. Select Create helper and pick Template.
    3. Choose Sensor from the list of template types (older versions call it “Template a sensor”).
    4. Enter a Name, for example “Total power”.
    5. Paste your template into the State field. A live preview below the form shows the result.
    6. Fill in Unit of measurement, Device class and State class if the value is a number (see the next section). You can also attach it to a Device, and set an Availability template under additional options.
    7. Select Submit. Your new entity is ready, for example sensor.total_power.

    To change it later, open the helper from the Helpers list and select the settings icon. UI template sensors are saved instantly, no restart needed.

    The fields that matter

    • State (state): the template. This is the only required part.
    • Unit of measurement (unit_of_measurement): for example W, kWh, °C or %. Setting a unit tells Home Assistant this is a number, so it draws a line graph instead of a colored bar.
    • Device class (device_class): what kind of value it is, like power, energy, temperature or timestamp. It sets the icon and formatting. The unit must be one that fits the device class.
    • State class (state_class): measurement for values that go up and down (power, temperature), total_increasing for meters that only count up. It turns on long-term statistics, which the energy dashboard needs.
    • Availability (availability): a template that returns true or false. When it is false, the sensor shows as unavailable instead of showing a wrong value.

    The YAML version

    YAML is the plain text format Home Assistant uses for its settings. If you like keeping things in files, add template sensors to configuration.yaml:

    template:
      - sensor:
          - name: "Living room power kW"
            unique_id: living_room_power_kw
            unit_of_measurement: "kW"
            device_class: power
            state_class: measurement
            state: "{{ (states('sensor.living_room_power') | float(0) / 1000) | round(2) }}"
            availability: "{{ has_value('sensor.living_room_power') }}"

    Notes on the format:

    • template: appears only once in the file. Put all your template sensors under it.
    • unique_id lets you rename the sensor and change its icon in the UI later. Always add one.
    • After editing, go to Settings > Tools > YAML, select Check configuration, then reload Template entities. No full restart needed.

    If you still have the old style, sensor: with - platform: template, move it to the format above. Current versions no longer load it, and Home Assistant shows a repair message under Settings when it finds it. The official template integration docs list every option.

    5 practical template sensor examples

    Replace the entity IDs with your own. Each example works in YAML, and the part after state: also works in the State field of the UI helper.

    1. Total power from several smart plugs

    template:
      - sensor:
          - name: "Total plug power"
            unique_id: total_plug_power
            unit_of_measurement: "W"
            device_class: power
            state_class: measurement
            state: >
              {{ states('sensor.fridge_power') | float(0)
                 + states('sensor.tv_power') | float(0)
                 + states('sensor.washing_machine_power') | float(0) }}

    float(0) turns the text state into a number, and uses 0 if a plug is offline. That keeps the total working when one plug drops off the network.

    2. Count how many doors are open

    template:
      - sensor:
          - name: "Doors open"
            unique_id: doors_open
            state: >
              {{ ['binary_sensor.front_door',
                  'binary_sensor.back_door',
                  'binary_sensor.garage_door']
                 | select('is_state', 'on') | list | count }}
            attributes:
              which: >
                {{ ['binary_sensor.front_door',
                    'binary_sensor.back_door',
                    'binary_sensor.garage_door']
                   | select('is_state', 'on')
                   | map('state_attr', 'friendly_name') | list | join(', ') }}

    The state is a number (0, 1, 2…). The which attribute (extra information stored on the entity) lists the names of the open doors, which is handy in a notification like “Still open: Back door”. For door and window sensors, on means open.

    3. Feels-like text from the temperature

    template:
      - sensor:
          - name: "Outside feels"
            unique_id: outside_feels
            availability: "{{ has_value('sensor.outdoor_temperature') }}"
            state: >
              {% set t = states('sensor.outdoor_temperature') | float %}
              {% if t < 5 %}Cold, wear a coat
              {% elif t < 15 %}Cool, bring a jacket
              {% elif t < 25 %}Comfortable
              {% else %}Hot, stay in the shade
              {% endif %}

    This one returns text, so it has no unit. The thresholds are in °C. Change them if your sensor reports °F. The availability line stops the template from running when the temperature sensor is offline.

    4. Days until bin day

    template:
      - sensor:
          - name: "Days until bin day"
            unique_id: days_until_bin_day
            unit_of_measurement: "days"
            state: "{{ (1 - now().weekday()) % 7 }}"

    now().weekday() gives 0 for Monday up to 6 for Sunday. With 1 for Tuesday, the sensor shows 0 on Tuesday, 6 on Wednesday, and so on. Use 0 for Monday, 2 for Wednesday, and so on. Templates that use now() update once a minute. Pair it with a reminder automation, like the ones in our schedules and reminders guide, that fires when the value is 1.

    5. How many lights are on

    template:
      - sensor:
          - name: "Lights on"
            unique_id: lights_on
            state: "{{ states.light | selectattr('state', 'eq', 'on') | list | count }}"

    states.light is every light entity in your home. This is a nice number to show at the top of a dashboard, next to a clock card.

    Common mistakes

    Using states.x.state instead of states(‘x’)

    {{ states('sensor.fridge_power') }} is the safe way to read a state. If the entity does not exist yet (for example during startup), it returns unknown. The other form, {{ states.sensor.fridge_power.state }}, causes an error in that case. Use states() for states and state_attr() for attributes.

    Forgetting that states are text

    Every state is text, even "42.5". You cannot add text, so convert it first with | float (decimal numbers) or | int (whole numbers).

    No default on float or int

    When a device is offline its state is unavailable, and 'unavailable' | float fails with an error that says no default was specified. Give it a fallback: | float(0). Or better, add an availability template with has_value(), so the sensor shows unavailable instead of a fake 0. A fake 0 can mess up graphs and statistics.

    A unit on a sensor that returns text

    If you set a unit of measurement or a state class, the template must return a number. Returning text like unknown or “Comfortable” gives an error. Leave the unit empty for text sensors like example 3.

    unknown versus unavailable

    unknown means the entity exists but has no value right now. unavailable means Home Assistant cannot reach it. has_value('sensor.x') returns false for both, so it is the easiest check.

    Too many template sensors that never change

    Every sensor writes its changes to the database. A few dozen is fine. If you build hundreds, read our guide on database data retention to keep the database small.

    Frequently Asked Questions

    What is a template sensor in Home Assistant?

    It is a sensor you create yourself whose value comes from a template, a short formula that reads other entities. You can use it to add, convert, count or describe data you already have, then show it on a dashboard or use it in automations.

    Where do I create a template sensor in the UI?

    Go to Settings, Devices and services, Helpers, select Create helper, pick Template, then Sensor. Enter a name and a state template, check the live preview, add a unit if it is a number, and select Submit.

    Do I need to restart Home Assistant after adding a template sensor?

    No. Sensors made in the UI are active right away. For YAML, check the configuration and reload Template entities from Settings, Tools, YAML.

    Why does my template sensor show unavailable or an error?

    Usually a source entity is offline or the entity ID is misspelled. Test the template under Settings, Tools, Template. Add a default like float(0) or an availability template with has_value(), and make sure text results do not have a unit of measurement.

    Leave a comment

    Your email address will not be published. Required fields are marked *

    This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

    Coming soon
    The ready-made Home Assistant dashboard
    One email when it's ready, with an early-bird price. No spam. Privacy