Hi there! Today we are going to talk about something more advanced in Home Assistant, which is called templates and custom sensors. With templates and custom sensors, you can format and convert your data so that it looks exactly the way how you want it to look. It's really powerful and I'm going to show you how you can set it up yourself in this video. When you are creating your dashboard in Home Assistant, you can add entities for all your devices. And in most of the cases, these entities always look the same. Sometimes you want them to look different. For instance, you want to have a different icon or a different color. And that's not always possible. With templates and custom sensors, you can do that. That's really powerful. Let me show you some use cases that we are going to build today. So what are we going to do? The first thing that we are going to create is a customized motion sensor message. Within that message, we are going to show a different icon based on the motion sensor state. So show a different icon if the state is motion detected or motion is not detected. We are going to do the same for the icon color. So that when motion is detected, we are going to show a different icon color than when motion is not detected. Then we are going to create a custom light counter sensor. Basically, this sensor shows us the number of lights that are on in our house. We are going to create a customized light counter message and we are going to show a different icon based on the light counter state. And also we are going to show a different icon color based on the light counter state. The last use case is that we are going to create a customized battery status indicator. We are going to show a different icon based on the battery state. So for instance, if the battery is 100% full, we are going to show a full battery icon. And if the battery is 50% full, we are going to show a half full battery icon. We are going to do the same for the color. So if the battery is full, we are going to show the icon in green. And if the battery is half full, we are going to show it in orange. And if it is almost empty, we are going to show it in red. The final result will be something like this. So let's head over to Home Assistant and I will show you how you can set this up yourself. When you want to use templates and custom sensors in Home Assistant, you have to use YAML. That might sound a little bit complicated to some people. But I am going to show you how to do it and I am going to try to make this as simple as possible so that everybody can work with this. There is this handy tool in Home Assistant where you can experiment with templates and sensors. And that is called the developer tools. Let's go over there and I am going to show you how you can work with that. When you go to developer tools over here, you have a couple of options. And the most important options that we are going to use now is the states tab and it is the template tab. When you click on the states tab, you can look into the values and attributes of entities in Home Assistant. For instance, if I click on set state and I retrieve an entity here, and I already created one which is a motion sensor, and I click on Smart Home Junkie motion sensor, you see that the state is currently off because there is no motion. You can also see that we have attributes here, for instance the temperature. This data is possible to retrieve using templates. So that is really handy to use. If you want to create a template or a custom sensor, you can experiment with this by going to the template tab. And what you see here is a template editor and it is already pre-filled with some default templates. We are going to remove them now because they are a little bit distracting. And I am going to show you how we can retrieve some data now using a template. When you want to create templates, you need to know what code you can use. Home Assistant is using Ginger 2. And there is documentation about that in this link. You will find it in the description below of course. And you can find all the documentation about how to use that here. I am not going through everything, but I am going to create some examples to let you see how this sort of works. So we are going back to developer tools and now I am going to create our first template. I want to create a template that retrieves the state of the Smart Home Junkie motion sensor. So if I go back to states and I am going to, let's click this away and click again Smart Home Junkie motion sensor. Then you see the ID of this entity here. Binary_sensor.SmartHomeJunkie_presence. So this is what we are going to need. Now I am going back to template and now I am going to add some code to retrieve the state of the motion sensor. I already prepared something here. I am going to explain how it works. So we are starting with two accolades and then the word states and then within parenthesis we are going to say binary_sensor.SmartHomeJunkie_presence. And what you see on the right side is that the result is off. And that is true because it was off. The state is off. If I go to states and I am going to Smart Home Junkie motion sensor you see that the state is off. So that is currently correct. So let's move a little bit in front of the motion sensor and see what happens. Look, adjunct to on. So that works. I got the state of the motion sensor here. That is great. The next thing that I want to do is I want to retrieve the temperature of that motion sensor. If you go back to states you can see in the entity of that motion sensor that the temperature is 28 degrees which is quite a lot but that is probably because I just touched it. So now we are going back to template and I am going to add another piece of code. As you can see this is a little bit different than the first line. This time we are not getting the state but we are going to get a state attribute. And we are going to say we want to have the state attribute temperature from the entity binary_sensor.SmartHomeJunkie_presence. Again within parenthesis and within accolades. And on the right side you see that we get a result of 28. So that works too. The next step is that we want to do something based on the state of that motion sensor. So let's go into that. I am going to add extra lines here. And I am going to paste the following code. And this is a little bit more code but I am going to explain this again. So what we have here is an if statement. So basically what we are saying if the state of binary_sensor.SmartHomeJunkie_presence.SmartHomeJunkie_presence. is equal to on then show a message there is motion detected. Otherwise show a message there is no motion at the moment. And as you can see on the right side you see there is no motion at the moment. So let's go over to the motion sensor and move a little bit in front of the motion sensor. Yes I did it and now you see it is saying there is motion detected because the state binary_sensor.SmartHomeJunkie_presence is on. You see that it is on here because this is the result of this line and that says it is on and this is actually the same code as this code here. So that is also working quite nice. The next use case that I am going to show you is about showing a light counter. I have some code prepared and I am going to explain you exactly how that works. I am going to paste the code here and basically what this code does is that it counts the number of lights that are on in my house. You see that it says momentarily there are 9 lights on in my house. What does it do? It basically counts all the light entities in my house and is saying do not take into account the entities that have an attribute which is called SDconsGroup and which is set to true and do not take into account the entities that have an attribute entity_id that is defined. It is saying take into account all the entities that have the state on. So the state of the light should be on. Make a list of that and count that list. That is what this code is saying. As you can see I get a result of 9 lights. So let's see if it works if I turn off some lights. So here are my lights and let's say it is now set to 9 and let's say I am going to turn off this mood light and then you see yes it is set to 8. Now I am going to turn off another light. It is set to 7 and now I am turning on the light again. It is again 8 and it is again 9. So that works perfectly too. In this use case we are going to retrieve the battery status of the battery that is in a motion sensor in my kitchen. Therefore we need this code and what this code is doing is basically again getting the state of in this case the entity sensor.kitchen_motion_sensor_battery level. Let's see just to double check if this is correct we go to states and I am going to paste that here and indeed here is my kitchen motion sensor and the battery state of that is 60. So it is 60% full and we also have some extra attributes here like percentage as a unit of measurement. Now go back to template and what you see here is staying 60%. So that is correct. I am getting the state of sensor.kitchen_motion_sensor_battery level. I am converting that to an int just to make sure that it is a number and I am rounding that down to the first full number and I am going to show you later why I am doing this and in the end I am going to say add a percentage sign. I can also say percentage and then you see that it shows percentage here. If I make a space in front of it it looks even more nice but let's make it just a percentage sign. So now we have retrieved the battery level of the motion sensor in the kitchen. Now wouldn't it be nice if we can show a certain icon based on the battery level itself. So let's say if the battery is 100% full we show a full icon and if the battery is 60% full we show an icon with a battery that is 60% full and we can do that. We are going to paste some code here and again we have an if statement here but what we are going to do first is we are going to declare a variable battery level here and what we are going to say is set the variable battery level to the state of sensor.kitchen_motion_sensor_battery_level and that is the value that we also used here and that value is basically 60. So what this code is saying or the result of this code is that the variable battery level will get the value of 60. Then we are going to the if statement and what we are saying here is if the battery level is equal to 100 then show an icon mdi_battery. Otherwise show an icon mdi_battery dash with the number. So in our case I would say that we will see mdi_60 and that is correct. Now you can say hey this is not an icon and yes you are correct this is not an icon but we can use it later on in our dashboard to make it an icon. If you want to know what code you need for what icon there is a site for that and it is the material design icons. So that is this site and you can search for instance on battery and then you get all the battery icons and if you hover over those icons you see the codes that you can use. So for instance battery 10, battery 20, battery 30 etc. This is battery 60 and that is the one that we are going to use. So this site is really valuable. By the way in Home Assistant in a lot of places you can automatically already find these icons but in the developer tools it is not that easy yet. So for all the use cases I created the code in the template editor to make sure that it works and now we are going to use this code in our dashboards. When you want to use custom sensors you have to add something to your configuration.jaml or to your sensors.jaml and I am going to show you how to do it. For that you need the file editor or you need studio code server. If you don't have it you can install it. I will show you where you can do that. So then you go to configuration, you go to add-ons, backups and supervisor and then you go to the add-ons store and here you can search for a file editor or you can search for the studio code server and install that and then you are fine. I already did that so I am going to my studio code server which I already opened here and in studio code server you can open all the files of your Home Assistant. So first I opened the configuration.jaml and what I did here is that I added a line to my configuration.jaml and that's this line that is called sensor include sensors.jaml and then I created a new file which is called sensors.jaml and in sensors.jaml I am writing all the code for my custom sensors. Now for our use cases I created two custom sensors and the first one that I am going to talk about is the count number of lights on custom sensor. That is this piece of code and what you basically see here is that this piece of code is the same code as that I used in my template editor. Let's go over there, assistant, go back to developer tools template. There you see this code is basically the same as this code. There is only some more code around it and what you do here is you are going to say I have a platform it is called template. Within template I have sensors. I am going to give it a name and this will be the name of the entity that you can find back in Home Assistant. I am going to give it a friendly name number lights on and I am going to give it a value template and that value template is basically the value that gets returned for this entity. That is the state that you get back and I am going to give it an icon in this case MDI light bulb group. That is the first custom sensor that I created. Then the next custom sensor is the battery in the kitchen custom sensor. I did exactly the same actually here. What you see here is again the code within the value template is the same code as that you see that we used here. What we experimented with in our template editor we can use in our final custom sensor. Again there is different code around it. Again we have a platform template sensors. The entity name is battery underscore status underscore kitchen. Friendly name is battery status kitchen. Then the value template that I copied and the unit of measurement in this case is a percentage sign. One of the things that is really important when you are working in YAML that you make sure that the indentation is always right because if the indentation here is wrong you will get errors. In Studio Code Server you get already a warning about it. That is really nice. Now the warning is gone. When you saved your sensors.yaml or your configuration.yaml you have to reboot home assistant. For that you go to configuration, you go to settings, then you can check the configuration if what you typed in your sensors.yaml or in your configuration.yaml is correct. Then you click restart and then you restart home assistant. After you restarted home assistant you can check if the entities that are created using these custom sensors are available in home assistant. Let's go back to developer tools and now we go to states. Now we are going to check if those entities are here. I am going to say number lights on. There it is. The state is 9 and the friendly name is number lights on. If you go back to Studio Code Server you see that the friendly name is number lights on. That is correct. The state was 9 and we already knew that it was 9 because I showed you before. This custom sensor is now working. I have a custom sensor and you can also see that the entity name is indeed sensor.number_lights_on. Look at that. Number_lights_on. The same we are going to do for battery status kitchen. Let me copy this. I go back to developer tools and in the state I am going to paste that entity. And yes, there it is. It shows that the state is 60%. What we did now is actually really cool. We created our own sensor. Something that was not in home assistant before and we created it ourselves. Now we can use these sensors in our dashboards too. Now we are going to add our freshly created new entities to a dashboard in home assistant. You can use the default entities card for that. But I want to use some more special cards because then I can show you a little bit more and it makes it more advanced. For that we are going to use hex. And if you haven't installed hex, the link to the video of how to install hex is in the description below. And what we are going to add to hex is two new front end integrations. So for that we go to front end and I already have them installed. And one of them is called the template entity row integration. And the other one is called the mushroom integration. If you haven't installed them yet, you can find them by going to explore and download repositories and search for them here. I will not find them because I have already installed them. But if you find them here, just click on them, install them and you're fine. Now that we installed those two front end integrations, we are going to use them to create our dashboard. So let's go to our dashboards. For this tutorial, I already created a dashboard and that dashboard is called templates and custom sensors. It's totally empty. So let's start with the first card. We are going to the three dots, click on edit dashboard and we're going to add a card. And now I'm going to add a custom card. So manual and I'm going to paste this code here. So what does this code do? It shows that it's an entities card and within the entities card, I'm going to show only one entity and that entity is the custom template entity row. That is the front end integration that we installed through HEX just now. And what we're saying here is that we want to have a different icon based on the state of this motion sensor. So what we're saying here is if the state of binary sensor smart home junkie presence is equal to on, so let's say there is motion, then show MDI motion sensor, else show MDI motion sensor off. In this case, there's no motion. So this icon is the MDI motion sensor off. Then the name, again, if the state of binary sensor smart home junkie presence is equal to on, then show there is motion detected, else show there is no motion at the moment. So you see there's no motion. So it's saying there's no motion at the moment. And then the color. If the state of binary sensor smart home junkie presence is on, then make the color green, else make the color gray. You see that it's gray. So now let's move in front of that motion sensor and see what happens. See motion is detected. So the icon changed in MDI motion sensor. The name changed in there is motion detected and the color changed to green. So this works great. This is a little bit of a difficult example because this front end integration is a little bit difficult for a lot of people. That's why I added the mushroom cards because they are more easy to use. And in the next examples, I will use mushroom cards. Let's add our custom sensor for the light counter to our dashboard. For that, I'm going to add a card. We're going to search for mushroom template cards. I'm going to remove all this. And first we're going to choose the entity, which is number lights on. And I'm going to add some codes for the icon. So what are we doing here? We are testing if lights are on or off. So if lights are off, then show us MDI light bulb group off. And if lights are on, then show us MDI light bulb group. There are lights on at the moment. So we see light bulb group as an icon. Let's do the same for the icon color. So we're going to add some code here. And again, we are testing if lights are on or off. If they're off, show the icon in gray. And if they're on, show the icon in amber. There are lights on currently, so it shows in amber. Let's do the same for the primary information. We're going to add some code here. And again, we are testing if lights are on or off. If lights are off, then show us the text all lights are off. If lights are on, then show us the number of lights that are on and the text lights on. So there are nine lights on, so it shows us the number which is nine and the text lights on. So that works. Let's test what this does if we turn the lights off. So I'm going to turn off all the lights in my house. And now you see that the icon changes, the icon color changes, and the text is changing. So this works. Now put them on one by one. And everything is back to how it was. So let's add our custom battery status sensor to this dashboard. So first I want to show you a card that's not really working, but I think it will make things more clear if I take the next step after that. So we're going to search for the mushroom entity card. And we're going to choose for the battery status kitchen. And then you see that the correct icon is shown. It's the 60% icon. It shows 60%. It shows the title. But the only thing that we cannot do is that we can show a color based on the percentage. And I really would like to have that I can only choose one color. And that's not really what I want. I want to have more flexibility. So I'm going to save this anyway. And now we're going to add another card. And that card is the mushroom template card. Let's remove all this again. And now we're going to choose the entity, which is the battery status kitchen entity, which is our custom sensor. And I'm going to add some code for the icon now. And because of the fact that our custom sensor for the battery status already has an if statement in it to show the correct icon, I can now retrieve that attribute, which is called icon from our custom sensor in this icon template field. And it shows us the correct icon already, which is the 60% icon. Now let's add code for the icon color. Unfortunately, you cannot add code to your custom sensor to determine the color. So we have to add some code in here. What we're doing here is again, setting a variable battery level that contains the state of our custom sensor. And then we are going to check if the battery level is higher than 60%, then it should be light green. If it is higher than 20%, it should be orange, and else it should be red. And there is an end if statement also here. Our battery is currently 60% full. So the first if statement checks if it's higher than 60%, ours is 60, so it's not higher. So it's not going to turn into light green, but it is higher than 20%. So our second if statement is if the battery level is higher than 20%. So it should turn orange. And indeed, it is orange. So that works fine. Now add some primary information. Let's just call that battery level and add some code for the secondary information. And what this code is doing that it retrieves the state of our battery status kitchen custom sensor, it rounds it down to the next lower full value, and it adds a percentage symbol at the end. So this is working too now. Now this was my introduction to templates and custom sensors in Home Assistant. I only used it in this tutorial in dashboards, but you can use it in other places too. For instance, in automations, I might create another video about that later. Anyways, I hope this helps. If so, give the video a thumbs up, subscribe to my channel, tick on the notification bell, and see you next time. Bye bye.