Do you recognize that you came home and it turned out that you left some doors or windows open? If you use the templates and automation in this video, you will never have to worry about that again! And in the meantime, you’ll also learn how template sensors work! Let’s dive into this!
โญโญโญ NOTE: โญโญโญ
This article accompanies a YouTube video. I wrote it for people who would rather read than watch a video. To keep doing this, I want to ask you to check out the video, leave a comment under the video, give the video a thumbs up, and subscribe to my YouTube channel. This means that the video is offered more often to new visitors so that they also stay informed of the latest Home Assistant tutorials.
Thank you for your support!
Ed
Introduction
I arrived home recently and it turned out that one of my children left a door open before they went out. Luckily nobody entered my house, but it triggered me to create an automation in Home Assistant to prevent that in the future. In this video, I will show you how you can get a message on your phone if everybody left the house while a door or window is still open. It will not check if your locks are really closed. For that scenario, you’ll need a smart lock. This tutorial will check if a door or window is physically open and send a warning to your phone if the last person leaves the house.
Let’s look at what we will create in this tutorial.
We will create one template sensor that determines if a door or window is open. Next to that, we will create another template sensor that shows what doors or windows are open. And we will create an automation that sends a message to our phone as soon as the last person left the house and a door or window is still open.
But, before we start, please subscribe to my channel and hit the thumbs-up button for this video. This way, you make sure that I can keep creating these videos for you!
What is a Template Sensor in Home Assistant?
You might be asking yourself what a template sensor is. Well, if you add a device to Home Assistant, you’ll see that sensors are being added to Home Assistant. For instance, if you add a motion sensor to Home Assistant, you can trigger based on the state of that sensor. So, a sensor has a state. The state of a motion sensor is either On or Off (and sometimes also unavailable) and you can trigger another device (like a lightbulb) based on that state. For this, you use automations in Home Assistant. But, sometimes you want to trigger something in Home Assistant based on a state, but you do not really have a sensor for it in a device. In our case, we want to send a message as soon as at least ONE OF the entrance doors in the house is still open when we leave the house.
This is typically a use case that cannot be solved with the sensors of the devices alone. Well, technically, you could do that with an automation that checks every door sensor and acts upon the state of each door sensor, but it’s not the most future-proof solution. So, in cases like this, you can create your own sensors which we call template sensors. Your own created sensor will also return a state that you can use your own sensor in your automations to trigger upon.
Now, to get our use case to work, you need at least one door or window sensor. I use the Aqara door sensors and Switchbot Contact sensors for this. You’ll find links to both devices in the description below.
Setting up the template sensor that shows how many doors are open
The first template that we are going to create is a template that shows how many doors (or windows) are open in our house. Let’s create it.
If you want to start using template sensors, you have to add a line to the configuration.yaml. It’s easiest to do this with an add-on called Studio Code Server. Go to Settings, Add-ons, Click on Add-On Store and search for Studio Code Server. Click install to install Studio Code Server. After installing Studio Code Server, turn all the toggles on and click on Start.
If you open Studio Code Server, you’ll see all the files on your Home Assistant server. Open the configuration.yaml file and add this line to it:
template: !include templates.yaml
After that, make sure that you create a templates.yaml file by clicking on the New File icon. I already prepared the template and will walk you through it line by line.
By the way, you can also download the code that I created for this video through the download link in the description below. It’s the easiest way to get this to work. You do not have to type the code from the screen and will save you time and frustration. You might as well download it first so that you can follow along in this tutorial. Next to that, you sponsor me so that I can keep creating these videos for you.
Create the code to show how many doors are open
- So, our templates.yaml starts with the line
- sensor:
Below this line, we are defining all our custom sensors.
- First, we give our sensor a name. In this case: Number Of Doors Open. We can look up our sensor by this name later in Home Assistant.
- Then, we are giving our sensor a unique id. You can generate the unique id by clicking on your right mouse button in Studio Code server en selecting Generate UUID at cursor. the unique Id is needed so that we can edit some settings for our sensor using the UI of Home Assistant.
- In the next line, we are defining an icon. in our case mdi:door-open
- And now we are going to define the state of our sensor. This is the value that our sensor returns.
- First, I define a namespace variable that will hold the result of a query that we are going to do on the database. I called it doorsensors.
- Next, we are creating a filter to filter the data from our database. I want to filter all the door sensors in my setup that have the state “on” which means that they are open.
- It works best to first go to the developer tools and retrieve the state of a door sensor to see what the attributes of a door sensor are.
- Click on Set State and search for one of the door sensors in your set up.
- So, go to Developer Tools and click on the States Tab. In my case that’s Office Door
- Now you see the State attributes here. What we need to filter are all the devices that have a device_class: door.
Exclude some doors
But, sometimes you do not want some door sensors to be taken into account. For instance door sensors that you might use on your fridge or, in my case, my letterbox. I can leave them out of the filter by using the friendly_name attribute which I will show you in a minute.
- Now, go back to Studio Code Server
- So, I filter first on all the devices that contain an attribute device_class and where the device class has the value “door”. Just like we say in the developer tools.
- And I want to exclude one door sensor in my case which has the Friendly name “Mailbox Sensor contact”. Using this method, you can add your own door sensors that you do not want to include. For instance door sensors that you use to monitor the door of your fridge or inside doors.
- I only want to include doors that are open, so I also filter on the state ‘on’.
- And for each result, I store the name of the device in the namespace and create a list.
- This last line counts the number of entries in our namespace and returns that value as the state of the custom sensor
- To activate this sensor in Home Assistant, click <ctrl><shift>-P and select Home Assistant: Reload Template Entities.
- You can also go to the Developer tools, open the YAML tab and click on Template Entities.
Test the Template Sensor
Our First custom template sensor is now created and loaded into Home Assistant. Let’s see if it works.
- Go to Developer Tools
- Click the STATES tab
- Click on Set State
- Enter the name of our new sensor: Number Of Doors Open
- The value is 0 because all my doors are closed. Let’s open a door and click on the refresh icon,
- The value is now 1, so it works!
I need your help!
You will be doing me a huge favor if you subscribe to my channel if you haven’t already. And, you will help me a lot if you also give this video a thumbs up and leave a comment. This way, YouTube will present this video to new people, making the channel grow! In the video description, you will also find information about how you can sponsor me so that I can continue to make these tutorials for you.
Thank you!
Setting up the template sensor that shows what doors are open
We can now see how many doors are open with the sensor that we’ve just created. I also want to know what doors are open exactly. For that, we are going to create another template sensor that returns a list of open doors in its state.
- Go to Studio Code Server
- This second sensor template is almost the same as our first template. There are multiple ways to do this, but to keep this tutorial simple, I decided to create two template sensors.
- So, I copied the code of the first template sensor and gave it the name: What Doors Are Open.
- I assigned a new unique id.
- The filter is exactly the same as our first template sensor.
- This time I’ve added an if statement that checks if any doors are open. If doors are open, then a list of device names is returned in the state. The backslash n makes sure that each name starts on a new line. If no doors are open, the value “unavailable” is returned.
- Load the templates into Home Assistant again by going to the developer tools, YAML tab, and clicking Template Entities.
Let’s test this second template sensor now.
- Go to the Developer Tools
- Click on the States tab
- and Click on Set State
- Now enter the new template Sensor name: What doors are open
- The state is unavailable because all doors are closed.
- Now let’s open a door and click refresh
- Yes, the Office door name is shown now, so this works.
Set up the automation
We only have to do one thing more and that is creating the automation that will send a message to our phone to warn us that one or more doors are still open while everybody left the house.
- Go to Settings
- Go to Automations and Scenes
- I created an automation already, so let’s open this.
- At triggers you see that I’ve added a state trigger
- The Entity is my zone, which is called Zone
- At “To” I entered 0. This means that this automation gets triggered as soon as 0 persons are in the zone. To make this work, you have to use the Home Assistant Companion app or another tracking device and have that device connected to your Person Entity in Home Assistant. See this video on how to do that.
- Then, at Conditions, I check if our template sensor that counts the number of opened doors has a value higher than Zero. This is a state condition with the entity name Number Of Doors Open
- At Actions I use a little bit of YAML.
- I’ve added a Call Service action and the data of that action shows a Title and message
- The message shows the line “The Following doors are still open: followed by the list of doors that is generated by the template sensor that we’ve created before.
Our automation is now Ready and will send a message as soon as everybody left the house and one or more doors are still open.
Conclusion
Let me know in the comments if you succeeded to set this up yourself. You can also ask questions on my Discord server. There’s a nice community over there that can help you with your questions. You’ll find the link to my discord server in the description below.
If you don’t want to type the code from the screen, you can download the code that Iโve created for this video via the ko-fi link in the description below. With this, you sponsor me, and make it possible for me to keep creating these videos for you.
I want to thank everyone who has supported me in making these videos and tutorials so far. I could never have done this without you. Thank you! You can support me through Patreon, Ko-Fi, or by joining my channel. If you also want to support me, look in the description of this video for the links. With that, you make it possible that I can continue to make these videos for you.
Oh, donโt forget to give this video a thumbs up, subscribe to my channel, and hit the notification bell.
I will see you soon!
Bye bye!