ESPHome Smart Mailbox

Needed some help to developed a smart mailbox using a d1 mini, esphome and home assistant. Got different examples on the internet, but none with esphome. I got the ground connected to RST with a switch to wake up the d1 mini from deep sleep, when the mailbox lid is open. But need to create a sensor that will turn on and off state for the mail box and send the info to home assistant.

Below is an example that I started creating, but template already doesn’t support the on_boot

### MQTT information
mqtt:
  broker: XXX.XXX.X.XX
  discovery: true
  username: XXXXXXX
  password: XXXXXXX

### Mailbox open and close notification
sensor:
  - platform: template
    icon: "mdi:mailbox"
    name: "Mailbox Lid"
    on_boot:
        - if:
            condition:
              wifi.connected: true
            then:
              - mqtt.publish: "ON"
              - delay: 700ms
              - mqtt.publish: "OFF"

### Battery Monitoring  
  - platform: adc
    icon: "mdi:battery"
    pin: VCC
    filters:
      - multiply: 1.175
    name: "Mailbox VBatt"
    unit_of_measurement: "V" 
    update_interval: 15s
              
### Mailbox Wifi information
  - platform: wifi_signal
    name: "Mailbox WiFi Strength"
    update_interval: 15s

  - platform: uptime
    name: Mailbox

### DeepSleep
deep_sleep:
  run_duration: 60s

Please read point 11 here then edit your post.

Thanks, :ok_hand:

You’re using mqtt instead of the api which is good. The api isn’t the best for deep sleep applications.

You don’t need to publish anything on boot. If your sensor has changed it will be sent to home assistant and from there you can automate a notification.

Catching the state change can be tricky on boot if your mail-man is as fast as mine. So the delay/on/off may be of some use.

Here’s my mailbox ESP config to show you what I mean. It has two sensors (door and lid) which I use in home assistant to determine full / empty. These are commoned to another input using diodes as when I wrote this only one wake input was available for the ESP32. None of this is relevant to you as you are using an ESP2866 board that only supports one wake input and you only have one input anyway. Just thought I’d clarify the code. The important bit is the wake time, what happens if a sensor (lid or door ) is left open (it goes to sleep) and the delay times for debouncing the switch and allowing for the wake up to capture the switch transition.

esphome:
  name: mailbox
  platform: ESP32
  board: nodemcu-32s

wifi:
  ssid: 'WAPLO'
  password: !secret wifi_pwd
  manual_ip:
    static_ip: 10.1.1.77
    gateway: 10.1.1.1
    subnet: 255.255.255.0

logger:
  level: WARN

mqtt:
  broker: 10.1.1.100
  username: !secret mqtt_usr
  password: !secret mqtt_pwd

ota:
  password: !secret esp_pwd

deep_sleep:
  run_duration: 2min #### <---- overkill, takes about 45sec to wake up and report in
  sleep_duration: 720min  ### <---- report in twice a day
  wakeup_pin_mode: INVERT_WAKEUP ### <----- go to sleep even if the door or lid is left open
  wakeup_pin: GPIO12

sensor:
  - platform: wifi_signal
    name: "Mailbox WiFi Signal"  ### <---- get a signal strength reading on wake, but most of the time 'unknown'
    update_interval: 4s
    filters:
      - sliding_window_moving_average:
          window_size: 15
          send_every: 15

  - platform: adc
    pin: GPIO36
    name: "Mailbox Battery" ### <---- This turned out to not be useful as my Li Bat to 5V power supply is low side switched so the battery voltage always looks like 4.2V
    attenuation: 6db
    unit_of_measurement: "V"
    update_interval: 4s
    filters:
      - multiply: 1.89
      - sliding_window_moving_average:
          window_size: 15
          send_every: 15

binary_sensor: 
  - platform: status
    name: "Mailbox Status"  ### <--- useful to make sure the device wakes at least twice a day or HA alerts me

  - platform: gpio
    pin:
      number: GPIO25
    name: "Mailbox Lid"
    device_class: door
    filters:
      - delayed_on: 100ms ### <---- debounce time
      - delayed_off: 10s ### <---- delay to capture state change on wake up

  - platform: gpio
    pin:
      number: GPIO26
    name: "Mailbox Door"
    device_class: door
    filters:
      - delayed_on: 100ms  ### <---- debounce time
      - delayed_off: 10s ### <---- delay to capture state change on wake up

  - platform: gpio
    pin:
      number: 12
      mode: INPUT_PULLDOWN
    name: "Mailbox Wake Pin"

Screenshot_2019-10-05%20Home%20Assistant(1)

Thanks for your feedback and appreciated it. Yes when researching, I read about the api isn’t ideal for deep sleep, as I implemented a septic level detector (ultrasonic sensor) running on D1 mini with mqtt, battery and a solar panel.

If I understood correctly, adding the filter to the platform, I can capture the information correctly for the automation.

### MQTT information
mqtt:
  broker: XXX.XXX.X.XX
  discovery: true
  username: XXXXXXX
  password: XXXXXXX

### Mailbox open and close notification
binary_sensor:
 - platform: gpio
   pin:
    number: GPIO16
   name: "Mailbox Door"
   device_class: door
   filters:
     - delayed_on: 100ms  ### <---- debounce time
     - delayed_off: 10s ### <---- delay to capture state change on wake up

### Battery Monitoring  
sensor:
  - platform: adc
    icon: "mdi:battery"
    pin: VCC
    filters:
      - multiply: 1.175
    name: "Mailbox VBatt"
    unit_of_measurement: "V" 
    update_interval: 15s
              
### Mailbox Wifi information
  - platform: wifi_signal
    name: "Mailbox WiFi Strength"
    update_interval: 15s

  - platform: uptime
    name: Mailbox

### DeepSleep
deep_sleep:
  run_duration: 60s

As I had the issue when mimicking the delivery of the postman, as when the D1 connected the mailbox state was closed already.

Did you use retain option to remember the mqtt state, as I see in the screenshot it’s showing unavailable.

Yes. That’s what the filter will do.

Yes I did use retain (it’s the default so does not have to be stated) .

They are unavailable is because of the LWT messages.

Supposedly it is possible to override this behaviour by setting blank birth and will message topics.

I tried this once when I didn’t know much about mqtt and could not get it to work. Now I realise it was because of retained LWT topics that needed to be deleted from the broker. I may get around to fixing this one day. But I would have to undo a fair bit of ‘workarounds’ first.

Hi @tom_l

Do you have an schematic? and the components? I mean the values of Resistors and diode´s model

I would like to make my mailbox smart using an esphome smart mailbox like yours

Thanks in advance

No just the proto-board layout above.

All 100K and 1N4148 but any small signal diode (or even rectifier diode) will work.

Hello, unclear what the right connections are in this image. Could you please post/sent a clear schematic ? Many thanks !!!

I did not make a schematic and am not going to. Just copy the proto-board layout.

Hi, I understand but maybe you could clarify some things ? On the proto board the + of the battery seems to be connected to the CMD Pin ? Or is the proto board seen from underneath ? I have difficulty finding the right pins. Really would appreciate a little help …

Seen from above.

Edit: deleted incorrect board.

Super ! Thanks a lot !!!

Hang on, That’s not the right board. This is:

Ok, trying to find a Eagle Cad library for this one …

You do realise that most of that circuit has been made redundant by ESPHome now supporting more than one wake pin on the ESP32?

No I did not … So you are saying that f.i. the LID sensor can be a wake pin whilst the DOOR sensor could just be another wake pin, both able to wake the ESP32 from deep sleep ?

Correct. This was introduced after I developed the circuit to OR the lid and door with diodes to a single wake pin.

Advanced features:

  • esp32_ext1_wakeup ( Optional ): Use the EXT1 wakeup source of the ESP32 to wake from deep sleep to wake up on multiple pins. This cannot be used together with wakeup pin.
    • pins ( Required , list of pin numbers): The pins to wake up on.
    • mode ( Optional ): The mode to use for the wakeup source. Must be one of ALL_LOW (wake up when all pins go LOW) or ANY_HIGH (wake up when any pin goes HIGH).

Deep Sleep Component — ESPHome

the only caveat is that they have to be RTC pins:

RTC_GPIO0 (GPIO36)
RTC_GPIO3 (GPIO39)
RTC_GPIO4 (GPIO34)
RTC_GPIO5 (GPIO35)
RTC_GPIO6 (GPIO25)
RTC_GPIO7 (GPIO26)
RTC_GPIO8 (GPIO33)
RTC_GPIO9 (GPIO32)
RTC_GPIO10 (GPIO4)
RTC_GPIO11 (GPIO0)
RTC_GPIO12 (GPIO2)
RTC_GPIO13 (GPIO15)
RTC_GPIO14 (GPIO13)
RTC_GPIO15 (GPIO12)
RTC_GPIO16 (GPIO14)
RTC_GPIO17 (GPIO27)

RTC PINS ? Real Time Clock ? You mean like GPIO0, GPIO1 … etc in this case ? Oh meanwhile you already answerred it … :slight_smile:

Thanks for your help !!!

Yes real time clock pins, note GPIO1 isn’t one.