Door lock detector based on analog hall effect sensor

I have done quite a few things with Arduino and ESP8266 but I’m quite a noob with ESPHome.

I’m making a door lock position sensor. Detection is based on hall sensor in the analog pin of esp8266 and it outputs as resistance about 0.74 V / 1600 Ω (750 RAW) when unlocked and less than 0.4 V / 800 Ω (400 RAW) when locked. How could I output that as binary locked value?

I also have a status led that I’d like to turn on if the door is unlocked. I tested that as switch but it should probably be output since it does not need to be switched by user. So how do I hook that with the sensor value?

Many thanks if someone more knowledgeable could help me a bit! :blush:

Currently the config looks like this:

esphome:
  name: esp-side-door

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "3ef4hiZ8gQbOfbvur9CDAIfBDdQ4bSDM3of4IagOpUM="

ota:
  password: "c1488287f6ed561d8ad184b27f6259f9"

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

  ap:
    ssid: "Door Fallback Hotspot"
    password: "ue1MRs75mgl1"

captive_portal:

sensor:
  - platform: resistance
    sensor: hall_sensor
    configuration: DOWNSTREAM
    resistor: 5.6kOhm
    name: Lock Sensor

  - platform: adc
    id: hall_sensor
    update_interval: 5s
    pin: A0
    #raw: true

switch: #change to output
  - platform: gpio
    pin:
      number: GPIO4  #D2
    name: "Warning Light"

Lock state sounds like a job for a template binary sensor, which would use your hall_sensor.

You could set up the light a few ways. Output is fine. Or mark the switch as internal.

To interlock the light with the lock, use on_press and on_release actions to switch it on/off.

You may also be interested in the door lock sensor here.

Thanks! Got it working. Here’s my config if it helps someone else.

sensor:
  - platform: resistance
    sensor: hall_sensor
    configuration: DOWNSTREAM
    resistor: 5.6kOhm
    name: Lock Sensor

  - platform: adc
    id: hall_sensor
    update_interval: 1s
    pin: A0
    raw: true

binary_sensor:
  - platform: template
    name: "Door Open"
    lambda: |-
      if (id(hall_sensor).state > 500) {
        return true;
      } else {
        return false;
      }    
    on_press:
      then:
        - output.turn_on: warning_led
    on_release:
      then:
        - output.turn_off: warning_led

output:
  - platform: gpio
    pin:
      number: GPIO4  #D2
    id: warning_led
1 Like