Home Assistant Templating for Beginners -Part 1

Templates are a powerful feature in home assistant that allows you to control information coming into or going out of home assistant.

It is used for:

  • Formatting outgoing messages in, for example, the notify platforms .
  • Process incoming data from sources that provide raw data, like MQTT, rest sensors or the command_line sensor.
  • Automation Templating.

Home assistant uses the Jinja2 templating engine so you need to be familiar with this before you start.

There is is a very good basic tutorial here, but we will cover the basics in the tutorial as we create the code.

For the purpose of this tutorial we will use templating to create a notification when a motion detector detects motion and create an automation that switches on a fan when the temperature goes above a certain temperature.

The actual code for the template is placed in the yaml file but there is a frontend template editor that lets you create and debug the code.

The screen shot below shows the template editor


To use it enter the code in the template editor pane and the results are shown on the right in the results pane.

Entity and States

Home assistant stores entity data in a state table. An entity has a state and also optional attributes.

Below is a screen shot of the entity sensor.button_battery (from developer states tab). The state is 100 and the attributes are listed along with their values.



The state table is actually a database table and template extensions provide access to the state table from templates.

Template extensions provide several functions to query the state table, they are:

  • states()
  • is_state()
  • state_attr()
  • is_state_attr()

To get the state of sensor.button_battery in a template we can use :

{{states(‘sensor.button_battery’)}}  //preferred states is a function

or an alternative  form is


The opening and closing brackets {{..}} enclose an expression that is to be printed to the output.The screen shot below shows other delimiters.


When using the states as a function notice that the entity is in quotes.

We can access the attribute of the entity using state_attr() function.

So to access the voltage we use :


and the link quality:


notice we use two parameters in the function call. The entity name and the attribute name.

If we need to check an attribute for a particular value we can use the is_state_attr() function as shown:


This should produce false as the voltage is 3000 . Alternatively we can use


which  will produce true.

We can show measurement units of a state in several ways:




Self Worked Example:

Below is a screen shot showing the attributes for the entity temperature_battery.

Answer the following questions.


Create the commands to display:

Q1- The entity state?

A1- {{states(‘sensor.temperature_battery’)}}

Q2-The temperature  value?


Q3 –The humidity value?


Q4 –The voltage value?


Q5 – The sate with units

A5 -Below

Q6 – Check if the voltage is <2900

A6- {{state_attr(‘sensor.temperature_battery’,’voltage’)<2900}}

Q7 – Check if the humidity is >50.





A5- {{states(‘sensor.temperature_battery’,with_unit=True)}}

A7- {{state_attr(‘sensor.temperature_battery’,’humidity’)>50}}

Part 2 -Automations

In part 2 we look at using templates to create automations.

Related Tutorials and Resources


Please Let me Know if you found it Useful
[Total: 0 Average: 0]


    1. Graeme
      They go in the configuration.yaml file.
      You don’t name them as such as they are used for automations/notifications.

  1. I think there is a mistake in your text:
    “This should produce false as the voltage is 3000 . Alternatively we can use
    which again will produce false.”

    It will produce true if the voltage is 3000 (greater than 2000)

Leave a Reply

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