Wireless PIR Sensor

Hi… I’m working on a project that I know I’m going to need help with.

I want to setup a wifi PIR sensor to work with a Sonoff and HA.

My list of components:

  • HC-SR501 PIR Sensor
  • ESP-01 Wifi Module
  • 5-30V to 3.3V DC-DC Buck (ASM1117)
  • Sonoff Basics

Basically, I was the setup to turn off the fan and light shortly after I leave the room. I don’t need it to turn on the lights/fan automatically. I just need it to turn them off when no one is detected in there.

How I envision it working is when someone turns on the lights and/or fan, the respected Sonoff sends a MQTT status update to HA. From there, HA will wait 10-15 mins and then ask the PIR sensor to check if there’s any movement in the room. If the PIR returns “True”, then HA waits another 10-15 mins to check again. This continues until the PIR returns a “False”. Once a “False” is returned, HA waits again another 10-15 mins and check one last time. If “False” is returned twice in a row, then HA will then send a MQTT message to turn off the lights and/or the fan.

Also. I would like to setup the Wifi PIR with batteries. At first I was thinking 4 AAA batteries, but now I’m not too sure. I also know that while the PIR will use 5+V, the ESP-01 will need to volt at 3.3V. For this, I also got a 5-30V to 3.3V buck module. I think though, that I will need to add something else, but I’m not sure. With this setup, I figure that the ESP-01 will need to be constantly powered and the PIR may be turn on when needed. I was thinking that it will need some kind of a relay to toddle the power to the PIR. I’m just not sure what kind relay or whatever else may work may be used.

To conserve battery power, I was thinking of using deep-sleep between the 10-15mins gaps and also, if possible, after hours (2am to 8am).

Here a flow chart of what I’m trying to achieve:

I am a complete noob when it comes to HA and writing sketches. So, any help with this will be greatly appreciated.

not sure about the hardware, but I have lots of PIRs for motion detection. the way I use them is when PIR goes off, it triggers an automation to turn the lights off.
your PIR will have 2 potentiometers, one for sensitivity (mines usually set to max) and one for time (mines set to about 5min) and there’s also a jumper connector if you want to retrigger as long as there is movement (which again I’ve selected)
This way PIR show +3.3V when motion is detected and for 5min after last motion detected, then goes to 0 (this is your trigger for HA)
You could set your ESP to be off wifi and only connect when PIR goes from +3.3V to 0V, then disconnect. This would save you batteries for sure. I’m not familiar with the ESP-01 but I have a few WeMos D1 mini pros ESP8266) so I suspect it would work in a similar sort of way…

1 Like

I do something similar in my Loft to auto turn off the lights when no one’s in there. I’m not at home to post my automation but basically it triggers via PIR showing no motion for 15 mins, then it shuts off the lights. I’ll post my automation when I can for you.

- alias: Loft Lights (Auto Off)
  trigger:
    platform: state
    entity_id: binary_sensor.multisensor_feather_5_2
    to: 'off'
    for:
      minutes: 30
      
  action:
   - service: switch.turn_off
     entity_id: switch.loft_lights_switch

If no motion in the loft for 30 mins, turn the lights off. The timer auto resets on motion. Great for if your kids leave lights on when they leave a room.

1 Like

I’d stay away from the esp-01, spend a few more bucks for a mini d1 or similar.

I have one esp-01, and the problem is the lack of io, it only has two available and you have to be careful how you use them or it won’t boot correctly all the time. Both pins have to be high at start for a normal boot. One of them has to get pulled low to program.

I ended up adding an io expander to it, and then you’re not saving any money.

1 Like

here’s my automation if it helps.
Take into account movement, sun and a few other sensors and dims the lights off for 2min when no movement, then turns them off
I dim them off so that if I happen to be in the room but don’t move, I don’t end up in the dark

1 Like

Thanks for your reply. I did thought of that method, but I was afraid of the many MQTT messages being sent from the PIR.

However, I still wouldn’t mind giving that method a try to see how it would work. May be I’m over thinking the method and this may be more suitable than I thought before. Do you have somewhere to look where I can find a working sketch for the ESP-01. I will be using Tasmota on the Sonoff, which I already know can handle the part it needs to play.

ESP-01 has 4 GPIOs (GPIO0-3). from what I understand, two of them may be used.

Thanks again.

I got the ESP-01s already, so I want to try them out before deciding on the WeMos D1 minis.

Also, I think that the issue you were having with reliability has to do with power regulations issues with ESP8266 chips. This issue is more obvious when using batteries as it need about 80mA to boot, especially with button batteries. When using AAA/AA batteries or power adapters, this issue in often negligible. Otherwise, you will need to add a 1000uF capacitor to the module.

Oh yea. There’s also a way to expand the number of GPIOs to add up to 6 more. It will take some soldering, though.

Thanks for your reply.

MQTT traffic is minimal.

For the ESP to boot up in “normal” mode, GPIO 0 and 2 have to be high, and gpio15 has to be low. The ESP has internal pull ups and downs on these pins, so if not connected they will be in the right state.
You need to ground gpio0 to go to program mode. I put a little switch on mine to pull it low to program.

Nothing to do with poor power, I have 5 ESPs deployed around the house, and I know what a power problem looks like.

Its pretty easy to verify, caused me some frustration and head scratching when I first started working with ESPs.

And yeah, you can connect wires to the pins to add more gpio’s, but I’m not sure about long term reliability going that route, vs how cheap a mini d1 is.

If you plan ahead, and order from China, they are pretty cheap. I’m still surprised how cheap these guys are, for what they can do.

1 Like

Thanks on this.

Basically, I’m just repeating what others have said already…

Setting up anything that ASKS the PIR sensor to check if someone is in the room is certainly doable, but, it’s sort of “non standard”. Typical sketches for this sort of thing have the PIR sensor checking every X amount of time for movement and reporting the changes to MQTT (or whatever). So MQTT traffic would be a message when motion is detected, and another when it stops. You could certainly only report the “stop motion” events and not the “start motion” events, if you wanted to save a little bandwidth/battery.

Also, you can build this if you want to, but you certainly don’t HAVE to. There are cheap, off-the-shelf devices that already do this and run for a year+ on a set of batteries. If you want it to be REALLY inexpensive, look into the vast number of pre-made, off-the-shelf 433Mhz PIR sensors. They, typically, send a message on motion detection (not on motion stop). But, the automation is fairly simple. It’ll report another motion event every X time (some are configurable, some have 2 settings to choose from – i.e. every 5 seconds or every 5 minutes, some are not configurable at all). So… assuming yours reports every 5 minutes, you just build an automation that says “if it’s been 15 minutes since the last report, then turn off the devices”.

A setup like this allows you to decouple the PIR sensor from the switch. The sensor doesn’t need to know that the switch is on. And the switch doesn’t need to know that the sensor detected motion. The sensor just keeps sensing all the time. And the switch just keeps switching all the time. When Home Assistant sees that 15 minutes have elapsed without a motion event, if the device is on, it turns it off.

1 Like

I decided to try your method, but I’m still using the ESP-01S modules with the HC-SR501 PIR modules.

I wanted to flash the ESP-01s with Tasmota, but it seems they don’t support it at all. To make it work with Tasmota, I’d have to customize the source codes and compiles it. I don’t know enough to be able to attempt that right now.

I tried flashing an ESP-01s version of ESPurna as well. It flashes without any issue and once the configuration was set, it took care of many of the setting in HA for me. This was great. But, the bin files they have available, are for specific sensors or relay. None general, that will work for a PIR sensor.

Anyways, I found that ESPEasy has a firmware that seems to work. Plus, with the PUYA storage chip, they have a special bin for the ESP-01s. It seems to be working good so far, but I’m not yet used to ESPEasy and HA enough to know exactly how to get it done with certainly. ESPEasy documentation sucks. They explains things half-way or with some assumption that the reader knows what they’re talking about.

What sketch did you use for your setup?

I actually only use my ESPs to control LED strips, my PIRs are connected to GPIO ports on a RPi.

Check the code I’ve used for my Xmas baubles, it might steer you in the right direction.
You’ll have to figure out how to read from a port and what to do with that info as a result…

1 Like

It can be a challenge to find documentation in the espeasy wiki, but there is a lot there if you spend some time digging.

I’ve used espeasy for a while now, but mainly for relays and reed switches. I can try to answer questions, and you can ask at their forum.

One rather cool feature is you can send commands with something like
http://[ip addr of esp]/control?cmd=gpio],5,1

This will set gpio 5 to a 1

I’m sure you can read gpio’s too, but I’m not sure of that syntax at the moment.

And a link for you
https://www.letscontrolit.com/wiki/index.php/PIR_Sensor

Randy

1 Like

Actually, to get the gpio status, you can look at the device page
https://www.letscontrolit.com/wiki/index.php/ESP_Easy_web_interface#Devices_page

I normally pull up the log page, and watch the log scroll. You’ll be able to see change of states real time.

Randy

1 Like

Thank you, Randy. These links will help a great deal. In ESPEasy, they don’t have a setting for HA MQTT so I used OpenHAB MQTT. It seems to be working. There are some settings that I’ve never seen before and not sure what to put in them.

OpenHAB%20MQTT%20-%20Unknown%20Settings

LWT is Last Will and Testament, from what I understand it lets you know if the MQTT part of your esp goes offline.

I’m using a ping test to make sure my esp’s are online, so I’m not using the LWT stuff.

And you should use openhab mqtt as a “generic” mqtt, its also what I do.

1 Like

Thanks Randy… What about Controller lwl topic?

I have lwl blank, not sure what it’s for.

It’s controller publish that’s important to read a switch
%sysname%/%tskname%/%valname%

sysname is the name of the esp, tskname is the name you gave the switch when you create it in devices, and valname is the values field from devices

1 Like

I manage to get pretty far since I last posted. Finally for the pir sensors and the sonoffs working (publishing fine). Everything showing you in HA and seems to be working fine and many thinks to you, Randy.

All that’s left is to setup the automations. Trying to understand it. So far, I know I will need a “trigger”, “condition” and “action” statements. I know what the trigger and action will be, but I have to figure out what syntax are available for me to shape the conditions and how to lay it out properly.

Another thing I need to figure out is how to configure for both the lights and the fan together. I have the switches all in the same group.