Motion detector for flexible lighting control.
about the project
With TRÅDFRI, IKEA offers a very well thought-out, visually appealing and inexpensive smart home lighting system. This system is also very easy to integrate with Home Assistant.
If you want the light in a room to be switched on by a motion detector, the associated motion detector from IKEA is the obvious choice. Such a scenario may be desirable, for example, if you have a small baby and the changing table is in the bathroom. If the baby is to be swaddled in the middle of the night, you don't necessarily want to press the light switch and, if necessary, have to dim the light.
The problem with the IKEA motion detector, however, is that you cannot set the length of time the light should remain on. The light goes out again after three minutes. If you have installed this motion detector in the bathroom and the shower is also there, you will be in the dark again after three minutes. Depending on whether the shower is in "sight" of the motion detector, waving your arm will help to switch the light back on. If the bathroom is a bit larger or there is a shower curtain, that is not enough and you would have to install additional IKEA motion detectors.
To solve this problem, we are constructing our own motion detector in this tutorial, especially for connecting to Home Assistant via ESPHome. Now we have all the freedom and can set exactly how long the light should be on in Home Assistant. For example, 30 minutes - which should be enough to swaddle the baby or take a shower. In addition, we can also define that if the light is switched on at night by the motion detector, it is automatically dimmed down. So that the baby is not frightened and continues to sleep relaxed after the diaper change.
Of course, this motion detector can also be used in conjunction with other Home Assistant-compatible lighting systems.
Other application scenarios for the motion detector can include:
- Monitoring rooms to see if there are any uninvited guests.
- Heating control in such a way that the heating is automatically raised in rooms that are rarely used when people are present.
- Activation of surveillance cameras when movement is detected.
The following components are required for assembly:
- WeMos D1 mini NodeMCU
- HC-SR501 PIR infrared motion detector
- Breadboard, 400 pin
- 40 x jumper cable 10 cm, male male
The complete kit in our shop:
In addition, an executable Home Assistant system with installed ESPHome is required. See details later in the tutorial.
Smart lighting system
In this tutorial we use a smart lamp from IKEA TRÅDFRIwhich supports a brightness control.
So that the "D1 Mini NodeMCU" can be plugged into the breadboard, it must first be soldered to the pin strips.
Please make sure that the pin strips are soldered on at right angles so that they later also fit the breadboard. To do this, you can first solder a pin to each pin strip and after it has cooled down, put everything on the breadboard (do not plug it in yet) to check whether it fits. If OK, the other pins can then simply be soldered. If it is not yet OK (wrong angle), the angle of the pin bar can be easily corrected by heating the already soldered pin.
Bend pin headers
We use the unused pin headers of the D1 Mini for the PIR infrared motion detector.
To do this, we bend one of the pin headers with the long pins at a right angle. We use one of the pin headers with the short pins as a template.
Then we bend the pin header with the long pins a little further, so that an acute angle is created.
This means that the pin header is under tension after it has been inserted into the breadboard and prevents the PIR infrared motion detector wobbles.
PIR infrared motion detector
The PIR infrared motion detector has two controllers:
- The left controller determines the duration of the output signal
- The right controller determines the sensitivity
Since a short output signal is sufficient for us, we turn the left controller to the left as far as it will go. We can set the right controller to medium sensitivity.
In addition, the PIR infrared motion detector has a jumper with two settings:
- Outside: The output signal is maintained for a period of time after movement and then deactivated again, even if further movements are detected.
- Inside: The output signal remains active if further movements are detected.
Since we want the light to stay on as long as movement is detected, we put the jumper inside.
In the next step, all components are placed on the breadboard. The following Fritzing diagram gives a good overview.
We plug the already bent pin header into the bottom of the PIR sensor on the one hand and into the breadboard on the other.
With the three jumper cables we have to be careful that we plug them into the breadboard in such a way that the three pins of the PIR sensor connect to the D1 Mini.
Prepare the smart home system
For the basic setup of Home Assistant see: https://www.home-assistant.io/getting-started/.
For the basic setup of ESPHome see: https: //esphome.io/guides/getting_started_hassio.html
To connect IKEA TRÅDFRI to Home Assistant, see: https://www.home-assistant.io/integrations/tradfri/
Connection to Home Assistant via ESPHome
Create node in ESPHome
In Home Assistant we navigate as follows: Supervisor -> ESPHome -> Open user interface.
With the "+ symbol" we create a new node.
As the node name, we store "Bewegungsmelder_bathzimmer".
We select “Wemos D1 and Wemos D1 mini” as the device type.
Next we enter “WiFi SSID”, “WiFi Password” and “OTA Access Password”. The "OTA Access Password" is optional and provides additional security if we are going to transfer updates directly to the D1 Mini via WLAN in the future.
Finally we click on “Submit” under “Finish”.
Next we add the for the PIR infrared motion detector required YAML configuration. To do this, we click on "Edit":
Now we add the following lines at the end of the YAML configuration:
# Sensor configuration entry
- platform: gpio
name: "PIR Sensor"
Upload the ESPHome configuration
Next, we connect the D1 Mini to the computer on which Home Assistant is running via a USB cable in order to be able to upload the configuration. In our case a Raspberry Pi.
Now we receive the notification “Discoved new serial port” from ESP Home. We can select the port in ESPHome. In our case / dev / ttyUSB0 (USB2.0-Serial).
Now we select the USB port in ESPHome at the top right. In our case / dev / ttyUSB0 (USB2.0-Serial).
And click on "upload" to upload the files required for ESPHome to our D1 Mini.
Now we can take a coffee break first, because the first upload takes a few minutes.
At the end of the upload, the motion detector connects to the Home Assistant via WLAN. We can now disconnect the USB cable of the D1 Mini from the Raspberry Pi and plug it into a normal USB power supply unit or a power bank.
Detects the PIR infrared motion detector a movement, so we can see this here too.
If we now call up the ESPHome user interface again, the bar at the top of the tile of the "motion detector bathroom" is green.
Add ESPHome Node to Home AssistantAt the same time a notification appears:
If we now click on "Notifications", we receive the following message:
Of course, we want to know what kind of “device” it is and click “Check it out”.
The “Integrations” screen now appears and we click on “Configure” in the “ESPHome” tile.
Now we can add the new ESPHome node to Home Assistant by clicking "Submit".
Next, we enter the password previously saved for "OTA updates".
And click on "Submit" again.
Next we can choose an area of our apartment.
Next we can test again whether our motion detector works. To do this, we click in the Home Assistant under Settings on “Devices” and then on “Movement detector bathroom”.
If a movement is detected, the status changes from "Quiet" to "Movement detected".
Now we can rename the name of the device by clicking on the pencil symbol.
In the input mask that follows, we enter the desired device name.
Set up automations
To control the light in the bathroom using the motion detector, we now need two automations:
- Movement in the bathroom: turns on the light.
- No movement in the bathroom: switches the light off again after a certain time.
To create the automations, we navigate in Home Assistant as follows: Settings -> Automations.
Automation 1: movement in the bathroom
Now we click on "Add Automation".
In the following picture we click on "Start with an empty automation".
First we give the automation a name.
Next, we define the trigger and store that the automation should be triggered when the PIR sensor of the motion detector in the bathroom has detected movement.
However, we don't always want the lights to be turned on. Therefore we set two conditions.
If the light is already switched on, switching it on again would change the brightness (see brightness below). Therefore, the light may only be switched on when it is still off.
In addition, we don't need to turn on the light during the day (at least when we have a daylight bath). Therefore we make the switch-on process dependent on the position of the sun and only allow it to be switched on "after sunset" and "before sunrise.
Finally, we store the action. The light should be switched on and dimmed down to a value of 15.
Now we save and go back to the overview of the automations.
Automation 2: No movement in the bathroom
Again we click on "Add Automation".
In the following picture we click again on "Start with an empty automation".
And here, too, we are initially giving the automation a name.
Next, we define the trigger and store that the automation should be triggered if the PIR sensor of the motion detector has not detected any movement for 30 minutes.
Finally, we store the action. The light should be switched off again.
Disclaimer - all information without guarantee:
The information contained in this tutorial (contribution) has been researched and compiled to the best of our knowledge and belief. However, mistakes can happen to us too. And something can also go wrong during the implementation of the tutorial or the content can be misunderstood. We cannot therefore accept any liability for any damage caused by following this tutorial. We are continuing to develop our tutorials. If something is inconsistent or unclear, please let us know so that we can correct or add to the point concerned. Thanks very much.