I need to understand - 1 - Templates

I have 274 Automations and use Scripts. I also imported several Blueprints.
However, I do not understand Templates. What is the difference with Scripts and can I get examples of how/when to use Templates ?

This is tricky to explain.

Templating is a bit like using variables to creates new sensors, show things in the UI and create some logic

I saw that there are Youtube videos on Templates. Iā€™ll watch those.
Thanks

Also have a read through the link below and Google ā€œhomeassistant templatesā€ and check out some examples. Iā€™m happy to post some here to give you some food for thought.

1 Like

The first sentence in the link provided by @jchh explains it pretty well

The template integration allows creating entities which derive their values from other data.

The name might be a bit technical and you could alternatively call them ā€œcustom sensorsā€ or ā€œhelper entitiesā€. Template entities are simply speaking sensors (or more specifically entities) that you can freely define to represent a specific information you want to see/use in HA.

An easy example I have in my setup is for example the energy consumption of a self-controlled water pump. If energy is consumed, the pump must be active, if no energy is consumed, the pump is inactive. A simple binary sensor I can show in the UI or use in automations.

2 Likes

Thanks to both. I have some homework now :slight_smile:

How are Scripts Different than Automations?

Scripts are essentially the same as the Action/ā€œThen Doā€ block in an automation. Instead of having a defined trigger, scripts are made to run by using a service call. Those service calls can be made by automations, Dashboard card actions, or other scripts. Scripts make it easy to reuse action sequences. They can make code tweaking/maintenance easier because you have a single place to make changes.

What are Templates?

If you have looked at any Home Assistant YAML configuration, like that found in automations, you may have noticed that they are mostly composed of key/value pairs like:

entity_id: switch.example

where entity_id is the key and switch.example is the value.

At the most basic level, the Jinja Templates in Home Assistant are a way to fill values. Templates allow us to retrieve data like the state of entities and process that data using logic, math, and other programming tools to make those values dynamic.

Where Should I use Templates?

Use them where they are supported. If you donā€™t know if they are supported in a particular location within your configuration the best thing to do it to check the documentation.
Integrations that are still configured in YAML usually have a section of their documentation labelled ā€œConfiguration Variablesā€ [example]. The section consists of a list of the allowed configuration variable keys used by that integration and information about those variables. If templating is supported it will say ā€œtemplateā€ next to the variableā€™s key. If templates are not supported, it will say the data type that is expected such as string, float, integer, or list.

image

Additionally, the documentation is full of YAML configuration examples. A lot of effort has been made to include template examples where they are supported.

Where can I use Templates in the UI?

When Should I use Templates?

There are thousands of times when you might want to use a template.

  • When you need a value to change based on certain criteria. Example: The brightness a light turns on should change based on the time of day.
  • When you need to modify a value. Example: a sensor that tells you the temperature difference between inside and outside.
  • When you want to make your automation or script more compact. Templates can often replace the logic of complex chains of conditions or a Choose action with a dozen options, in just a few lines.

Templating can be used to solve simple problems like the examples above or perform much more complicated computations.

How do I use Templates?

  1. a. Read the documentation to get an idea of what tools are available:

    b. Read tutorials or watch videos.

  2. Think about a problem that you want to solve.

  3. Try to solve it with templates in the Template Tool. If you get stuck, do a searchā€¦ if you canā€™t find an answer, ask for help.

  4. Implement the solution.

  5. Profitā€¦? :upside_down_face:

5 Likes

Iā€™d be interested in the fact that you have

But have not written a template. You must have run into situations that were like real hard to logic thru without a few curly braces. Like needed a random number or word from a list, or wanted to use the value returned from a trigger to do something in the action later.

Templates are the magic sauce that makes these things happen.

1 Like

I think that you make my pointā€¦ When I understand Templates then I may be able to simplify some of my Automations.
I seem to have a mental block understanding Templates and even some of the expanations given here above donā€™t make it to my brain. When I have some time I will watch some of the Youtube videos on HA Templates.

This should give you some ideas. Templates from basic to exotic from one of the HA Devsā€¦
mysmarthome/jinja_helpers at master Ā· skalavala/mysmarthome Ā· GitHub.

3 Likes

Make a wiki out of this. Itā€™s awesome!
Put all but the first paragraph and plug it in the cookbook under templatesā€¦

I will def point people to it going forward.
In my left column for nowā€¦

image

1 Like

try to ignore that theyā€™re called ā€˜templatesā€™. That threw me off and cost me a couple of days. I understand the history of the naming but itā€™s really not helpful in the Home Assistant context.

1 Like