Mailbox Sensor

“You’ve Got Mail!”

Similar projects worth following
After going all-in with Home Assistant, adding random bits of telemetry to my house has suddenly started making a lot more sense since everything can seamlessly talk amongst themselves now. I think everyone can relate to those times when you’re anxiously awaiting a mail delivery and find yourself checking the mailbox multiple times a day; this inspired me to throw together a very simple sensor to notify me when the mail has been delivered. And of course, in addition to a simple notification on my phone it was a legitimate NEED that it play “You’ve Got Mail” throughout my house.

My particular mailbox has a flap (similar to how some houses have a flap on their front door that the mail can be slid through), as well as a larger door that can be opened with a key to retrieve the mail. Sensing the movement of the flap is how I detect the mail as having been delivered.

The entire process from conception to finished project was probably a record for me; ESPHome just makes a project like this so easy. After the initial ‘idea’ (obviously I’m not the first person to think of doing this), I pretty quickly settled on a magnetic contact as my actual sensor. I considered some other options that would probably be less susceptible to errors, but they all seemed like they would be more of a pain to install and be less ‘plug and play’. I also settled on an ESP32 pretty quickly, as I knew I wanted to run the whole thing off of AA batteries (AC power is close-by, but that would look a lot less elegant and would likely not meet with wife approval), so having a ‘Deep Sleep’ mode was critical.
After a short bit learning the relevant bits of ESPhome, I put together my ‘code’ and after a few days of tweaking some settings while on a breadboard I ended up with this:

  friendly_name: Mailbox

  name: mailbox

  board: esp32dev
    type: arduino

# Enable logging

# Enable Home Assistant API

  password: "de2be17edd32842394fa0b50241e20b6"

  - platform: status
    name: "$friendly_name Node Status"
    id: system_status
  - platform: gpio
      number: GPIO15
      inverted: true
        input: true
        pullup: true
    name: "Mailbox Contact Sensor"
    id: contact_sensor
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
    ssid: "Mailbox Fallback Hotspot"
    password: "yTQxMoGcyJlE"


  - platform: wifi_info
      name: $friendly_name IP Address
# Send WiFi signal strength & uptime to HA
  - platform: wifi_signal
    name: $friendly_name WiFi Strength
    update_interval: 60s
  - platform: uptime
    name: $friendly_name "Uptime"

    number: GPIO2
    inverted: true

  run_duration: 30s
    number: GPIO 15
  wakeup_pin_mode: KEEP_AWAKE

A good chunk of this is just standard stuff I put in all of my ESPHome projects to give me data about the device for troubleshooting, etc. (I picked this up from various sources over time and have found it incredibly helpful). The only real ‘smarts’ of this is sensing the state of the magnetic contact sensor. Additionally, to preserve power I put the microcontroller into a deep-sleep mode whenever the sensor detects the mailbox flap as closed. When mail is deposited, the door opens and the sensor wakes up the device and reports its current state before going back to sleep after a short bit (over time I may lower the amount of wake-up time, but for now I just wanted to make sure it worked rather than squeeze every bit of life from the batteries). On the Home Assistant side, I just look for the device to go from ‘disconnected’ to ‘connected’ and treat this as ‘the mail has been delivered’ for my automation purposes. The reason for this is simple: the door opening process is very transitory and will all but assuredly be finished by the time the ESP32 wakes up, connects to my Wi-Fi and is able to report the state of the sensor.

As soon as I was happy with the results, my son helped me design a simple box to house everything in. A few hours of printing and a few minutes of soldering and… voila!

The actual install was pretty simple, but it did require a little improvisation. While well aware that metal and magnets play together, I was surprised how much just being NEXT to some thin metal resulted in the magnetic contact sensor barely working. Ultimately, I was able to arrange things such that the contact sensor and magnet were nearly touching when the mailbox flap door is closed and the...

Read more »

Mailbox Sensor Box.f3d

Fusion 360 File

fusion - 116.45 kB - 03/06/2022 at 21:00


step - 76.96 kB - 03/06/2022 at 21:00


  • ESP32 and Deep Sleep

    Ben Brooks03/28/2022 at 21:08 0 comments

    Well... after a couple more weeks my 6-AA batteries still proved to be insufficient to keep powering the ESP32 (or at least enough to power up the WiFi). It's been chilly here, but barely below freezing at night, so temperature clearly isn't the culprit this time (or at least not the main one). Considering that it's in deep-sleep all the time, except for about 30 seconds every day, it seems that my power calculations were WAY off. My best guess is that the voltage regulator on the development board burns a LOT of power (well, lots is relative depending on your power source), and hence the order of magnitude less battery time I'm seeing. Fortunately, it dawned on me a few weeks ago that my mailbox is on an exterior wall that happens be be directly behind my washer/dryer AND there also happens to be a free outlet right there as well. It feels quite silly to say, but I just got done drilling a small hole through the exterior of my house so I could run a USB cable into my mailbox (and yes, I caulked it on both sides).

    Not quite as satisfying as getting it all running on batteries, but now I won't ever have to deal with them either. Win some, lose some.

  • Cold Weather and AA Batteries

    Ben Brooks03/08/2022 at 18:45 0 comments

    Well, this might set a record for the shortest working project that I thought was 'done'. Turns out AA batteries drop in voltage when cold a lot more than I expected. After some very mild cold weather (barely below freezing), the voltage dropped enough that the ESP32 wasn't able to fully boot/connect to Wi-Fi on being woken up. Obviously there are much more elegant ways to handle it, but based on what I had on hand I decided to just double the number of AA batteries I use (yes, I realize how excessive it is using 6xAA batteries to power such a simple and small device). Size of the finished project isn't really an issue, and presumably the extra voltage should mean I can eek out every bit of juice out of the batteries now. My only real concern is whether the loss from the voltage regulator on the board will start adding up more. Time will tell...

View all 2 project logs

Enjoy this project?



Danny Martin wrote 02/03/2023 at 21:54 point

Since I am new to Home Automation, can you give me more details as to how you completed the setup in HA. Thanks

  Are you sure? yes | no

Ben Brooks wrote 02/03/2023 at 23:47 point

I'll shoot you a PM.

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates