Door sensor

This was my first project. It runs on ESPHome on a D1 Mini (based on ESP8266), which is integrated into my Home Assistant setup through WiFi. The sensor is a hall effect sensor and responds to magnetic fields. In this application the board sends a notification to my mobile every time the magnet is moved. The 3D models were developed in Onshape.

The main part.
The sensor

How it works

The hall sensor value is read continuously through the ADC. A binary sensor then turns the analog voltage into a clean open/closed state, which also drives a status LED. Home Assistant listens for that same state and sends the notification to my mobile when it changes.

# Read the analog value from the hall sensor on A0
sensor:
  - platform: adc
    pin: A0
    name: "Hall Sensor Raw"
    id: hall_sensor
    update_interval: 1s
    filters:
      - multiply: 3.3

# Turn the analog reading into a clean on/off state at a fixed threshold
binary_sensor:
  - platform: analog_threshold
    sensor_id: hall_sensor
    threshold: 2.9
    id: hall_sensor_binary
    name: "Hall Sensor"
    # Debounce: ignore state changes shorter than 200ms (e.g. during boot)
    filters:
      - delayed_on: 200ms
      - delayed_off: 200ms
    on_state:
      then:
        - if:
            condition:
              binary_sensor.is_on: hall_sensor_binary
            then:
              - switch.turn_off: led_relay
            else:
              - switch.turn_on: led_relay

# LED on D5 reflects the current door state
switch:
  - platform: gpio
    name: "LED"
    pin: D5
    id: led_relay

The analog_threshold platform converts the raw ADC voltage into a binary “magnet present or not” reading at a fixed threshold. The delayed_on and delayed_off filters debounce that reading, so a brief voltage dip, for example right at boot, doesn’t toggle the state by mistake. The board only exposes this clean state. The actual notification is handled by an automation in Home Assistant instead.

Images

Picture of the setup
Overview
Picture of the setup without the lid.
Without the lid
Picture of the finished battery pack.
Backside of the PCB prototype