Motion Detection with ESPHome?

I have a vent fan controller that is a combination of circuits and ESPHome YAML code from different sources. The one I’ve made is for use in my shop, to handle smoke from my CNC/Laser system and to vent my 3D printers.

I have two small dogs and have them trained to use a pet potty and will soon be moving that potty area into a storage closet and adding a vent fan and using an ESP32 to control it in the same way I’m controlling the ones in my shop.

I don’t want the fan running all the time. I thought about maybe doing something like 5 minutes of every hours, but, ideally, I’d run it for 5-10 minutes after every time they use the puppy potty.

I have never had to detect motion or change and have thought that of 3 possibilities:

  1. A motion detector, or
  2. A video camera
  3. Some kind of entrance monitor

The problem is I want the area of detection to be limited to ONLY the pet potty and I know many motion detectors use ultrasound, so I don’t want to use sound that will bother dogs. I don’t know if there are motion detectors I could use with ESPHome, or just what kind of input from them I’d be looking for or using.

For video, I thought it might be possible to mount a webcam that would detect when X% of the image changed, or when it detected change in the image (which would mean a dog was in the potty area). Of course, there would be some tolerance in there to allow for the change over a day as the dogs make use of the potty pads. So I can’t just do something to check if the image is all white or not. (The potty pads are white, so if I had a camera, I figured it’d look straight down on it.)

For an entrance monitor, that can’t be something they step on and trigger, unless it can go under a protective shield, so there’s no problem with potty leakage possibly damaging in (if that kind of leakage happens). Maybe a light beam they don’t see that gets interrupted as they go in?

I’m sure there are a number of other methods I haven’t even thought of. I’m open to other ideas. The main point is to be able to toggle something on when motion ends. (I may need to toggle something when motion starts and something else when it ends - but I would think the end is more important than the beginning.)

What I’d do would be to set up the system using a countdown (I forgot the actual object name - it’s in my current fan controller code) so the fans would start a few minutes after the motion happens and stops. That way the dogs don’t associate their actions with triggering the fans.

Any ideas on how to detect when they go in the potty area and leave it?

They don’t use ultrasound.

HC-SR501 Adjustable PIR Motion Sensor Detector Module, Arduino Raspberry Pi PIC - AliExpress 502

There are many things you could use for this that don’t use ultrasonic:

ikea vallhorn might be ideal, cheap, use 2x aaa (ikea aaa rechargables highly recommended), you can place it in your closet and mount however desired.

you might also consider the ikea water leak sensor, for notifications about wetness.

both use zigbee, so you would need a suitable zigbee dongle/network set up.

You missed the point of the original post…ESPhome on an ESP32.

The point is to solve the problem, it doesn’t need esp specifically.

Another possibility could be a light beam sensor from a garage door.

You should just consider using BT room presence instead. Going that route will allow to filter out all false positives so that its not kicking On if a person triggers the “dumb” motion sensor and then it will allow you eliminate the need to just run it on an hourly cycle because, using BT allows you accurately track where people/devices/BT Beacons are within the house(which room) and the distance the beacon is from the BT Node(esp32).

Here’s mine for example. Mind how it looks in HA, im still playing with how i want to display the information and which sensor data. What im showing is how you can track people/devices throughout the house/rooms and create specific automations based on who or which device is in an area/room.

You can track Android, iPhone, Apple watches, BT beacons, etc. These are the Beacons I use and i just attach one to my dogs harnesses to be notified if or when she escapes from the fence and takes off!
[Bluecharm](Bluetooth BLE iBeacon (BC021-MultiBeacon) – with Button Trigger and Motion Sensor – Blue Charm Beacons Bluetooth BLE iBeacon (BC021-MultiBeacon) – with Button Trigger and Motion Sensor – Blue Charm Beacons)

1 Like

Here’s the old HA card i used for displaying beacons/devices FYI. Basically the same except I wasn’t tracking Beacon battery levels.

Wow! Love seeing that there’s so many ideas. It’s going to take me a while to go through them all to evaluate what will work well and be easily setup in my situation. (Easy doesn’t always work, but after decades of working with tech, easy is a big bonus!)

Ah - Thanks! I have never had reason to use any kind of motion detection, so I haven’t had to keep up with the tech involved. I had to look up PIR and see it’s infrared. I think that’d work well for 2 chihuahuas and their pet potty. While one is a long hair and would show less heat, still, I think dogs would show up in an IR system. This is one I’m going to look into more thoroughly. But, off the cuff, I’m guessing I could control levels on most PIR motion sensors? Like just how strong a heat presence it it needs to be triggered? Even if that’s all, I’d want to write a routine to detect presence and when the presence ends, so I might end up hooking it up to a Pi, so I can do more programming in a language of my choice.

Thank you! Some items in there I need to read up on to get a clear idea of how they work. The IR beam sounds like a good one I can look into. I saw that and thought, “Great!” then, “But with two dogs, how do I know if they’re going in or out?” Then I realized that with the countdowns ESPHome provides, they’re reset each time you call them. So if I set the countdown for 3 minutes, every time the beam is interrupted, the countdown starts over again, so it’ll always start 3 minutes after the last interruption, which will be when they leave. I don’t have to count interruptions to know - the countdown won’t go off as long as it’s being triggered and retriggered.

I looked that over. The only protocol I could find that I could be sure it uses is SmartThings - and I’ve seen a few references that Home Assistant doesn’t have it yet - not 100% sure about that. Have you seen docs or other info about other protocols it uses?

I do not understand how that will help. Am I supposed to put BT collars on the dogs so I know when they’re in a small pet potty area? The only people in the house don’t wear smartwatches and often we leave our phones on the kitchen counter when we get home. Since this is for a pet potty, it’s an area where the humans don’t go - unless we’re telling the dogs to go (and they do go on command - if they have a full enough bladder!), or cleaning it.

I’m just not clear how BT is going to help me know when a chihuahua is in the pet potty area or when it leaves it.

yes, there are adjustments you can make on the one I linked to.

Since you mentioned ESPhome from the start, this is how I control a light in my pantry using an ESP with a PIR for motion detection which then operates a relay to turn on / off an LED strip.

ESPhome Code
esphome:
  name: pantry_d1_mini

esp8266:
  board: d1_mini

wifi:
  ssid: !secret wifi_ssid_not_1
  password: !secret wifi_password_not_1
  manual_ip:
    static_ip: 192.168.30.63
    gateway: 192.168.30.1
    subnet: 255.255.255.0
  ap:
    ssid: pantry_node
    password: !secret wifi_fallback_ap_password

web_server:
  
api:

logger:

ota:
  password: !secret ota_password
  platform: esphome

binary_sensor:
  - platform: gpio
    pin: D1
    name: "Pantry Motion Sensor"
    device_class: motion
    id: sensor
    on_press:
      then:
        - script.execute: light_control

output:
  - platform: gpio
    id: r1
    pin:
      number: D5
      inverted: true


light:
  - platform: binary
    name: "Pantry Light"
    id: l1
    output: r1
    

script:
  - id: light_control
    mode: restart
    then:
      - light.turn_on: l1
      - delay: 20s
      - wait_until:
          condition:
            - binary_sensor.is_off: sensor
      - light.turn_off: l1

Thank you for the code - always helpful!

If I can do it all in ESPHome, that, of course, makes it a lot easier. As I mentioned, I was also thinking, if needed, I could do it on a Pi that could provide an input line to an ESP32 or might otherwise communicate with ESPHome - but being able to wire it in directly would be nice and probably a lot easier, in the long run!

I may have some questions on that after I’ve studied your example.

The ESP route will be far cheaper and easier to setup than an RPi.

I just ordered a set of 5, for a good price, on Amazon. (I expected the price to be higher due to tariffs, but it’s not.)

I will likely, though, take my test Pi and connect it there first for experimentation and so I can adjust it and get used to how it works and how sensitive it is. I think it’ll be easier to test it there. (I have a Pi laptop from Elcrow - lift the keyboard and it has all sorts of things you can use for testing sensors and setups and prototyping boards that connect to the Pi. It’s an awesome test bed!)

ikea vallhorn is ZIGBEE based, so any supported zigbee controller/dongle can be used and integrated with HA. search here or google for zigbee to learn more about that. ikea has several zigbee devices, although I understand that they are/will be moving to matter for future products.

in any case, for ~$13 + cost of a zigbee dongle if you don’t already have one, you can have a simple, ready to go motion sensor that you can place just about anywhere and that should suit your need.

I believe the BT presence does imply you would want some kind of BT collar attachment, this could be a tile or apple tracker, or any number of cheap chinese knockoffs attached to your pets collars. These could potentially give you much better insight into where your pets are within a wider area of your home/yard.