Door sensor with ESP32 + ESPHome + reed switch, free over MQTT

Know from your phone whether the cold-room door or the warehouse gate was left open, with an email alert. Free on EzGYM.

The ESP32 + ESPHome + reed switch reads a magnetic sensor (reed switch) on the door and publishes over MQTT when it opens and closes. EzGYM gives you the server for free, with a username and password for each device.

You set how many minutes it can stay open: if the cold-room door is still open after 5 minutes, we email you, and again when it closes.

Depending on how the sensor is wired, the device may report “on” with the door closed. The sensor page has an “Invert” switch so it says Open and Closed correctly.

How to set it up

  1. 1 Create your free EzGYM account, go to Sensors and add a “Door or gate”.
  2. 2 Write down the server, username, password and topic the app gives you.
  3. 3 Wire the magnetic sensor between GPIO4 and GND of the ESP32, and add the block below with your data to the ESPHome YAML.
  4. 4 Install the firmware on the ESP32 from ESPHome.
  5. 5 Open the door and check that EzGYM says Open; if it says Closed, turn on Invert in the sensor page.
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO4
      mode: INPUT_PULLUP
    name: porton
    device_class: door

sensor:
  - platform: wifi_signal
    name: senal
    update_interval: 60s

mqtt:
  broker: devices.ezdis.net
  port: 1883
  username: sn4k2p9x7q1m
  password: TheSensorPassword
  topic_prefix: s/sn4k2p9x7q1m
  discovery: false

In this example, sn4k2p9x7q1m is the sensor username and s/sn4k2p9x7q1m its topic: replace them, and the password, with the ones the app gave you when you added the sensor. They are not your app login.

Frequently asked questions

Which door sensor do I use?

Any two-wire magnetic sensor (reed switch), like the ones used in alarms. It goes between an input of the device and GND.

Which username and password do I enter in the ESP32 + ESPHome + reed switch?

The sensor ones, not your account ones: when you add the door in Sensors, EzGYM shows its username (it starts with “sn”) and its password. The password is shown only once.

Does it warn me if the device disconnects?

Yes, if you turn it on in the sensor page. It works well with ESPHome and Tasmota from this guide, because they send a signal every minute even if the door does not move.

How much does it cost?

Nothing: the broker and the email alerts are free, up to 30 sensors per account.

More guides

Create a free account