Post box lid sensor

Disclaimer: I apologise in advance if there is a similar topic here but I have searched unsuccessfully and I am new to the forum as well as to ESPhome.

What I want to achieve (description): I am trying to create a sensor for my postbox lid that tells my home assistant if the lid moves (open and/or close). Since I want the device to last long, I aim at using a solar charged battery and deep_sleep.

What I have:

  • ESP32 C3-dev
  • hooked up push-button on PIN 2
  • a connected solar charging circuit with battery
  • solar panels
  • no idea how to write proper ESPHome-YAML

!! The pushbutton RELEASES on lid-opening. !!

What I want to achieve (check list):

  1. Charge the battery via solar
  2. Use battery to power the esp-chip
  3. Connect and update the ESP32 to my home assistant via ESPHome and WIFI
  4. Register the status of the pushbutton on PIN 2 and process it in home assitant (dashboard and mobile-phone notification)
  5. enter deep_sleep after some seconds of inactivity
  6. wake up from deep_sleep on change of push-button (push and/or release)
  7. prevent deep_sleep according to helper-boolean in home assistant (found a tutorial but don’t understand the basics somehow)

What I got so far (code):

esphome:
  name: briefkasten
  friendly_name: Briefkasten

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "somekey"

ota:
  password: "somepassword"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Briefkasten Fallback Hotspot"
    password: "yetanotherpassword"

captive_portal:

deep_sleep:
  run_duration: 30s
  wakeup_pin:
    number: 2
    allow_other_uses: True
    inverted: True

binary_sensor:
  - platform: gpio
    pin:
      number: 2
      allow_other_uses: True
      id: "postbox_lid"
      inverted: True
      mode:
        input: True
        pulldown: True
    name: "Postbox Lid"


  - platform: homeassistant
    id: prevent_deep_sleep
    name: Prevent Deep Sleep
    entity_id: input_boolean.prevent_deep_sleep

Considerations to make the scenario easier:

  • I can live with the esp only waking up on opening the lid, sending the status once and going back to sleep. I suppose that makes managing the wake_up-pin-status easier to configure.

Any help with the code would be greatly appreciated. I really struggle with getting started with ESPHome.

(I can only add one picture for now…)

Welcome. We are all about mailbox/post box sensors. For your security you should not show your ssid and password or any keys. Edit them or ask Mods to delete them.

Welcome @OtterLinnus…

I haven’t used it personally, but for this feature it looks like you can use a list of wake-up pins in esp32_ext1_wakeup. You’d probably use ANY_HIGH and invert pins as required.

That should get you a wake up on “push”, which should be enough (at least for a momentary rather than latching button). Add the lid sensor too.

Could you elaborate a little on what you consider “activity”?

My initial thought is to use a template binary sensor with a delayed_off: to track activity. Your events of interest toggle it to be on. When it toggles off you trigger deep_sleep.enter.

You could also construct a timer of sorts from an uptime sensor and a global that resets on events.

There are plenty of examples around the forum for this. Search harder;)

Here’s one to get you started.

Thank you. The important stuff is hidden (the actual WIFI-Creds behind secret). The other stuff is bound to change anyways. But generally I get your point. Thank you for the reminder.

Of course. I imagine the post-person not using my postbox every 40 seconds. So I consider the ESP staying awake for about 30 seconds maybe a good period to start with. I view that as some kind of veeery long debouncing for the deep_sleep. :smiley:

Ah that’s a good one. I will check that later today and see if I can make sense of it. Thank you.

How good is your wifi strength out at your post box? Will you be putting the ESP inside or outside as this could have a big effect on the signal. Inside the box the signal may not be able to get out. Outside the box then you need a good enclosure as water will get in. We tend to make these as our post boxes are on the edge of our properties. The problem then comes when signal gets low and it fails to connect and goes into AP mode and never comes out of it without a physical power cycle, so you might want to set a very long ap_timeout.
The signal might seem good at times in the - 60 to -70db but when it slips in to the -80 to -90 db range it may go funny. An ESP with an external antenna can give much better signal.
You will have better connection and connection times if you set a static_ip address and fast_connect. These tend to be some of the problem that plagues people when it seems to be working but only intermittently.

1 Like

That is very good advice. I might consider going static. The postbox however is fortunately not too ar from my main router. I’d estimate about 5m and a front door. Would mqtt be the more stable choice? (Have not set up an mqtt brokeer with my HA-yellow, yet. Might consider doing so if this is the more stable design for ESP-devices.)

The ESP qill be inside the postbox. No external antenna so far. Connection was ok I’d say. Although…
Regarding the code above. Can anyone tell me why the ESP won’t wake up/connect anymore? Could that already be a version of the AP-problem as described above? Or did I code something entirely illogical to begin with?

If it has gone into AP mode you should be able to see it’s ssid name if you check with your phone. Always worth checking logs via esphome to see if it’s sending out anything. It’s a personal preference if you use API or MQTT but not both on same device.

But it won’t go into AP-mode if it is in deep_sleep, or do I interprete this function incorrectly?

It’s very hard to tell if it’s in deep sleep or in AP mode as neither will be connected to HA.

binary_sensor:
  - platform: gpio
    pin:
      number: 2
      allow_other_uses: True
      id: "postbox_lid"
      inverted: True
      mode: INPUT_PULLUP
    name: "Postbox Lid"	

Are you sure you need to pull down pin 2 and not pull up pin 2 ?

pullup
Pin 2 senses when voltage changes. It’s normally weakly pulled up and when press your button it hard pull the pin to ground and device knows button has been pressed.

I forgot there was an even easier way to wake from deep sleep without having to use code.
Another solution to wake from deep sleep

Is
Most people end up using a reed switch rather than a button. Is it possible in your setup that the postman after putting in the mail could set the lid on the button gently and not press the button down again?

When I did not include the pulldown, I got 100 messages in 2 secs. So I figured I should include it. Maybe pullup makes more sense? (tbh I don’t get this: “Pin 2 senses when voltage changes. It’s normally weakly pulled up and when press your button it hard pull the pin to ground and device knows button has been pressed.”)

For the “gentle post-person”: That’s why I wanted to do the deep_sleep based on time. The lid could even stay open if the letter was too large. So I only need an “OPEN!!” and then go back to sleep. But I thought it would be interesting to know if the lid closed in the same moment or if my letters are exposed.


Yes, we have all left a switch on the end of a long wire with a floating pin( i.e. a pin not pulled up or down) and suffered the consequences. I did it with a relay to a light and got a strobing effect. The long wire of your button will act as an antenna and pick up stray EM waves and pin 2 will sense the voltage and fire of the binary sensor. A twisted pair of wires may reduce the effect but really just need to pull the pin high or low.

Right. Sounds…sound :smiley: Should have thought about that. But it’s been some years since my last micro-device tinkering. So you say pullup is the way to go?

For the rest of the functionality…is there some code-example somewhere where I can borrow the basics? This ESPHome-YAML really leaves me in the dark so far. And I have successfully programmed Arduinos in the past :confused:

Mahko_Mahko’s code for doing it in API the deep sleep prevent for OTA updates. I do it with MQTT. MQTT way

Played around with inverted and pullup/-down. This is the only version with reliable feedback to HA:

esphome:
  name: briefkasten
  friendly_name: Briefkasten

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "~~~~~"

ota:
  password: "~~~~~"

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

binary_sensor:
  - platform: gpio
    pin:
      number: 2
      # allow_other_uses: True
      id: "postbox_lid"
      inverted: True
      mode:
        input: True
        pulldown: True
    name: "Postbox Lid"

Now, off to the deep_sleep implementation.

This seems to work:

esphome:
  name: briefkasten
  friendly_name: Briefkasten

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "~~~~~"

ota:
  password: "~~~~~"

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

deep_sleep:
  id: deep_sleep_1
  run_duration: 2minutes
  wakeup_pin:
    number: 2
    allow_other_uses: True
  wakeup_pin_mode: INVERT_WAKEUP

binary_sensor:
  - platform: gpio
    pin:
      number: 2
      allow_other_uses: True
      id: "postbox_lid"
      inverted: True
      mode:
        input: True
        pulldown: True
    name: "Postbox Lid"

  - platform: status
    name: "Briefkasten Status"

I am now monitoring the status to fire a “Postbox Moving”-notification.

You’re getting on well. For notification if you use the HA companion app on your phone is easy in the action setting of an automation. With the priority setting it will alert you even if phone is locked.

action:
  - service: notify.mobile_app_sm_yourphone
    data:
      message: TTS
      data:
        tts_text: You've got mail
        media_stream: alarm_stream_max
        tag: Alarm
        priority: high

Telegram is also good but little harder to setup.