Home Assistant and ESPHome devices on an ESP32
Intermediate
Or see every lesson.
- 261parts on the bench
- 31boards running now
- 1.00xreal time, on every board
Most of the devices in a homemade smart home are small: a board that reads a few sensors in a room, a relay that presses a button, a strip of LEDs that fades. An ESP32 runs all of them, and ESPHome has made them easy to flash without writing code. What is still hard is the part YAML hides: which pin a sensor can use, why a reading is nonsense, why a light flickers at dusk.
The circuit above is a room multisensor on an ESP32-C3 in Mokxi: a DHT22 for temperature and humidity, a BH1750 for light and a PIR for motion. Press Run, click the PIR, and the Serial Monitor shows each reading as the message the board would publish. Open the Web tab and the same values come back as JSON from the board’s own server.
Two things up front, because they matter. Mokxi runs Arduino sketches, not ESPHome, so each device here does by hand what the ESPHome components would, and the help page shows the matching YAML for when you flash a real board. And the network is simulated, with no MQTT broker and no way out to your real Home Assistant: the board prints what it would send instead of sending it.
Want the step-by-step version? The lesson "Start the Home Assistant devices course" walks through this with checkpoints.
Open the lessonFive devices people build first
Mokxi has a template for each: the multisensor; a radar hallway sensor that keeps the light on for ten quiet seconds and only switches it in the dark; a garage door opener with a relay across the wall button and two reed switches; a WS2812 strip controller with on, off, brightness, color and effects that fade; and a plant sensor that turns a capacitive soil probe into a calibrated percentage with a thirsty alert.
Each one is wired the way you would build it, with the sensors that talk to the board’s pins on 3.3 V and the modules that need 5 V, such as a PIR or a radar board, on their own rail. A 5 V pull-up on a 3.3 V pin is one of the easiest ways to damage an ESP32, and a breadboard in the simulator is a safe place to learn where each wire goes.
Motion is not presence
A common question in Home Assistant forums is why a motion sensor turns the lights off on someone reading on the couch. A PIR and the RCWL-0516 microwave radar both see movement, not people. The 24 GHz mmWave boards sold for presence, such as the LD2410, also sense someone sitting still, and they talk over a serial connection.
Mokxi models the RCWL-0516, so the radar template uses the trick everyone uses with a motion sensor: an off delay. The hallway counts as occupied the moment anything moves, and only clears after ten quiet seconds. ESPHome calls the same idea a delayed_off filter. The template is honest about the limit: sit perfectly still long enough and the light goes out.
The garage door, safely
The garage door opener is the most popular build and the one with the most at stake. The safe pattern is the one ESPHome’s own garage door example and most community guides follow: a relay wired across the opener’s low-voltage wall-button terminals, closed for about half a second, never wired to mains or the motor, and one or two reed switches so the device knows where the door really is.
The relay must boot off and never stay on, and nothing should bypass the opener’s photo-eyes or its reversal when the door meets something. The template writes the relay pin low before making it an output so it cannot click at power-up, ignores a second press within a second and a half, and reports a problem if the door has not reached a switch in time.
digitalWrite(RELAY_PIN, HIGH); // the relay closes across the button
pressedAt = millis();
// ...and in loop(), half a second later:
if (pressing && millis() - pressedAt >= PRESS_MS) {
digitalWrite(RELAY_PIN, LOW);
}From the simulator to ESPHome
When the circuit behaves, the move to real hardware is usually ESPHome. Each template’s help page lists the YAML that does the same job: a dht and a bh1750 sensor and a gpio binary sensor for the multisensor, a template cover with device_class garage for the door, an esp32_rmt_led_strip light for the strip, and an adc sensor with calibrate_linear for the plant probe. Reading the sketch next to the YAML is a good way to understand what each component does underneath, which is what you need the day a reading goes wrong.
The Home Assistant devices course walks through all five, one lesson each, with a check at every step that reads the running simulation. It starts with the multisensor, because every later device reuses its pattern: read a part, decide when a change is worth reporting, publish it, and serve the same state to anything that asks.
Questions
Does Mokxi run ESPHome?
No. Mokxi runs Arduino sketches on a simulated ESP32-C3. Each template does by hand what the ESPHome components would, and its help page shows the matching YAML for when you flash a real board.
Can the simulated board talk to my Home Assistant?
No. The network is simulated and stays inside the browser tab, with no MQTT broker. The sketches print the messages they would publish and serve their state as JSON to the Web tab.
Is the RCWL-0516 an mmWave presence sensor?
No. It is a microwave Doppler sensor that sees movement. mmWave boards such as the LD2410 can also sense someone sitting still; Mokxi does not model them.
How should a garage door opener be wired?
Across the opener’s low-voltage wall-button terminals with a relay that boots off and closes briefly, never to mains or the motor, keeping the opener’s photo-eyes and reversal working.
Keep going
Where the facts on this page come from
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.