ESP8266 MQTT PIR Sensor

I made a sketch for a simple PIR sensor using a NodeMCU. It can be used inside HA using either a binary_sensor or sensor using the MQTT platform.

Here is the git repo:

Here is an example of it in use in my home:

This example creates a binary_sensor with an automation to brighten a light_group, if another light sensor is below a threshold and a someone is home, then dimming the lights after 10 minutes if the PIR does not detect movement. This will only occur if the lights are still on.

binary_sensor:
  platform: mqtt
  name: Kitchen PIR
  state_topic: sensor/pir001
  payload_on: 1
  payload_off: 0

automation:
  - alias: Kitchen Motion Detected
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_pir
        to: 'on'
    condition:
      - condition: state
        entity_id: group.phones
        state: home
      - condition: numeric_state
        entity_id: sensor.sn1_ldr
        below: '450'
    action:
      - alias: Wake Kitchen lights
        data:
          entity_id: group.kitchen
          brightness_pct: 80
        service: light.turn_on
  
  - alias: 'Reset kitchen lights 10 minutes after last movement'
    trigger:
      platform: state
      entity_id: binary_sensor.kitchen_pir
      to: 'off'
      for:
        minutes: 10
    condition:
      - condition: state
        entity_id: group.phones
        state: home
      - condition: numeric_state
        entity_id: sensor.sn1_ldr
        below: '450'
      - condition: state
        entity_id: light.kitchen_room_lights
        state: 'on'
    action:
      service: homeassistant.turn_on
      entity_id: scene.reset_kitchen_lights

Hardware setup example:
image

3 Likes

Great stuff! Thanks!

ArduinoJson was updated to v6, for this project to work I had to change line 44 of the main.cpp file from

StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
to
StaticJsonDocument<BUFFER_SIZE> jsonBuffer;

Also my PIR does’nt like 3V output from the nodeMCU, I got some weird up/down behaviour. Solved it by connecting both the MCU and the PIR to 5V (and same ground).

Hi. Will this work for the ESP8266 WeMos D1 Mini NodeMcu board?

Update: Process looking good so far.

I am on the right track I think. Performed the build and I do not get any errors like I was having before. I am still waiting on my pir to arrive in the mail to fully test if my setup process is successfully.

This is what I have for setup

platformio.ini

[env:d1_mini_lite]
platform = espressif8266
framework = arduino
board = d1_mini_lite
upload_protocol = esptool
lib_deps = ArduinoJson, PubSubClient

I was getting 2 errors. The first is the gentleman above me Potterstraat suggest to change the jsonBuffer

StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
to
StaticJsonDocument<BUFFER_SIZE> jsonBuffer;

The second error I was getting was something to this affect

arduino.h error reference to byte is ambiguous

I found this post suggesting to change this line in the arduino.h file, lines 247
The file location for me was C:\Users\Duc\.platformio\packages\framework-arduinoespressif8266\cores\esp8266

uint16_t makeWord (byte h, byte l);
to
uint16_t makeWord (uint8_t h, uint8_t l);

When I (Ctrl+Alt+B) to test/build, I did not get any errors.

Also, Note to myself if I ever want to connect the PIR on the 3v rail with this hack.