D1 mini, relay and reed sensors. REED sensors do not change status of door

Hi, still new to this game but I am having trouble trying to get the reed sensors to change the status of the door to open or closed when the magnetism energise s(closed door) or deenergises (open door).

The code I have is:

esphome:
  name: garage-door

esp8266:
  board: esp01_1m


# Enable logging
logger:

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

ota:
  password: ""

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

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

captive_portal:
    

binary_sensor:
  - platform: gpio
    pin: GPIO4
    name: "Garage Door Contact Sensor"
    id: contact_sensor
    #internal: true
    filters:
    - invert:
       #Debounce the contact sensor to prevent rapid on/off/on events
    - delayed_on_off: 100ms

switch:
  - platform: gpio
    pin: GPIO5
    name: "Garage Door Relay"
    id: relay
    internal: true

cover:
  - platform: template
    device_class: garage
    name: "Garage Door"
    id: template_cov
    open_action:
      - switch.turn_on: relay
      - delay: 0.5s
      - switch.turn_off: relay
    close_action:
      - switch.turn_on: relay
      - delay: 0.5s
      - switch.turn_off: relay
    lambda: |-
     if (id(contact_sensor).state) {
        return COVER_OPEN;
      } else {
        return COVER_CLOSED;
      }

Any help gratefully received, I have been trawling the www, and changing pins all day.

Thanks in advance

The Newbie of the community - and yes I am slowly learning coding but could be doing with your help.

I use a shelly 1 for my garage door.

Everything you have looks pretty good, except you don’t specify a value for invert of your binary sensor.

For the shelly 1, the pins are the opposite

substitutions:
  friendly_name: garage3

esphome:
  name: garage3
  platform: ESP8266
  board: esp01_1m

# The door contact sensor that is attached to SW on the 
# Shelly 1. Not exposed to HA, instead used to set the 
# state of the cover.
binary_sensor:
  - platform: gpio
    pin: GPIO5
    name: $friendly_name Contact Sensor
    id: contact_sensor
    internal: false
    filters:
      - invert:

# The relay in the Shelly 1 that will deliver the pulse to
# the gate opener (not exposed to HA)
switch:
  - platform: gpio
    pin: GPIO4
    name: $friendly_name Relay
    id: relay
    internal: true
# This part allows remote reset of the device
  - platform: restart
    name: $friendly_name Relay Reset
    id: restart_switch
    
# This creates the actual gate in HA. The state is based
# on the contact sensor. Opening/closing the gate simply
# turns the relay on/off with a 0.5s delay in between.
cover:
  - platform: template
    device_class: garage
    name: HoonTune Farm $friendly_name
    id: template_cov
    lambda: |-
      if (id(contact_sensor).state) {
        return COVER_OPEN;
      } else {
        return COVER_CLOSED;
      }
    open_action:
      - switch.turn_on: relay
      - delay: 0.5s
      - switch.turn_off: relay
    close_action:
      - switch.turn_on: relay
      - delay: 0.5s
      - switch.turn_off: relay
    stop_action:
      - switch.turn_on: relay
      - delay: 0.5s
      - switch.turn_off: relay      

I am still new also, but a possible problem could be the way the reed switch is connected.

Is the pin pulled to vcc or ground when the reed switch is near a magnet? Does the pin have a pull-up or pull-down resistor set? I normal set a pull-up on the pin and then have the reed switch pull the pin to ground when the magnet is near it.

binary_sensor:
  - platform: gpio
    pin: GPIO4
      mode:
        input: true
        pullup: true

The board you are using does not have internal software enabled pull-up/down resistors, so you have to use physical resistors, the value is not that important, any value between 10K Ohm to 100K Ohm will do.

You have two options for wiring the switches, using a pull up or a pull down resistor:

Reed switches are normally closed (not open as shown in the diagrams). And binary sensors turn on when the GPIO has 3.3V on it. So you most likely want Option 1. Though the option you choose isn’t that important. If you find you have chosen the wrong one you can easily invert the signal in software.

1 Like

Thanks

Can I ask for some education on why DIY Smart Garage Door Opener + Home Assistant Integration : 5 Steps - Instructables seems to work?

I am not doubting anyone at all.

Ps just starting on esp language and don’t have a clue what the programming for this is doing.

Thanks everyone, was having little success and then by chance tried another REED and it all worked.

Back to Toolstation with the faulty one then!