Home » Blog » How to create a perfect Toilet Occupancy Sensor in Home Assistant

How to create a perfect Toilet Occupancy Sensor in Home Assistant

Oh no, the light goes out again while I’m on the toilet. That motion sensor really doesn’t work at all in the toilet. Now I have to wave my arms again to make the light switch on. Do you recognize this? In this video, I show you the solution to this problem by creating a toilet occupancy sensor. Let’s go!


โญโญโญ 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

Watch the video here

Introduction

Almost all my lighting in the house is controlled by motion sensors. That works perfectly for almost all rooms, but it doesn’t work at all for the smallest room in the house. Nowadays an average toilet visit takes quite a long time if you are checking your mail and your socials in the meantime. And yes, then the light sometimes turns off. So, until now my lights in the toilet were still “dumb”. But I found a solution to that! And this solution is suitable for many more applications. I will tell you at the end of the video what other use cases apply to this sensor.

Oh, and in this video, you immediately get to see a sneak preview of the new automations user interface that is in Home Assistant from the 2022.9 release!

In this tutorial, we are going to build a simple DIY occupancy sensor that determines whether someone is present in a room based on the distance to the sensor. We will build this using ESPHome. You need the following parts for this:

  • A Wemos D1 mini
  • An HC SR-04 distance sensor
  • Some Dupont cables
  • A micro USB data cable
  • A soldering iron
  • Possibly a 3D Printer to print a nice box

You will find links to the parts in the description of this video. I get a small affiliate fee for these parts without you having to pay more. This helps me a little bit in making this video.

We are going to assemble our distance sensor first. This is a fairly simple job. the connections are as follows.

You can download the scheme and all the code used in the video here.

After we solder everything together, our sensor will look like this.

You can now connect the sensor to your computer via a micro USB data cable. Make sure you really use a micro USB data cable and not a cable that only transmits power.

Enable the Occupancy Sensor in ESPHome

We are now going to program the D1 mini with ESPHome. Make sure you have ESPHome installed in Home Assistant. You can do this in the Home Assistant Add-on store. Search the add-on store for ESPHome and install it.

  1. Open ESPHome and click New Device
  2. Click Continue
  3. Now enter a name for your project. In my case that is distance sensor

When you start ESPHome for the first time, you will also be asked to enter your WiFi data. Make sure that you enter the WiFi data from the same access point to which your Home Assistant server is connected.

  1. Now click on Next
  2. Now select the board you are using. In our case, that’s the D1Mini. So we choose ESP8266 here
  3. Now click on Next
  4. We have yet to enter the correct code, so we are not going to install the software yet. So click on Skip now.
  5. Now click on Edit and replace the code with this code.
    In this code, I indicated that the sensor updates every 5 seconds. You can adjust this to an interval that works best for you.

You can download all the code used in the video here. Downloading is free, but you can also make a small donation there when you download the code to support me so I can keep making these videos for you.

PCBWay logo

If you want to make prototypes or low volumes of your electronics projects, go to my sponsor PCBWay. As one of the most experienced PCB

manufacturers and SMT assemblers they can supply various PCBs, CNC, 3D print, assemble PCBs and even design your own PCBs. So, got to PCBWay to have your PCBs produced at competitive prices!

  1. Replace the WiFi data with the data from your own WiFi access point.
  2. Click on save now
  3. Let’s check our code first.
  4. Click on the three dots in the distance sensor map.
  5. Click on Validate

If the message at the top of the screen is green, that means your code is correct. So now we can install the code on the D1Mini.

  1. Click Install
  2. You now have several options. I have connected my D1Mini to my PC, so I choose Plug Into This Computer
  3. You can see that the code download is now being prepared. This will take a while. Please wait until it’s done.
  4. Then click on Download project
  5. The compiled code will now be saved on your PC
  6. Now click Open ESPHome Web
  7. Then click on Connect
  8. You should now be able to choose the USB port to which your D1Mini is connected
  9. Choose that USB port and click Connect
  10. Now click on Install
  11. Choose the file you just saved on your PC
  12. And click Install

The compiled code will now be installed on your D1Mini

  1. Then click on Close
  2. You can now close the browser tab or window
  3. Click Close

You will see that the distance sensor is initially set to Offline, but that will change to Online. We’re going to see if it works now.

  1. Click on Logs
  2. Because we want to know if it works via WiFi, we click on Wirelessly.

You will now see a log screen on which the information from your sensor is visible. You should see that the distance to an object in front of the distance sensor is displayed. I also added a sensor value that indicates how strong the WiFi signal is, but that is just for checking. We don’t really need it for this use case, but it can be useful to see if your WiFi signal is strong enough to reach your distance sensor.

  1. Now click on Close

Awesome! Our distance sensor now works in ESPHome. Now we’re going to make sure it’s seen by Home Assistant too!

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!

Enable the Occupancy Sensor in Home Assistant

  1. Go to Settings
  2. Go to Devices & Services

You should see that Home Assistant has automatically discovered your new ESPHome device.

  1. Click Configure in the distance sensor map.
  2. Click Submit
  3. Select an Area. In my case that is toilet
  4. Open the distance sensor by clicking on device

You will now see the distance and the WiFi signal appear as entities.
Super nice! Our sensor now works in Home Assistant. Let’s quickly create an automation to ensure that the light in our toilet stays on when someone is on the toilet.

Create the Automation to bring the Occupancy Detection to life

  1. Click on Settings
  2. Go to Automations & Scenes
  3. Click on Create automation
  4. Select: Start With An Empty automation

Hey, what you see now is the new automation interface that becomes effective in version 2022.9 of Home Assistant. At the moment (it’s still August 2022) it’s not active yet, so this is a special sneak preview if you watch this video for the September 2022 release.

  1. Click on the pencil next to New Automation
  2. Give the automation a name
  3. Click Submit

Add the Triggers

We are now going to add a trigger that detects if someone is on the toilet.

  1. Click on Add Trigger
  2. Select Device
  3. Select the distance sensor
  4. At “below” we choose 1 meter. So, if the distance to the sensor is less than 1 meter, we want the light to stay on. You can of course adjust this to a value that works well for you.
  5. Then click on the three dots in the top right of the Trigger

You can change two things here. What’s new is that you can give the Trigger a Friendly Name. This makes the automation a bit easier to read. This is not mandatory, but for this tutorial, I’ll show it anyway.

  1. Click on Rename
  2. Give the trigger a Friendly Name
  3. Click Submit
  4. Now click again on the three dots at the top right of the Trigger
  5. Now click on Edit ID

We are now going to give this trigger a Trigger ID. if you don’t know how Trigger IDs work, check out my video about Trigger IDs.

  1. Give the trigger a Trigger ID

We are now going to add a trigger that detects that no one is on the toilet.

  1. Now click on Add Trigger again
  2. Choose Device
  3. Select the distance sensor
  4. Now we choose 1 meter at “above”. So, if the distance to the sensor is greater than 1 meter, we want the light to go out.
  5. We set the duration to 1 minute. So, nothing has to be detected within 1 meter for 1 minute before the light goes out. Sometimes the sensor can pick up something (such as a fly) and then you don’t want the light to go out immediately. You can of course adjust this value so that it works best for you.
  1. Also give this trigger a name and a Trigger ID

And then we add one more trigger. This is because this sensor can detect a distance of up to about 2 meters. If that distance is greater, it returns the value Unknown. We should also check for this in case the distance cannot be measured. In that case, the light should also go out.

  1. Click on Add Trigger again
  2. Choose State this time
  3. At entity choose Distance
  4. Then click on To

What’s new in this interface is that you can now select a value. In the old version you had to type in the value, but now you can just select it. Super handy!

  1. Select Unknown here
  2. And set the duration again to 1 minute.
  3. Also, give this trigger a name and a Trigger ID
  4. Go to Actions now
  5. Click on Add Action
  6. Select Choose
  7. Click on Add Condition
  8. Select Triggered By
  9. Select the Trigger ID occupancy-detected
  10. You can also give this condition a name if you want.

We want the light to come on or stay on when this trigger is activated.

Add the Actions

  1. Now click on Add Action
  2. Select Device
  3. Select your toilet light
  4. At action select “Turn on Toilet Light”
  5. You can also rename this action. As you can see, a name is already generated for you, but you can change it if you want.

We are now going to add the option that turns off the light

  1. Click on Add Option
  2. Inside it, click on Add Condition
  3. Choose Or
  4. You can also change the condition name if you want.
  5. Now click on Add Condition within the OR condition
  6. Select Triggered By
  7. Select the Trigger ID no-occupancy
  8. Click on the following Add condition
  9. Select Triggered By
  10. Select the Trigger ID unknown-occupancy
  11. You can also change the names of these conditions again if you want.

Now what we’ve done is created a condition that says if no occupancy or unknown occupancy is detected, something must be done. That something in our case is that the light has to go out.

  1. Now click on Add Action at Actions
  2. Select Device
  3. Select the Toilet light
  4. Select “Turn off Toilet Light” by Action
  5. You can also rename this action.

Renaming Triggers, Conditions and Actions is optional, so you don’t have to do it.

  1. Click Save now!

Your automation is now working. If you test it, you will see that the correct triggers are activated the moment you get within a meter or outside a meter of the sensor. The code for this automation can be downloaded here. You can download it for free or decide to sponsor me before you download it.

Super! You have now built your own toilet occupancy sensor and can start using it right away.

Print a casing for the Toilet Occupancy Sensor

It is of course even better if you can build a nice box around it. I recently got a Creality Ender 3 Pro 3D printer, with which I printed this box for my occupancy sensor and I must say that I am very satisfied with this printer. I got it up and running within one evening. The prints that come out of it are of good quality and almost nothing went wrong during the printing. The Ender 3 Pro is a very affordable printer and you can now buy it at a discount from Banggood via the link in the description below the video. There is also a discount code in the description that you can enter during the checkout process. The discount code is only valid for a limited time, so order it quickly!

You will find a download link in the description of the video where you can download the design of this box.

Other Use Cases

You can use this distance sensor for many more things, for example to see whether the water level of a well or pond is too high or too low. Or in the kitchen to turn on the lights under the countertops when you’re standing in front of the counter. And there are much more applications to consider. Let me know your ideas in the comments below the video. I am very curious!

I can imagine you wondering why I don’t use batteries for this sensor. Well, ESPHome devices connect via WiFi and that consumes a lot of power. If you want to use batteries, you have to set the measurement interval very high (1 time per hour or so) or add a solar panel. Otherwise your batteries will be empty within one or two days!

This Do-it-yourself occupancy sensor can detect up to 2 meters away and is very suitable for the use case that I have shown in this video. If you’re interested in a more comprehensive occupancy sensor, check out Lewis’ new project from Everything Smart Home. He himself has designed a Smart Motion Sensor that can do much more. The link to his site can be found in the description below the video.

I hope this video helped you and 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!


,

Back to all articles