ESPHome based PIR motion sensor setup
We have a couple Hue Motion Sensors in our apartment to provide some automation for lighting control which work great, albeit a bit expensive.
I wanted to extend our setup for our bedroom and since we already had an ESP32 NodeMCU device running ESPHome with a DHT22 Sensor collecting temperature and humidity metrics, extending this felt like the logical choice.
This led me to to discovering some basic PIR Motion Sensors which can easily be hooked up and report back if motion is detected.
- ESP32 NodeMCU
HC-SR501 PIR Motion Sensor
- AM312 PIR Motion Sensor
- Jumper cables
- Micro USB power cable
- Hot gue gun (Optional)
- DHT22 sensor (Optional)
- Tupperware box (Optional)
Since writing this blog post I have since replaced the HC-SR501 sensor with a AM312 due to the amount false positives that I was getting. As you can see from the screenshot below it dramatically reduced this. The only downside I have seen so far is the lack of customisation with sensitivity and trigger interval but it has worked flawlessly in the location we have it positioned.
ESPHome is an awesome platform that allows you to easily scaffold out IOT projects with a few lines of YAML.
Below is the configuration for our ESP device in our bedroom.
esphome: name: esphome_bedroom platform: ESP32 board: nodemcu-32s wifi: ssid: 'Sunshine_Recorder' password: !secret ssid_password domain: .beachi manual_ip: # Optional if you want a static IP static_ip: 192.168.1.104 gateway: 192.168.1.1 subnet: 255.255.255.0 logger: # For feeding back to HA api: password: !secret password ota: password: !secret password web_server: port: 80 sensor: - platform: dht model: AM2302 pin: GPIO15 temperature: name: "Bedroom Temperature" humidity: name: "Bedroom Humidity" update_interval: 60s binary_sensor: - platform: gpio pin: GPIO13 name: "Bedroom Motion" device_class: motion
Once flashed to the device ensuring that the
GPIO pins are connected correctly. You should be able to navigate to the address you configured or the DHCP provided one and you should get back the sensor statistics.
Configuring Home Assistant
Next lets hook this up to Home Assistant which is as easy as navigating to
Configuration -> Integrations -> + (Bottom left) -> ESPHome
and entering the IP/Host of your device.
Once configured, you will have a
binary_sensor configured which can be used in automation.
Since our setup is in our bedroom we do not want to have the lighting coming on when we move at night so added an input boolean which gets enabled when we activate our bedtime routine.
# Input boolean bedtime: name: Bedtime initial: off icon: mdi:hotel # Scripts bedtime: alias: 'Bedtime' sequence: - service: input_boolean.turn_on data: entity_id: input_boolean.bedtime - service: script.turn_on entity_id: - script.all_off # Automation - alias: Bedroom Motion On trigger: - platform: state entity_id: binary_sensor.bedroom_motion to: 'on' condition: - condition: state entity_id: input_boolean.bedtime state: 'off' - condition: state entity_id: sun.sun state: 'below_horizon' action:B085Q5ZR33 - service: light.turn_on entity_id: light.bedroom - service: switch.turn_on entity_id: switch.socket_fairy_lights - alias: Bedroom Motion Off trigger: - platform: state entity_id: binary_sensor.bedroom_motion to: 'off' for: minutes: 1 action: - service: light.turn_off entity_id: light.bedroom - service: switch.turn_off entity_id: switch.socket_fairy_lights - alias: 'Turn Off Bedtime' trigger: - platform: time at: '08:30:00' action: - service: input_boolean.turn_off data: entity_id: input_boolean.bedtime
I didn’t want to leave the ESP32 device exposed so build a very rudimentary case out of a tupperware box.
Cutting a hole on the front for the PIR sensor, using a hot glue gun to keep it fixed. As well as a small hole in the bottom for power, and one on the side for the DHT22 sensor.
To block out as much light from the LED on the ESP32 I cut some cardboard to shape.