Simple outdoor PIR sensor for home assistant

At home I use home assistant to automate almost everything. One of the things I was still missing was a simple outdoor PIR sensor that can send detections to home assistant. This can then be used to turn on a light or to create other automations.

In this blog I want to share how to set up a simple PIR sensor by combining several low-cost items. I assume you have already some knowledge on how to setup home assistant and how to use ESP Home. If not, please use other tutorials first to get up to speed with this. Also, you will need to do some soldering, compile a small program and flash an ESP board. This I will explain step by step.

 

First let’s start with the components you need for this:

  • Outdoor 12V Pirsensor. I bought this from AliExpress here.
  • ESP8266 board D1 Mini, found here.
  • AC-DC power supply, 5V version found here.
  • Resistor of 10k ohm.


 

Total costs less than 15 euro (including shipping). Above links are examples I used. You can probably also find other units elsewhere. 


Schematics

The components are connected via the following scheme:




According to the specs the pir sensor works on 12V but it seems to work correct also on 5V and even lower. When a detection occurs, the red wire is connected to ground by the pir sensor. The pull-up resistor of 10k ohm will pull up the level to 3,3V when there is no detection.


ESP home yaml definition

Since I have multiple sensors and I don't want to specify some variables multiple times I use two yaml definitions:

  1. .substitutions.yaml file with generic entries
  2. pir.yaml with the logic for this sensor
.substitutions.yaml
wifi_ssid: "your_ssid"
wifi_password: "your_passwd"
ota_passwd: "ota_passwd"
api_passwd: "api_passwd"

pir1.yaml
substitutions:
  plug_name: pir1
  location: backyard
  <<: !include .substitutions.yaml

esphome:
  name: ${plug_name}
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: ${wifi_ssid}
  password: ${wifi_password}
  manual_ip:
    static_ip: <static_ip>
    gateway: <gateway>
    subnet: <subnet>

ota:
  password: ${ota_passwd}

api:
  password: ${api_passwd}
web_server:
  port: 80

# Enable logging
logger:

time:
  - platform: homeassistant
    id: homeassistant_time

binary_sensor:
  - platform: gpio
    pin:
      number: D1
      inverted: true
    id: ${plug_name}
    name: "${plug_name}  ${location}"
    device_class: motion

You can also leave out the static_ip, gateway and subnet but I found that this is more robust. This way you are not dependent on the name resolution of your gateway.


Step by step setup

The board and the power supply will fit in the outdoor pir sensor. You will only need to supply it with 220V. Detections are parsed via wifi to home assistant.



To set this up follow below steps:

1. Flash your esp board for the first time.

Setup your esp board first. This will save you the trouble of having to flash it when it is build in. You will need the ESP add on in home assistant. This can be installed via the Supervisor page. When installed click ESP Home in the left menu and add a device with the plus-sign at the top right.

Choose the name of your device and in the other fields you can fill in bogus values and after that submit the device. After this you can find the device on your esp page. Click edit and paste in the yaml definitions found in the previous section. If you want to use the .substitutions.yaml file you can create this from the command line or using the file editor add-on. The file should be created in the esphome directory (/config/esphome/.substitutions.yaml). You can also skip this part and add the values directly in the pir1.yaml file.

When the code is set up, you can compile the code, download the binary, connect the board via a usb cable to your pc and flash the board  with the program esphome-flasher.

2. Connect the components

Solder the wires:

  1. Connect the plus of the power supply (red wire) to the plus of the pir unit (brown wire). You can strip the red wire halfway, solder them together and use a heat shrink tube to isolate it. The end of the red wire can then be soldered directly to the 5v pin on the esp8266.
  2. Connect the minus of the power supply (black wire) to the minus of the pir unit (blue wire). Same as with the previous wire, you can join them halfway the black wire and solder the end of the black wire to the ground pin of the esp8266.
  3. Now first put the red wire of the pir unit and the resistor in place before soldering them. The resistor can cross over on the esp8266 board from the 3,3V pin to the D1 pin and the red wire from the pir unit needs to be connected to the D1 pin. Use some isolation around the resistor to prevent unwanted connections.

3. Configure and automate

Now everything is in place you can connect the power supply to the main 220volt net. Do this with care!! Check the log at the ESPHome page in Home Assistant. You should now also get a notification saying there is a new device available. Click this to enable the device. You should now be able to create your first automations. Please note that you should set the timing of the pir unit as low as possible. Delays can be added in home assistant if needed. This way the sensor stays on for at least 4 seconds.

Troubleshooting

If it doesn't work, some hints on trouble shooting:

  • check if you can ping the unit
  • check if you can reach the webpage of the board (type the IP address of the unit in your web browser)
  • if it doesn't connect, use a usb cable to check the logs with esp-flasher
  • check the name of your binary sensor id. not all characters are allowed here




Populaire posts van deze blog

Doorbell with home assistant