Best way to connect custom sensor board to HA

I’m a beginner with HA scripting.
I made a custom PCB that connects an ESP32 to a magnet switch and has a relay to operate my garage door. I had no trouble setting up buttons in HA to open/close the door using MQTT but I am at a loss as to how to get notification of the door state. First I want to have an indicator that shows the open/closed state. Ultimately I want to send a notification (email for example) if the door is left open after a certain time. I can make the ESP32 code publish and subscribe whatever MQTT messages work best but not sure what that should be. I’m open to using Node Red if it would be simpler.

Hi there, by magnetic switch, do you mean a reed switch which you use a door open or close sensor? If so you can use the mqtt binary sensor to detection the door position like below.

  - platform: mqtt
    name: "Window Contact Sensor"
    state_topic: "home-assistant/window/contact"
    payload_on: "on"
    payload_of: "off"
    device_class: door

Make sure the payload for on and off are correct, also the state topic

That’s part of what I want but please explain further.
How should the sensor publish? Once each time the sensor changes state, every few seconds, or does HA publish a request for sensor state then waits for a response?

HA will subscribe to the state topic which is mentioned in binary sensor config. The sensor at the door should be configured to sent specific payloads for open and close to the same topic like on for open and off for close. When HA recieves a payload through the mqtt state topic, it would check if the payload is same as either payload_on or payload_off and select corresponding states.

There is no need for a mqtt request to be made towards the door for publishing the output.

What firmware are you using, tasmota or esphome or anything custom? I would like to recommend you to use esphome which will simplify all this setup but if you want UI based settings and all go for tasmota.

I think that answers my question. I am fairly comfortable writing code for the ESP32 but HA yaml file structure and syntax is very confusing to me!
Not sure how esphome or tasmota could handle my custom PCB. I have left out some details. The ESP32 plugs into the PCB which has relays and inputs for 2 garage doors. Relays are “hotwired” to the door switches, inputs wired to magnetic reed switches such that inputs go high when the doors are closed.

You are use both esphome and tasmota with ease with this PCB. You just have to now what are the pins engaging the relays and what are connected to the sensors. I recommend you go with any of the above firmware as they have native support with HA and also reliable.

If I may I would recommend you to try esphome as it is no doubt more powerful than tasmota while tasmota is user friendly in all manners. As you said Esphome yaml could be a little difficult at first, then try tasmota, there is no coding, everything can done through UI. Moreover if you want to use alexa voice assitant with this, tasmota has native support.

If you need help with any of these do ask.

Not interested in Alexa.
How would esphome or tasmota handle the relays? The garage door switches are momentary so the relay pin needs to go high for 150ms then go low.

Well, sounds like the Endstop Cover in ESPHome is pretty much gonna be perfect for you :slight_smile:

The relay configuration part of esphome will look like below

  - platform: gpio
    name: "Back Light"
    id: relay
    pin: GPIO12

Here I have connected my relay to the GPIO 12. Like this you can mention all your relays. The sensor for opening and closing can be deployed as below.

  - platform: gpio
      number: 0
      inverted: True
    name: "Relay Sensor"
    internal: true
    id: relay_sensor
        - switch.turn_on: relay
        - switch.turn_off: relay

Here my sensor is connected GPIO 0 and also I have specified what to do when the relay is on with on_press and off with on_release

To clarify, the relay GPIO needs to turn on then off quickly. Time delay of 0.15 sec (150 milliseconds)
The relay is in parallel with the mechanical switch that came with the garage opener. I am simulating a person pushing the button.

Ok, got the basics working. Thanks for the input!
I already have my c++ code working so no need for ESPHome or Tasmota. Nothing against them but they are best for off the shelf devices. I have a couple of temperature sensors with ESPHome and they work great!