First post relay code help

I have a d1 mini configured to a reed sensor; copy/pasted from another project. I am trying to modify the project so the sensor will power a relay. I have tried many combinations but can not get the correct code. I did have the switch showing in home assistant but it did not control the relay so I removed the code.

What is the code for the switch and then how does the switch power the relay on/off with the status of the binary_sensor?

 esphome:
  name: esphome_freezer_door
  platform: ESP8266
  board: d1_mini
 
wifi:
  ssid: 'ssid_here'
  password: 'password_here'
  fast_connect: true
  manual_ip:
    static_ip: 192.168.1.145
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    dns1: 192.168.1.1
    dns2: 192.168.1.1
  
api:
 
ota:
 
web_server:
  port: 80
 
logger:
 
binary_sensor:
- platform: gpio
  pin:
    number: GPIO13
    mode: INPUT_PULLUP
    inverted: False
  name: "Freezer Door"
  device_class: door
  filters:
    - delayed_on: 20ms

The binary_sensor is working correctly, just need to have it control the relay. The relay is stacked on the D1 mini.

The switch should be named “Freezer Light” and will be GPIO5. I want it to turn on when the binary_sensor: “Freezer Door” opens and remain off when the binary_sensor: “Freezer Door” is closed.

Hope this makes sense, any help appreciated.

Put your Switch/relay code back. Give it id: relay

Then update your binary sensor like this:


binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: False
    name: "Freezer Door"
    device_class: door
    filters:
      - delayed_on: 20ms
    on_press:
      - switch.turn_on: relay
    on_release:
      - switch.turn_off: relay

thanks, would you mind double checking the switch code? all seems to be correct as far as the switch operation in home assistant however the relay is not responding. If the code looks correct I will check and see if maybe there is a faulty solder connection?
thanks again, very much appreciated.

esphome:
  name: esphome_freezer_door
  platform: ESP8266
  board: d1_mini
 
wifi:
  ssid: 'ssid_here'
  password: 'password_here'
  fast_connect: true
  manual_ip:
    static_ip: 192.168.1.145
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    dns1: 192.168.1.1
    dns2: 192.168.1.1
  
api:
 
ota:
 
web_server:
  port: 80
 
logger:
 
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: False
    name: "Freezer Door"
    device_class: door
    filters:
      - delayed_on: 20ms
    on_press:
      - switch.turn_on: relay_1
    on_release:
      - switch.turn_off: relay_1

switch:
- platform: gpio
  id: relay_1
  pin:
    number: GPIO5
    inverted: False
  restore_mode: ALWAYS_OFF
  name: "Freezer Light"
  icon: "mdi:light"

Looks ok. The indentation should be like the binary sensor above but that’s only a style thing.

Thank You!!!
I will check the relay and power supply.