Home Assistant Templating Masterclass (Full Course)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
templating is one of the most advanced topics in home assistant and it causes people a lot of headache however you want to become a homosystem pro you need to master it in this coding class i'm going to teach you everything that i know about templating this course is meant to be watched sequentially but i'm going to leave timestamps in the description below if you want to skip to the specific parts of the course if you want to take this to another level there's a link in the description below to my smart home courses page this course will actually be uploaded over there but over there you get to watch without watching any ads you also have quizzes and assignments where you can actually show me your code and i can evaluate it and i can sort of give you feedback and you also get some cool quiz and a little certificate if you can complete the course but now let's roll the intro [Music] so absolutely core fundamental concept for you guys to grasp before we move any forward and this is like the only prerequisite you need to have an understanding of what states attributes and devices really are your device so your bulb or your motion sensor is represented in home assistant by something called an entity the unique way of identifying an entity is represented by an entity id an entity always has a state and the state is a representation of the entity so if it's a light bulb on and off you can overwrite the states of an entity without actually changing the physical device that's what you normally do in your developer tools and it will not actually impact the device itself you always have an entity id a timestamp for when it was last updated and last changed you most likely will have also have a name and a friendly name let's go to the developer tools and let's start playing around with some templates click on developer tools and click on the third tab template we're really going to be switching between temporary and states so we're going to need to use both at the same time if you want you can just open two windows if it's easier we're going to start with looking at the states of an entity so let's pick any entity i'm going to go to states and i'm going to look for a specific light called imac lamp and i'm going to copy it i'm going to click back to template i'm just gonna paste it in at the moment you can just see a string so anything come i can write anything and this is just going to be uh written on the right hand side on the right hand side you'll see the result you'll see the result type so in this case a string and you can see what is the result and over here we can actually type in whatever we want so this is our editing area and then at the bottom here actually tells you if it's listening in to any event in case something happens so this will change when we actually use real entities so we know that every entity has a state so we can get the state of the entity so is this on or is this off basically because it's light so let's start writing some templating so let's wrap around this this statement over here with the curly brackets and let's put a single quote around it all and now let's type this in properly and we're going to use a function called states open brackets close the brackets and we've got a result to see our result is on because the light currently is on there's another way to write this we can write something like this state dot light dot imac lamp and here you can see a huge amount of information so you can see we have a template state so we have a state imac lamp is on we have some attributes so there's plenty of information over here let's make more sense of this so if we add a dot at the end and we add something like name we can have the actual name of the entity id we can also reference enter the id again and we can see that over here and most importantly we can get our attributes so our attributes are going to give us a lot more information about what's going on with this entity so the best way to actually uh get only one specific attribute is to use something called the state attribute so if we have our kali brackets over here and we go state underscore attr and then we're going to put in our quote and then fill in the information for your own entity id close that bracket and it's saying the message so we get an error message so we're saying we're missing one requirement uh so there's one argument missing and that is actually the actual name of the entity that we want to get the information from so now we can see all the entities so if i want to pick the mode and just copy it in and add it here and we have the value of the mode which is normal so you can see that this is the key and and this is the value so if i change key so if i use some other value so i can pick brightness and we would get the result which is 212. the result doesn't have to be only um one singular value it can be actually multiple values so if we pick x y color and i copy that in you can see we have two values now specifically this also happens with the rgb color and with different other things now how do we actually split this out in a in a more concise way so let's pick for example the rgb color we should have three values two five five one six seven and eighty nine so you want to split them out the easiest thing to do is to use this expression over here so the curly brackets expression basically means go through a list and pick any value from a list so if i just put a 0 on it i will pick five five if i change this to one it will go to one six seven and i'll change it to two it'll go to eighty nine so let me show you again so i'm gonna keep this i'm gonna copy it i'm gonna paste it down below so you can actually see two five five one six seven so the way this works is that the zero value is the first first value so it starts with zero and each value in is is incremental so it's depending on the position okay in the list so in this example it's comma separated so i can have something like this zero and i get two five five cool so we've got the values now this is a integer so let's see if we can do the same thing with the string now if we pick a string so let's pick um what can we pick let's pick mode so if i'm going to go with mode so let me just modify this mode of course we have normal as our value now strings are actually in python you can iterate through a string and this is seen as as a as a list so to simplify this 0 would be n one will be o sorry n not m o two is r and so on so forth so i think you get the j so what we can do over here is we can pick the first value out of a string and that's going to be a zero we can then do one two three you know everything and you can see the string is switching so if you wanted to pick the first letter for any reason of a string you can use this little trick over here hey guys if you're getting value out of this video remember to like this video share this video and comment down below and let me know which part you're enjoying the most now let's look at a few ways in which you can manipulate strings so strings you can change them in a few different ways there's a complete list of functions i'm going to leave in the description down below um so you can actually go and and play with this in your own time the most useful ones for me are for example capitalize so if we add this pipe expression and we add the word capitalize this is written in in american english with a z not an s and you can see the results are different through on and on so let me just show you this in a better way so add this in so you can see on is sort of our standard result we can capitalize we can also do different things we can uh do something called upper which will convert all the strings to uppercase and you can probably guess there's going to be another one here with lower so lower actually is exactly the same as our original input so this all makes sense now i'm going to show you another example and i'm also going to show you how you can create a variable reason why you might want to use a variable and you might want to use it for calculations or for just simplifying and not repeating the same templating code over and over again so you do your templating code you store in a variable and you might reference that variable later on in your templating code so we've seen how states work and we can see that the current state of the weather at home is is rainy the current and you can see the attributes so we see things like temperature humidity pressure there's a long list of of attributes so if you remember correctly we can do state attribute open brackets close full so we can extract the temperature by writing this expression so i can get rid of this and actually get rid of this whole thing nice so we've got this now let's say for example that we wanted to do something like i want to compare the difference between the temperature inside the home and the temperature outside of the home so let's say we had a sensor and this sensor was called um i think i think i've got one called kitchen nest i'm gonna go and check now okay so i've picked another center the hallway sensor hallway temperature sensor so you can see this 21 degrees in the outside is 6 degrees so what could i do i could actually decide to do some calculation so let's say that i want to set a a variable to do that you can open your brackets again to do that open one bracket open the percent and here we can write something saying set so set is a keyword and now we can you know give our name of the variable so let's say we say outside temperature and in this case we need to use one equal not double equal because one equal is assignment so it's going to assign a value in the outside temperature and the value we are going to be assigning is basically this one over here which is the 6.5 and then we close it with the curly bracket awesome so you can see that this hasn't actually produced the value so when you actually using one curly bracket and the percent you are telling the you know the ginger to templating engine i basically need to do one operation you're not actually printing any value the print statement always comes from double brackets open and close so if i print out side temperature you can see now we've got a nice 6.5 so that's all good so if i did if we did something similar so i copy this whole line and did something like change this state so you can see that in some examples we're using the state attribute so sometimes temperature could be an attribute or sometimes temperature could be a a simple state so that doesn't really matter in our case because i can just replace that i can set it here and i will rename outside to inside temperature and then i can also print inside temperature so let me copy paste it in give it a nice space and now if i remove these over here you can see i have 6.5 and 21.2 i can do something like a comma and that will create like a list i can do like a double equal whatever i can put some text in can i do a uh subtraction between the two right can we do that let's see how we do that so remove these brackets and we can do something like this an operation between the outside temperature and the inside temperature so i don't know what's the difference error message we're getting over here right so we can't use the operand so an operand is a minus so if i do plus it will be same error okay so you can do minus plus multiplications and divisions so we can't use this operand so what do we do okay we can't use the operand we need to the message is actually telling us what to do it's basically saying that we can't use it for a string so one of these two values is given is giving me a string so i'm assuming that the string is the inside temperature and the outside temperature is a float in fact let's try and swap them around and see if the error message swaps around yes so you can see that now it changed so the string is the first value and the float is the second so you then know that the string inside temperature is a string so if i convert the inside temperature and i do that using a pipe i convert it to a float so i just write float ta-da we've got a result so because this is a float and this is now a float we can see that the temperature difference is 14.7 so i can write some text i'm going to say temperature difference is that uh you know 14.7 degrees what i also would really recommend i would recommend just adding a flow at the end even on the outside temperature just in case now because you are relying that this outside temperature so this actual expression doesn't change so in case homer system makes an update and they decide that this is a string you've already converted it afloat so you sort of you sort of future proofing yourself but technically at this stage it's ready to flow so it's not doing anything let me show you another example so i'm starting doing something a little bit more complex so let's assume so a weakness of thermostat sometimes that you have only one reading so you might have your thermostat in a cold spot and you really want a good average right so in my example uh i have a ground floor first floor and a second floor right so i i might have different temperatures for each floor okay so i might want my main thermostat that kicks in to be aware of that so what you can do is if you have a lot of these sensors you can set something for example let's say ground ground floor temperature or ground temperature and you just collect all the sensors that you have you convert them to float and you keep basically adding them in until you get to you know your number of sensors so assume that i have two sensors so then i'm going to take an average so i'm going to sum these together and then i'm going to divide it by two and be aware of the brackets here i'll show you so basically this is uh state so you have your normal state open bracket close bracket and then filter on the float so this is one expression then we've got another expression over here let's do this then we have this bracket and this bracket which is doing the dividing by two and then we're wrapping that all again with these brackets to round it up to two and we have a nice uh float of only two decimal places with round two obviously if you did round three or round four you know the the things would be different so so far so good round two we'll leave it as it is it's 20 dollar free and we always close the expression with the curly bracket the percentage curly bracket and then we can print out with ground temperature so maybe an assignment for you guys would be to complete this so if you have multiple temperatures and sensors go ahead and do this and and then you can have an average per floor and then you can have a general average which could be the average of each floor which would be slightly different than the average of all the actual sensors themselves but leave that to you i'm going to show you how we can actually loop from the states so looping is a console that i've covered in another video already which is specific for looping but however i just want to be quite clear looping is a powerful tool it is something that i think you should all explore so we're going to go through a different example today very similar to another example i did with about battery classes in the past so let's give it a go now the first line of code that we need is a output so we're going to need to create an output remember that output is just the variable and in this variable we are going to be storing two values that's why i'm using namespace and and you can see the uh curly brackets which which means that is is storing like a list an array let's let's make it simple so basically we're going to have two values and the values are going to be the name of the sensor and the current temperature of the sensor so we sort of need our first things first then we can start creating our loop okay so our for loop we're gonna day four so we're gonna be looking at for each state in so four and in our two keywords you can see them going blue so four state in and we're gonna go censors states dot center call and then we're gonna close the bracket close this up again and we see we've got an error message because we're missing that and four and i i think the temporary syntax errors are quite helpful if if we just try to read them and pay some attention to them so what can we do can we do can we do so i what i'm going to do is i'm going to add that in so we are good so we're gonna add the curly brackets and we're gonna just say um and loop so the end loop and four and that's another keyword i'll space it out because we know we could have something missing in here and the error message has gone away this is actually perfectly you know acceptable um you can have something like one two you know and you can see two is repeated a number of times the reason why that's repeated is because there are a lot of sensors in the list so it's for each sensor that i have i'm printing a value okay that's the idea of a loop will continue to go and it will iterate from it in fact if i did state you can see it printing out state let's see i'll put that around the curling brackets and you can see all the information that we have so state and you can see every single piece of information that you can use in the loop so that's one important thing so i can do stateless entity id and uh we have a list of all our entity ids we can change that and do state.state and you can see these are all of the values of those entity ids uh we can do both so we can do something like this state.state and say equals and we do n to the id right okay yeah so we're getting this wrong so we need to do something like this the subpro because we're not doing an operation we can keep them separate in their own curly brackets so now you can see we have the entity id and the state i can change this and we can put this maybe something different i think we'll put like the friendly name that would be like name if i'm not wrong cool so you've got name which looks a little bit nicer than the entity so this is the whole idea so now what we can do is we can filter the sensor so we can only have the time a specific um set of sensors so if you remember at the beginning when we did just state we saw a lot of information so we're going to leverage that we have the pipe and we're going to use the site attribute function and we're going to be using looking for a specific attribute so we'll be using looking for the attributes device class so we're going to be picking out basically we're filtering all of the sensors that have an attribute called device class which probably everything does have anyway so that's what the first thing we're doing but now we're actually going to do something a little bit more complex we're going to add an operand so we're actually going to be comparing with double equals and we're going to be using something called temperature now we have a list of everything that has actual temperature how do i know so let's check right so if we're looking at the one of these temperatures so the bedroom temperature is over here and this is what i'm actually using i'm using the device class temperature you i could change this to unit of measurement so if you add fahrenheit or celsius you could use this right and say oh if if the device the sensor has a device class and the device class is called temperature then i will put it in my in my fall and you can see that is basically what we're doing over here we're also pulling things that are unavailable like the fridge temperature is currently not working so we can also exclude this with an if statement the if statement over here will basically uh look at the states and deal with the unknowns so if we open our bracket and we're going to say if to say zero is is less than or equal to the state of state and then we will combat all of this to an integer -1 let me also close this up so we don't get too many error messages and let's give it an end so we need to always end the ifs similar to how we end the loops so if we indent this we can start seeing a pattern we should have excluded that value that we didn't need anymore because what we're actually doing is because we're using zero as a value we've got to go minus one so we know that if it if that is happening so you see if i put one unavailable appears if it was zero it's there if i'm minus one is gone but if i kept zero over here right and i change this to last time instead of less than equals two uh you can see it disappears so what basically what we're saying is that the if we convert the state to an integer okay and if it's a string it will convert to an integer and then that would basically give us that value so if we so that uh i think we can do it either way if it's clearer we can have the minus one and the less than equal to or we can just do it in a different way we you produce the same output which i guess is important now at the end of this if and the end of this four we should actually learn how to print these out in a little bit better way so we actually clean it up so we're going to be using a set so we're going to do this brackets open and again to close and we're going to be sats we're assigning the value so we're going to give a value to output so we're given an equal so we're assigning a value and we'll be assigning uh basically itself okay so output dot sends why are we assigning uh output to itself it's because what we want to do is so imagine you have one temperature line and then you have the second one and the third one on the fourth one you want the results to append so you don't want that when we go to the second line we lose the first one so we need that uh we need to keep adding basically appending to the end of the list so we have the output.census so we need that and now we can add the actual uh you know information to add it we'll be adding two values because uh that's what we're going to be having in this uh little array so we can add the state dot name and we're gonna use a tilde and the actual tilde and you gotta found it on the cue ball the tilde is going to make it be a string that is the point of this tilde and the quote and comma and then just space it tilde again and here we can put state dot state which will give us our value of the actual sensor and we can add a nice space at the end with our tilde expression now at the end of the loop if i print the output just like that now we've got to this point we've output output and we can see that the actual sensors is an empty array so it looks like we've gone wrong somewhere so i gave this a little bit of a look and it and i was missing the dot sensors so you the output dot sensors sensors you go uh was what was needed to actually get this all to work because actually this what i was saying before basically is appending the values cool so we can see that now we have all of our sensors outputting we still have our namespace remember our namespace we called it sensors which reflects this line of code over here so now what we're going to need to do is is just go dot sensors so we want to only get everything that's in the senses of this so now we can see the list how do we know it's still formatted as a list because we see these curly brackets coming through so as we used as you as any list you can do zero and you'll get the first value out of the list you could do minus one and you can get the last value of the list and you can do everything right two three four if you know how long the list is you can you can continue so that's one way you can actually format this you can actually do a nice pipe and the pipe will allow you to use something called the join which is a and a function that accepts brackets in the brackets you can put something like with the quotes you could put a comma and a command of space and you can see it all nicely in one piece of text or you can do something called a card return which basically is the backslash n which basically is the equivalent and then you got you've got your nice list of sensors another example that we looked at previously was another example a very similar thing is that you can actually find out all the lights that are currently on in your home assistant instance you get information like the name of the light the status which is on and also the color if you have an rgb color so you see that this actually is very useful and you can use it in lovelace dashboards in so many things like text-to-speech and you can just switch it off for example when you look at everything that's off and you can see everything that's off so it's easily customizable and the code very similar to what i just showed you uh actually very very similar i think the only difference over here is that we have a state and we're concatenating the state and we're having another the state name the state state and then we're just looking at the rgb color which is uh a key and we can see the stable attribute rgb color and you can see rgb color over here it's reflecting what's this so if i changed it you can see that this text is updating so this sort of hardcoded value for this as usual if you're uh if you're watching it's the course you download the files and and and see how they're built for yourself now everyone's gonna have the sensor.date it comes standard with home assistant and this sensor input gives us a value this value is a year year month month day day date so that's cool but what if we want to extract a certain part of the date okay we can use a function called split and i want to show you how we can actually do that so let's take this uh value over here and let's start splitting it okay so at the end of this bracket over here just start dot split and let's open up brackets now as soon as i've done stop split you can see that it's changed it looks like it's it's become like a list okay so it sort of knows that something is going to happen now how this is going to work what i want to do is i want to split this like this i want to basically say year is month is date day is okay so i want to be able to break down this in three different parts okay so the display is already creating a list an array okay it's creating an array and i'm using these two um in the context of today's lesson i'm just using them interchangeably uh listen all right okay let's split and what basis are we splitting we are splitting on the basis of the dash so when there's a dash we're splitting and this can work with anything so if you have a comma separated uh a string with commas in the middle you can separate on comma you can separate on on anything you want so splitting it up like this basically means we have free values so what i can say is that the current year is okay and copy this paste it in and remember we've used this a few times this trick we can do our arrays right so we can return a single position out of the array we know we start with they start with zero and we increment by one okay and because the format is year month date we know that already i can just do zero and that will give me that the current year is 2022. so most easily it's very simple to do because i just then while you copy and paste i replace year with month and let's say and i change this to value one you see the current month is a one let's continue the current day is angle value two and i said the current day is o6 now if you want to take the o's out you can add a pipe at the end and convert it to an integer so you can see now pipe int now it's not a string zero six was a string now it's an integer it's telling me it's one and really for consistency just do it on the year uh so so that's all cool so you can use a split and uh i hope you make good use of it because it's a quite a powerful function you can use this also in python if you're learning python now i'm going to show you some practical examples of how i use templating and if statements in home assistant okay over here there's a icon configurator so the way what i'm doing here and and you will recognize the mdis because they're like icons and what i'm saying is if the state of a specific sensor called the home occupancy sensor is on so the home is occupied then the output of this will be an mdi and will be the mdi of home dash account else so if it's not then i'm using the ndi home outline and you can see i've got my nd for the at the end so so the basic structure of an if statement is you have the brackets first is the condition then uh if it's basically if that condition is true then the value that we could return else and then the other value and then end if to close it so another way you can do this uh and i'm actually going to show you how i've done the homework sensor now so this um statement over here returns true or false so the true basically means that the home is occupied the false means it's not it's how this is built for each person that we have we have a person as a geo there's sabrina okay so we can evaluate these so is state is a function that gives me true or false it's not state it is state so there's a difference between state and is state so let me demonstrate to you that to you so if i open my curly brackets and i put states and i remove the this part over here so you can see that the state of myself is home i'm here i'm recording this video so the is state how the estate works is that it uses a comma to evaluate basically doing an if statement is saying if passing the geo is home then this returns true so this is giving me true okay but it's not finished out because the curly bracket is not closed we're doing all statements or is one of your operands you can use and use and or um not other operands but maybe and and and or and not are your most common operands an example that all basically is someone home so if it's not me it's someone else or the last piece of the puzzle is an input boolean so i have an input really with guest modes if there's a gas and this can get triggered through people connecting my wi-fi if there is a guest then it will go on so the home is occupied and this returns true now you can also use this logic in conditions now i'm going to show you something that you would have probably seen jason um and i'm going to show you how i use json so i've got some code over here the first part i'm doing you must have seen this by now if you've been watching this video but if you've been skipping along i'll quickly sort of recap setting creates a variable and assigns values to variables okay so the curly bracket over here is what is the convention used for json and json comes with key keys which are temperature and values which are 25 okay so we have this we have a comma and so this is 25 degrees celsius okay what we can also do over here is that we can basically loop through the values in the json and output them so for each and i've got two values for each key this is a key and it's a value in my json test dot items i want to output the key so let me just remove this so you can see it so you can see the key and the value you say it says temperature is and we've got over here we can also change this key and we can add things like capitalize to make it a little bit better and we've ended our fall so that's one way of doing things we can also set our own array so this this i'm using uh normal brackets so that you don't need the key value pairs and you can also do things like uh picking them out so you can pick zero pick one pick two and you can see i'm picking 22. so what you can do is you can mock things up so you don't need if you're using an api for example and you don't want to continue hit the api you can mock up your json file so create a mock-up here or mock-up array and then play around with your templating so that's a tip that i can give you one last cool thing i want to show you before we move into actually creating sensors and looking at conditions and how we can use these templates is the integration list so there's something called integration underscore entities and if you use it you will actually get a cool list of everything or every entity id and that's actually part of an integration so if you've got hue or if i change it to zha which is sort of what i'm using for zigbee you can actually have the full list of everything that's dependent on integration so i already got a use case for this before i just mention it here as something that could be useful uh and you might you guys might have some idea out of it so let me know if you do uh i'll be uh really looking forward to to read what you guys comment now i want to show you an example where you use templating in automations so automations are formed of triggers they're formed of actions and the form of conditions now in conditions themselves there is an option so when you actually build out your automation.yaml you can see that there's something called condition template now the condition template accepts a value template okay so if you provide your template and you put this little greater than symbol means you can use multiple lines to express what you want to express so in here i'm just making something up what i'm doing over here the meaning of this is saying this automation will only run on in the first three months of the year why i don't know i'm just making this up but it you could use it for things like uh raining season you know or some sort of other way okay winter summer whatever the important thing really out of this is that you return a true value okay so whenever the condition is met you want to return a true value and this is the only thing you want to return you want to return anything else you don't need to do an else because it's not a sensor you're basically just writing some templating code which you would do in the developer tools and once you're actually done you will put it in your automation.yaml okay now let me show you uh another example here in the configuration.yaml i've got a snippet off my configuration at yaml i've put it over here in the file editor so you can actually see it a lot better so i've got a few different ways i've used templating in the past to create my own sensors and then once you've got these sensors then you can use them as anything else and we don't have any uh limitations so if you don't if you find you're struggling uh with templating straight into conditions for example then i suggest you create it as a sensor first and then do it later okay so there's a couple two ways of doing it you can uh use template and then use sensor as a you know part of template or you can do it the other way around so you can have a template as part of sensors the tumble dryer is is an example that i'm using over here so i'm using a energy monitor that's looking at the power of the tumble dryer i'm converting that to a float i'm basically saying if the tumble dryer is at zero then i'm gonna say it's off else if which is a alif if the state of the tumble dryer is less than two so it means that's between two and and but it's bigger than zero if not you would have just stuck at this if you got to this point it means it's bigger than it's not equal to zero i mean it could be negative but it can't really have an uh negative energy in this example it would be standby as if it's not standby then i'm assuming it's actually drying this one it's a tumble dryer you can have this same code for washing machines against any anything you want the home occupancy center is what i showed you previously but this household all comes together the icon template is what i'm using over here for the icon so i'm returning mdis the value template is actually just all on one line and it's going to give me a true or false value so this center is true or false center home occupation will be actually the name of the center which is uh set over here but the name is expressed slightly differently over here when you define the template i need to find the sensors part of the template and at the end i actually have a cover a cover template i have a garage door and the garage door has a contact sensor and the contact sensor basically uses the state so if the contact sensor state is open which is defined by it being on then i would say that the value of the garage door is open if it's not open it's closed which makes sense uh we've got to end if and then the icon template will change so if the um basically it's the same check again right so if it's on it will show me the garage open symbol and if it's off it will show me the garage symbol remember every time you actually make changes to configuration or yellow you need to check if everything is fine before you restart home assistant do one at a time and check and test guys you've made it to the end of this video i fully appreciate the time they actually dedicated to watching this all if you want to get even more out of it as i mentioned at the beginning there's a link in the description below for the same content but the content will be expanded upon there will be new content coming on it will be add free view for you and also you get to interact with me through assignments some quizzes and you get a little certificate at the end but also check out the rest of the page because we're getting nearly towards 10 courses at the time of filming many more courses are coming online too so consider subscribing to the actual membership or check out the lifetime bundle to get the actual best value this is your front smile makers see you in the next one ciao
Info
Channel: Smart Home Makers
Views: 49,187
Rating: undefined out of 5
Keywords: home assistant, smart home, smart home makers, automation, home automation
Id: ZeQJn7QVuiA
Channel Id: undefined
Length: 46min 17sec (2777 seconds)
Published: Mon Jan 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.