ESPHome Pir Motion Sensor

A PIR sensor is often used to detect motion using Passive Infrared Radiation to detect changes on temperature readings of any object (human body or not) that’s moves in front of it’s lens. So “motion” for this sensor means detecting two different temperature between the two sensor side.

There are many options on the market that works good and are very commonly used with Home Assistant, for example I personally use models from Sonoff and Aqara. Both of them are battery powered with low energy Zigbee communication protocol and works generally very well with Zigbee2mqtt addon.

I’ve found two main downside of these devices that is same scenario can be annoying: the needs to replace batteries ( even if normally each battery can last around 1 year or more) and the Time-Delay: normally they have around 1 or 2 minutes delay between each reading, meaning that until the time out expires they are not able to detect any further movement.

Trying to overcame these two downside I decided to give a try to a different DIY solution, based on the ESPHome firmware.

Wiring PIR and Wemos modules

Very popular PIR module used on DIY projects is HC-SR501; used in combination with a Wemos D1 Esp8266 module and a standard 5V USB cellphone charger we can create a simple Motion Sensor.

HC-SR501 pinout

The HC-SR501 has ad adjustable Time-delay from 3 seconds to 5 minutes, so much less than standard Sonoff or Aqara devices. Additionally it’s also available a Sensitivity distance adjustment, from 3 up to 7 meters.

The output signal is 3,3 V so can be directly connected to Wemos digital Input. The VCC accept 5 to 20 voltage, so connect it to Wemos 5V pin.

Following the very simple wiring diagram:

HC-SR501 and Wemos D1 mini wiring

Once connected to power supply remember to wait about 30 seconds until the sensor readings became stable.

3D Printed Case

Download STL files here

ESPHome Binary Sensor

For best and easiest integration with Home Assistant I’ve used ESPHome to generate the code and ready-to-use sensor entity.

  • From ESPHome Addon console, click NEW DEVICE button, type a Name and click NEXT
  • Click SKIP THIS STEP
  • Select ESP8266 and click NEXT
  • Click SKIP again
  • Once back on the ESPHome main page the new device is listed with OFFLINE status, click EDIT
  • Copy and Paste following yaml code and click INSTALL

Remember to put your WiFi username and password on your secret.yaml file

esphome:
  name: pir-sensor-one

esp8266:
  board: d1_mini_lite

# Enable logging
logger:

# Enable Home Assistant API
api:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

binary_sensor:
  - platform: gpio
    pin: D4
    name: "Pir Sensor One"
    device_class: motion
    filters:
      - delayed_on_off: 20000ms

Further useful documentation here and here

  • Connect the Wemos module to your PC by a DATA Usb cable and click Plug into this computer
  • Select the USB COM port and click CONNECT
  • Wait until upload completion and click CLOSE
  • If everything went well … the device status will change to ONLINE in few seconds.
  • If you are in trouble uploading the code to your device using HA, try using ESPHome Flasher tool, download here the tool.
  • Home Assistant should automatically detect it and suggest to add and list the new device on the ESPHome Integration devices
  • The new device will add a new sensor entity with two status: Clear or Detected

Automation Example: Alexa notification on movement detected

From your Home Assistant main menu, go to Configuration – Automation and click CREATE AUTOMATION button

  • Click Start with an empty automation
  • Click 3 dots menu on top right corner and click Edit in YAML
  • Copy and Paste the following code:
alias: Motion Activacted Alexa Sound
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 5d0e9bab7c78efb82766562779a63cc5
    entity_id: binary_sensor.pir_sensor_one
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 1
condition: []
action:
  - service: notify.alexa_media_alexa_sala
    data:
      message: <audio src="soundbank://soundlibrary/magic_spells/magic_spells_14"/>
      data:
        type: tts
mode: restart
  • This automation will play ad sound (magic_spells_14) on my Alexa Echo Device. For all available sounds that you could play on Alexa, see this documentation here. With same Alexa TTS service you could also play TTS message instead of a sound.
  • Alexa notification needs Alexa Media Player custom components installed on your Home Assistant, more documentation here

Automation Example: Turn of and on Wall Tablet display

Use this automation to turn on and off the Tablet display once the PIR sensor detect someone in front of the screen. For further details about Wall Tablet Integration, see this article WALL TABLET – FULLY KIOSK HOME ASSISTANT

alias: Wall Tablet Motion Activation
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 5d0e9bab7c78efb82766562779a63cc5
    entity_id: binary_sensor.pir_sensor_one
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 1
condition: []
action:
  - type: turn_on
    device_id: b708c447a624cda20919671287f31d81
    entity_id: light.kpad_screen
    domain: light
    brightness_pct: 100
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - type: turn_off
    device_id: b708c447a624cda20919671287f31d81
    entity_id: light.kpad_screen
    domain: light
mode: restart