ESPHome Wireless PIR and Light Sensor

by qpaulson in Workshop > Home Improvement

21065 Views, 13 Favorites, 0 Comments

ESPHome Wireless PIR and Light Sensor

Screen Shot 2021-01-22 at 10.09.07 AM.png

This project is to build a cheap ( Under $15 ) Wireless PIR motion sensor with Lux sensor so that you can automated the lights for whatever room its monitoring.

I built this because my Son was always leaving his lights on and I thought this was a good way to save some money.

It uses the ESP8266 for to allow for wireless communications. ESPHome allows easy configs and OTA updates.

With the Lux sensor, you can have the lights only come on when its actually dark in the room.

This sensor works with Home Assistant ( I have not tried it with any other Platform )

Supplies

  • WeMos D1 Mini ESP8266 Development board ( Or equivalent/similar )
  • HC-SR501 PIR Sensor
  • Light Sensitive photoresistor
  • 1 - 10K Ohm Resistor
  • Small silicon Wire
  • Need to be running Home Assistant ( I have not tried these with any other Home Automation platform )

Wire It Up

Screen Shot 2021-01-22 at 10.43.28 AM.png

The wiring is fairly straight forward as the schematic shows.

The PIR Sensor takes 5V input and the Photoresistor takes 3.3V input

Wire the PIR Sensor:

Note: If you take the white cover off the front of the PIR sensor, you can see the labels for the pins

  1. Wire the Ground on the Development Board to both the 10K resistor and the ground pin on the PIR sensor
  2. Wire the D4 Pin on the Development Board to the middle pin on the PIR sensor, that is the signal wire.
  3. Wire the 5V output on the Development board to the VCC pin on the PIR sensor


Wire the Photorestor:

  1. Wire the other end of the 10K resistor to one end of the Photorsistor and to the A0 pin on the Dev board.
  2. Wire the other end of the Photo resistor to the 3.3V power output of the Dev board.

IF you use my 3D printed housing/case for this project - The wires will need to be short :)

Setup ESPHome in HA

Screen Shot 2021-01-22 at 11.01.38 AM.png

In Home Assistant, if you don't already have ESPHome setup - You will need to install it.

  1. Login to your Home Assistant
  2. Click on "Supervisor"
  3. Click on "Add-on Store"
  4. Search ESPHome
  5. Install
  6. Once installed..
    1. Set the slide bar to "Start on Boot"
    2. Set the slide bar for "Add to sidebar"
  7. Click "Start"
  8. There should be a green dot in the top right corner once its started successfully
  9. Click on the ESPHome link that is now showing on the left side Nav
  10. It will ask you for a login:
    1. User: admin
    2. Leave the password blank

Setup the Sensor in Home Assistant Using ESPHome

This is the tricky part....

Now that the ESPHome plugin is fully installed we can plug in the Wemos D1 Mini board and program it.

  1. Plug in your Development Board to the same machine that Home Assistant is running on.
    1. Don't worry if the USB device does not show up in the dropdown just yet
  2. Click on the Green + in the bottom right of the screen
  3. Give your Node a name and click on the big green "next" button.
  4. In the Device dropdown, select "Wemos D1 and Wemos D1 Mini" or whatever board you have
  5. Enter in your WIFI SSID and WIFI Password
  6. Set a password for your device as well so you can protect the OTA updates.
  7. Click the big green "Next" button
  8. Click Submit
  9. Now the best thing to do is reboot your Home Assistant because ESPHome is a bit buggy and it will save you hassle later .. To do this:
    1. Click on Supervisor
    2. Click System at the top
    3. Click on Reboot Host
    4. Confirm
    5. Wait...

... 5 Minutes Later ...

  1. When HA is back up and ready, and ESPHome loads again, you can load the ESPHome page in the left nav
  2. Click on the EDIT button under your newly created Sensor
    1. Append the Code snippet below to the end of your file:
  3. Click Save
  4. At the top of the screen is a dropdown that says "OTA", click on that and you should be able to choose the USB device that the dev board is plugged into
  5. Click the "Upload" button

If all goes well, it should build, compile and push the code to your dev board. You should hopefully see the Sensor come online and show the WIFI signal strength, IP address and events that look something like this:

[11:19:02][D][adc:056]: 'LUX Sensor': Got voltage=0.38V
[11:19:02][D][sensor:092]: 'LUX Sensor': Sending state 76.56250 lux with 2 decimals of accuracy [11:19:32][D][adc:056]: 'LUX Sensor': Got voltage=0.38V [11:19:32][D][sensor:092]: 'LUX Sensor': Sending state 76.56250 lux with 2 decimals of accuracy [11:19:50][D][binary_sensor:036]: 'PIR Sensor': Sending state OFF

Code Snippet: ( REMEMBER TO APPEND, DO NOT REPLACE )

sensor:
  - platform: adc
    pin: A0
    name: "LUX Sensor"
    update_interval: '30s'
    unit_of_measurement: lux
    filters:
      - lambda: |-
          return (x / 10000.0) * 2000000.0;

binary_sensor:
  - platform: gpio
    pin: GPIO2
    name: "PIR Sensor"
    device_class: motion

Setup the Automation to Turn the Lights ON!

Once everything is working, you can move the sensor to the room it will be monitoring. Just make sure it has access to the WIFI so that it can connect and send its data to Home Assistant.

Now that it's in the room where the light level is going to be monitoring you can have it turn the lights on!

In Home Assistant:

  1. Click on "Configuration" in the bottom left.
  2. Click on "Automations"
  3. Click "ADD AUTOMATION"
  4. Give your Automation a Name
  5. Under Triggers, Set:
    1. Trigger Type to State
    2. Entity: You should see in the dropdown now a PIR Sensor ( something like: binary_sensor.pir_sensor )
    3. To: on ( must be lowercase )
  6. Under Conditions, Set:
    1. Condition Type to Numeric State
    2. Entity set to the new brightness sensor in the list ( something like "sensor.multisensor_brightness" )
    3. Below to 50 ( You will vary this setting to adjust the ambient light level you want to allow 50 works for me and my home ) Higher == More bright, Lower == More Dark
  7. Under Actions, Set:
    1. Action type to Device
    2. Device to whatever light you want to turn on
    3. Action to Turn on ... Light
    4. Brightness to 100
  8. Click on Save

Move around in the room when its dark - the lights should come on..

Turn Those Lights Back Off When There Is No Motion

We will set two lights off modes.. The first is when you leave the room, the motion stops. The lights should go out. To do this we need to create another Automation. So, in Home Assistant:

  1. Click on "Configuration" in the bottom left.
  2. Click on "Automations"
  3. Click "ADD AUTOMATION"
  4. Give your Automation a Name ( "Turn lights off after 2 minutes of no motion" )
  5. Trigger, set:
    1. Trigger type to State
    2. Entity to your PIR sensor again, same as last time
    3. To to off ( again, must be lower case)
    4. for to 00:02:00 ( This is the duration you want to ensure there's no motion, you can adjust as you need )
  6. Actions, set:
    1. Action Type to Device
    2. Device to the same light you chose in the previous automation
    3. Action to Turn off .... Light

Leave the room for ~3 minutes depending on your PIR sensor sensitivity and the lights should turn off

Turn Those Lights Back Off When Its Bright

Now that the lights turn on and off as expected, we need to save some money and not have the lights on when its bright in the room.. So lets turn them off when the sun shines through.

Create a new Automation, as we did in the last 2 steps

  1. Give your Automation a Name ( "Turn lights off when its bright" )
  2. Triggers, set:
    1. Trigger Type to Numeric State
    2. Entity set to the Brightness Sensor ( should be something like "sensor.multisensor_brightness" )
    3. Above set to 50 ( or whatever you set the value to in the first automation )
  3. Actions, set:
    1. Action Type to Device
    2. Device to the same light you setup in the last 2 automations
    3. Action to Turn off ... Lights

Thats it.. Now you should never have to use that light switch again. :D

Clean It Up and Make It Pretty

Screen Shot 2021-01-22 at 10.09.34 AM.png

If you have a 3D printer you can print the case for this and package it up nice.. Otherwise I find a small box and some glue can also work wonders.

If you want to 3D print it, check out my Thingiverse project here: https://www.thingiverse.com/thing:4733292

I hope it works and I hope you enjoy your newly automated home!

Build more of these and automate the world! :D