Home Assistant Templates - A Beginner's Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
apparently i neglected to show you how to actually use the information from the face detection object detection and license plate recognition videos that we've been doing so let's fix that hey guys and welcome back to another tech tutorial tuesday the series where you guys ask questions and i'll do my best to answer them as quickly and efficiently as possible and today we are taking a look at templates it's not that neglected to tell you how to use them in the last couple of videos i definitely thought about it but they are a very complex and complicated topic and so they kind of require their own dedicated video so that's what we're doing today templates what are they what do they do and how do we use them before we get into it if you like this video make sure to drop a like and hit the subscribe button if you aren't already and if you want your question answered in the next tech tutorial tuesday make sure to drop it in the comments down below and you never know i might just answer it okay so templates what are they well templates allow you to do templating thank you so much for watching this video i will see you in the next one templates are kind of a very complex beast but a couple of the main functions that they can do are that they can help you format incoming sensor data so for example if you have some raw data that is just a very jumbled mess and it doesn't make too much sense you can use templates to extract certain bits of information that you want and into their own sensors templates can also create dynamic messages for text speech or for notification messages for example if you wanted to announce the name of a person that just came home you could use a template to do that instead of programming in everyone's names individually they can also be used to create if statements or for loops allowing you to build more complex automations how do templates relate to the previous videos well i mentioned in those videos about extracting information from the entity or the sensor and then using that in your automations but because the information is stored in the attributes rather than the state it's not so easy to get at and that's where templates come in we're going to be working on the data from the number plate recognition video and i'm going to give you three different examples of how you can use templates you can apply anything that i give you here to any of the other previous videos it works the exact same and you can apply it to anything else you want really i'm just using the number plate video as a practical example so that you can see visually what's going on otherwise i'll go off on some tangent and it won't really make sense quick disclaimer i am by no means a template expert and i can certainly hack my way along and get by but yeah definitely not an expert and like many things in home assistant there are many ways to do templates there are shorter written hand ways of some of the templates i'm going to show you i don't think they are visually the easiest to learn if you're a beginner so i'm going to show you the longer ways and then you can go back and do the shorter ways if you want i'm going to show you three examples of templates in home assistant the first one is how to use a template as a trigger within an automation then we're going to do how to use a template as a notification message also in an automation and then finally i'll show you how to use a template in order to create a sensor inside home assistant head over to dev tools and then templates and if you've never used this before then this is an excellent tool that will allow you to quickly test and prototype your templates to make sure that they work as you intended before you use them in your automations or config this saves you having to restart home system each time and you make a template to see if your sensor works and you can just do it here get the results you want and then copy it over to your config okay so i've got two windows open here this one on the left is the developer tool states and the one on the right over here is the developer tools um templates and that looks a little bit like this and i'm going to get rid of the stock information for now if you're not sure how to get to these pages and just come down to developer tools click on states and again developer tools and then template at the top there so when we're working with templates you'll find that there are two different or two most common type of templates you'll see and that when we're working with templates you need to use the curly brackets so they start with a curly bracket and they end with a curly bracket if you have a template that has a double curly bracket a little bit like this that indicates that it is a statement and so that will essentially print out the value or the contents of the template and if we have um a template that starts with a curl bracket and then a percent like this and then ends with a percent and a curly bracket that is the start of a statement so an if statement or a for loop or something like that i know that sounds a little bit confusing but it'll get a bit more apparent as we go through the examples so if i the on the left here i've got um an entity that everyone should have and that is the sun.sun sensor and you'll see that i have a state which is above horizon and then i've got the attributes and it has a ton of values in here remember that an entity can only have a single state it can't have multiple values in there so just one state but it can have multiple attributes as you can see here so i've got a single state and i've got multiple attributes and one of the most simplest templates you can do is to get the state of a sensor so if i do something like this so i've got my double curly brackets which indicates that i'm going to do a statement um and then if i type states open a bracket and then enter the name of my sensor which is sun.sun and then close my brackets i get something that looks like this on the right hand side it says above horizon and that matches the state of my sensor over here hopefully that makes sense so double curly brackets indicates that it's going to print out whatever is inside the um template another one you can do is to get the attributes um so if i change this from states to state underscore attr and then after the entity i need to do a comma and then i enter the name of the attribute that i want to get so i'll just set elevation and that gives me the value of the elevation so 20.72 you can see that that matches the elevation of 20.72 so that is a very basic example now let's move on to a more practical example and that is going to be using the license plate license plate information that we got from the previous videos okay so i've resized my screen a little bit hopefully you guys can see that and then on the left hand side here we have my image processing and the plate recognizer entity and then on the right hand side we have the template editor or tester and the first one i want to do is to get the number of remaining api calls from the image processing sensor and that is this value here this calls remaining so what we want to do is over in the template editor so that um sorry this calls remaining is actually under the statistics and all i'm going to do is start building out my template so i start with the double curly brackets and remember that indicates a print statement and then i use the state underscore attr for attribute because the value we want to get is in the attributes column then i open my um normal brackets and open a quote and then i start entering the name of my sensor so that is the image processing one i'm just going to copy that because it's quite long and then close your quotes and then using a comma we give the name of the attribute that we want to get so we actually want to get the statistics one so enters the statistics close your brackets and then you'll see on the right hand side that gives us all of the entries that are under the statistics attribute so see total calls usage year month resets etc etc and that corresponds to this information on the left hand side here but the one we actually want to get is the calls remaining all of this extra all of this other information is just extra and fluff that we don't need to deal with at the moment so remember if you take a note of this value so calls remaining two four eight five i'm gonna make this screen bigger just so we have a bit more room to work with um and then on so basically i want to filter down this value to get just the two four eight five and we can do that by adding a dot at the end and then using the um attribute that we want to get so calls underscore remaining and that's going to give us a number two four eight five so we're getting a bit closer um but that doesn't really help us out because in order to use a template in a trigger you need to have if a um a value of true or false so we need our um template to give us a true or false value so that we can use that as a trigger so the next thing i'm going to do is to enter um so remember i said that these double brackets indicate a print statement and a bracket with a percentage indicates a expression or a that can be used for like an if statement or a for loop or something like that so i'm going to change those brackets because we're going to do an if statement and so all i need to do is um add at the start at if state a attribute and statistics call remaining and then at the end i'm gonna do a less than two four eight five and you'll see over here is complaining that we haven't added in so what we're gonna do is on the next line we enter true and then on the other line under that we enter brackets percent and then we do else close your brackets and then on the next line enter false and then we end our statement using the and if block and so that is going to return the the false value on the um right hand side here and that's because if you look at it our calls remaining isn't currently under two four eight five if i were to change this to two four eight six that value will now change to true and so that is essentially how to do a basic if statement and using the state attribute so all i need to do now is to copy this and if we head over into our automations and we create a new automation all i need to do now is to change the trigger type to template and then we simply paste in the value of our template that we got from the template tester save that and now your template will execute whenever this sensor gets below a certain value now we're going to jump over and do a um template that will give us a dynamic notification message okay so back over in the template tester the next one i want to do is to get a list of all the number plates that our um image processing sensor has picked up and detected so we want to loop through and list all of these plates that are in here and that could be any number of plates it could be one plate or it could be ten plates and so what i'm gonna do is get rid of this and then we're gonna start off using a expression statement using the bracket percent and then i want to um use a set parameter which is essentially um assigning the value to a variable so if you think about it in terms of if you've ever coded before um it's kind of like setting a variable so we're doing the set command and then we give it a name so we're just going to call this vehicles because i oh the vehicles um equals so i want to get a list of all the vehicles and that is under the vehicle's attribute so i'm going to say set vehicles equals state underscore attr because it's an attribute open my brackets and then i'm going to paste in the value of that sensor again and then add a comma and this time i'm going to get the vehicles attribute so enter that close my brackets no percent and then this time you'll see over on the right hand side um it is a valid template but it's not returning anything and that's because we are using the um percent brackets so what we can do is on the line underneath we can simply use the double brackets and then print out the vehicles variable and so that gives us a list of all the vehicles nicely ordered on the right hand side so that's kind of useful um but it's not really what we want because there's some extra information here i don't really need to know the region code because i set that and i don't really need to know the vehicle type because it doesn't really make any difference to me and whatsoever so now we're going to set a for loop and to do that we just get rid of this and then we're going to set um the bracket percent and then start a four loops and so we say four um vehicle in vehicles and that's gonna start a new for loop um and this vehicle does not matter you could name that whatever you want i can name that um laptops for all it matters it's just a placeholder for a name so on the next line down we'll indent um and then we'll start printing so what i want to do is to get a vehicle um let's just do that for now so we're gonna print vehicle and then we're gonna end our for loop using the brackets percent um and four and you'll see this time it prints out the exact same information as we had before it looks slightly different just because we're using a for loop so that's pretty handy um but what if we want to narrow it down even further so all i really want to do is to get the plate so this time i could um in the vehicle all i need to do is to enter vehicle.plate and that will change all of the values and only return the values of the plate number so that's pretty useful um [Music] if i also want to get the confidence i can then add a second one and do something like vehicle dot confidence and that will then return the plate alongside the um confidence value so that i can use that in my automations and we can add anything we want in here um we could also do something like this we could enter plate and then we could do confidence just to make it look a little bit nicer for our notification because we're going to be using all of this information and putting it into a notification so that we get an instant message and telling us all of the number plates that have been detected for example so that is pretty good and useful however um the only thing that is kind of bugging me is that this confidence level isn't really um i mean it does make sense um because it's 0.9 that's 90 however and we could make it a little bit nicer by changing that into a percent so what we're going to do is to use one of the functions called round and all you need to do is to do the straight line or the pipe symbol and then we enter round and let's do round to two and so that gets rid of all the extra decimal places and then we simply multiply by 100 then that gives us our percent and we can enter a percent symbol right at the end okay so i've made that a little bit bigger for you and then all we need to do is enter the percent symbol at the end and now that is starting to look pretty good so we've got our plates um but it includes the um the plate number it detected and then it also gives us a confidence score of the um the proper percentage so we've we've done some simple math and we've got the exact percentage that it is and so once again all we need to do is then copy this information head over to our automations and we create a new automation um and this time you could use something like the state of the um number plate sensor so plate plate recognizer um and you could chain you could set the state of that from zero but that's not really what we're focusing on just now and then in the actions um you could do something like call service and then do note notify and then in the message you could do something like this and just paste in the templates that we have here make sure to get your indentation correct otherwise it will not work and so there we go that is essentially a basic notification message and that will loop through and give you a notification and it will tell you all of the plates that were recognized right in the notification message now let's go back and do the third example and that is to use a template to create a sensor for our third and final example i want to create a binary sensor that detects whether the number plate is detected or not and you could use that for some sort of presence detection so if that number plate is detected it will change the binary sensor to on or home and if it isn't it will be off or away so i want to get a specific plate or look for a specific plate within this um within these attributes so essentially we're going to combine a for loop and an if loop into one template here so what i do is i'll get rid of that for the moment so what do is get rid of that for the moment so the plate we're going to be looking for is this g66x9ymf so if we remember that and i'm also going to grab the name of this image processing sensor just so i don't have to type it again and we'll make this a little bit bigger just so you guys can see so we're going to start this one same as before by creating a new and variable so we're going to set set vehicle that did the same type before set vehicle equals and then we're going to do state underscore attr open our brackets and enter the name of our sensor and then we will enter the name of the attribute that we're looking for so remember that is under vehicles so the name of the attribute we're looking for is under vehicles once again so enter vehicles and then we will close our brackets and again if we just print out um vehicles vehicles we get a list of all of our vehicles again and we need to narrow that down so we can find it just this specific number plate so what we're going to do is to create a for loop once again so exact same as before we enter bracket percent and we do four vehicle in vehicles close our brackets and then on the next line down and so what we're going to do is we're going to search all of our vehicle plates for the exact specific plate that we need so if i just close my for loop so that i can show you um and then on the next line down remember that in the previous example i used the vehicle.plate and that gave me all of the number plates so if i show you that again print that out why can i not spell today and that gives us all of our number plates but that doesn't really give us the information that we need because there's um other plates in there that we don't necessarily need so what we can then do then we're going to start an if loop to search for the plate that we want so start your if statement with bracket percent if g669 ymf remember that was the number plate that we were looking for in vehicle v vehicle top plate and close our brackets and then on the next line down we do true we do true present else false and then close our if loop as you can see there it gives us false false true and that's because the number plate that we want was the third one in the list so that kind of works um it is useful so we can see that our number plate search is working if i change this to something that doesn't exist we'll get three falses and if i change it back we get our um true but we have extra falses in there which aren't really going to work because it's going to confuse our sensor it's going to add false false true and then the sensor won't work properly so all we actually need to do is just to get rid of the false and the else and that will leave us with a single true and that means we can then use that in our template sensor so now that we have found the exact plate that we want or narrowed it down we can then copy this copy this and then we're going to head over and create a new binary sensor over in our home assistant configuration we're just going to simply create a new binary sensor and so we declare a new binary sensor and then we set the platform platform to be template enter sensors and then we give our sensor a name so i'm going to call this car underscore found we can also give it a friendly name call that car found and because this is going to be used for presence i'm going to give it a device class of presence why can't i spell today um and then all we need to do is to create a value underscore template and then simply paste in the value of our template that we used in the tester and make sure that your indentation remains correct otherwise it could mess things up once you've done that you can then head over to configuration and then into server controls and you can actually just reload the template entities once you've reloaded head over into devtools and then states and then search for car underscore found and you'll see that the state is now on if i look at that it says that car found is home so that is working well but what about if we wanted to add a little bit more information into here so that it's a little bit more useful for our automations so what about if we wanted to add the confidence level into the attributes if we head back into the code so if we head back into the code and then on a new line we're going to enter attribute templates and then we give our attribute a name so i'm going to say confidence and then again we're going to use a template and we're going to paste in the exact same template that we have up here we're going to make a little bit of changes make sure your indentation is set correctly and then this time instead of true and then this time instead of printing true we're going to just simply print the vehicle dot confidence and that is all we need to do so now if we head back and restart home assistant so that is essentially printing the confidence level of that specific plate and then assigning that into an attribute so if we head back into configuration server controls and then reload our template entities jump back over into devtools and you will see that our template now has a confidence level added to the attribute so then we can use that to create our automations and use the confidence level just to make sure there is a little bit of extra security there if you wanted to add multiple cars into this you would simply repeat the process and then copy just simply copy and paste your um your template sensor copy this paste it again and then change the number plate that you're looking for and to the second vehicle you may also want to name these a little bit better than i have done there um but yeah simply copy and paste your templates um assign the plate and you should be good to go and there we go that is a basic beginner's guide to getting started with templates in home assistant and we covered three different examples of where you can use templates within home assistant and remember all of the tips and techniques that we covered you can take them and apply them to your own sensor or your own entities and get the results you need but that's about all the time we have for this video hope you guys enjoyed it make sure to drop a like if you liked it hit the subscribe button and make sure to leave your comments or questions in the comment box down below for what you want me to cover in the next tech tutorial tuesday thank you so much for watching and i will see you in the next video
Info
Channel: Everything Smart Home
Views: 109,285
Rating: undefined out of 5
Keywords: home assistant, home assistant templates, home assistant template sensor, home assistant template switch, home assistant template entity_id, home assistant template light, home automation, smart home, home assistant setup, home assistant automation, home assistant beginner, home assistant beginner's guide, home assistant getting started, home assistant if else automation
Id: GnW0mLt2YLg
Channel Id: undefined
Length: 26min 58sec (1618 seconds)
Published: Tue Feb 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.