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:
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:
- .substitutions.yaml file with generic entries
- pir.yaml with the logic for this sensor
wifi_ssid: "your_ssid" wifi_password: "your_passwd" ota_passwd: "ota_passwd" api_passwd: "api_passwd"
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.
1. Flash your esp board for the first time.
2. Connect the components
Solder the wires:
- 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.
- 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.
- 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