In this video I'm going to show you templates in Home Assistant - what they
are and how to use them. Make sure you stick around, and if you like what you
see make sure you hit subscribe below so you don't miss out on future videos.
Hi I'm Will from Will Surridge Tech, and today we're going to be looking at
templates. We'll look at how to implement them into Home Assistant, and how to use
them to improve your setup. So let's get going! So what is a template? Well you can
think of a template as a way of extracting information, manipulating it,
and then spitting it out as something else. It's a bit like a formula in Excel,
or a simple program that you might write to do something for you. You can get all
sorts of different templates, from template senses, to template switches, to
even template vacuums. So there are two main types of templates - you will either
have input templates, or output templates. An input template is like a sensor or a
binary sensor, but you've got the information to fill that sensor from a
different entity entirely, and you might have had to manipulate it to get it into
what you want. Whereas an output template uses lots of different entities
to create one overarching entity. For example you might have a fan that has
three different settings and so three smart switches to control it, with a
template entity you can combine those three smart switches into one fan entity.
So what might you want to use an input template for? Well I use a lot of mine to
extract information from one entity and spit it out in another. This makes it
easier to use it automations and to display in Lovelace. Let's have a look at
my climate as an example. I have a separate climate for each of my rooms. In
my case these climates are created using Honeywell's
EvoHome thermostatic radiator valves. Inside this climate entity is a lot of
information, some of it's quite useful, and some of it's not useful! I don't
really care about the setpoint - these are the schedules that I've created in the
Honeywell app as I don't use them. What I do care about is the heating mode and
the temperature of each room, so I'm using a template sensor to extract that
information and create its own sensor for it.
So to integrate this, we had the sensor to our configuration file, or in my case
the sensors file because I've got it referenced elsewhere. We add a new
platform, which is the template platform, and then we can have a list of all
template sensors. So for this example we'll have the bedroom temperature, we
give it a name ie bedroom temperature, a friendly name - bedroom temperature, and
then the value template is what the value of the sensor will actually be
recalling. So in my case is taking a state attribute from our climate bedroom
and is taking the current temperature attribute. The same applies to my heating
mode or heating status. It's the same principle, we name it, give it a friendly
name, but for the value template instead of grabbing the current temperature is
grabbing the HVAC mode. I also have some template sensors for the sunlight. This
affects how my floorplan is displayed. I'm so glad I could just copy these
because I really don't understand how they work themselves - I wouldn't have
been able to do it myself so shout out to Luke Vink on that one! If
you haven't already, make sure you check out my video on creating an awesome
interactive floor plan. As for actually writing the template, it works on
jinja2 - and their syntax. It's really well documented on the jinja website,
and also on the Home Assistant website. There are plenty of examples on the Home
Assistant website, and also on the Home Assistant forums to get you started and
give you some idea as to what you can do. If you head over to the developer tools
tab, there's a template section where you can test out your templates and make
sure it's spitting out what you're expecting it to. Another time you might
want to use an input sensor is if you have continuous data and you want to
categorise it and make it discreet. For example if you have a clamp sensor on
your TV this is detecting the power of the TV, you might want to categorise it.
So for example if it's 0 watts then the TV is off, if it's less than 10 watts
then the TVs in standby, then if it's anything else in the TV's on. So to add this we
add a sensor, on the platform template, give it a name and then the value
template here is going to be an if statement. So if the value of our sensor
is less than 2 watts then it's going to be 'off', we're using 2 because it gives us
a bit of wiggle room. If it's not that value then it will move on to the next
statement, in this case less than 10. If it's less than 10 then it will spit out
'standby'. Otherwise if it fails those two
conditions it'll move on to the else. Else it'll be 'on'. And that's it. You can
also use templates to combine senses, so if you have a device tracker on two
separate people in the household you might want another sensor that tells you
if someone is in the household, so to do that you just see if one or the other
are active, ie in the house, and then you can set your input sensor to on, or off
if nobody's in the house. Of course you could also use an automation and input
boolean to do this but this is just another alternative. You can also use
templates to change the icon or other attributes of your sensor - personally I
prefer doing this in lovelace using a custom button card, but if you want it to
affect the entity as a whole throughout your environment then you can use a
template instead. As for the output template it's the same principle we're
just merging multiple entities into one. You can create a template cover, a
template switch, a template light, a template fan, or even a template alarm
panel. So for an example for this with your garage door. Let's say our garage
door has a reed switch sensor on it to tell if the door is open or closed.
It then has one switch to shut it, and another switch to open it. So to do this
we're going to add a lock to our configuration file using the platform as
a template. Our value template is going to be getting the information from our
reed switch sensor. Then to lock it we're going to turn on the switch that
closes the door, and to unlock it we're going to turn on the switch that opens
the door. It's as simple as that. And that's all there is to it -
templates are there to extract or manipulate information. The more you use
them, the more you'll get comfortable with the syntax, but as I said it's
really well documented and there's loads of support out there if you get stuck.
I hope you're enjoying my videos. If you haven't already, make sure you follow my
social media they're linked in the description below - both my Instagram and
my facebook page. If you have any questions or any problems feel free to
message me and I'll do my best to help out. Also let me know if there's any
content you'd like me to cover in my videos in
the future. I hope you have a better understanding of what our template is
and how you can use it to your benefit. Make sure you hit subscribe below so you
don't miss out on future videos about my smart tech and how you can build yourself
the ultimate smart home!