AI Automation

ESP32 + MQTT + Home Assistant: DIY smart home, then let AI control it by voice

TL;DR

The ESP32 (built-in Wi-Fi) reads sensors and drives relays; ESPHome configures it in YAML without C++; data flows via MQTT or the native API into Home Assistant; automations run locally. The AI layer (Assist + an LLM) should only control explicitly exposed devices — never let AI decide on locks or gas.

Off-the-shelf smart homes are often locked into the vendor’s app, cloud-dependent and hard to customise. With a cheap ESP32, a few sensors, MQTT and Home Assistant, you get a system that runs locally under your full control — and can then add an AI layer to ask “how warm is the living room?” or “turn off every light downstairs” in natural language. This article walks through each layer, how to put it together and the safety considerations.

System layers

LayerTechnologyRole
DevicesESP32 + sensors (DHT22, PIR, door contacts…) + relaysRead data, switch loads
FirmwareESPHome (YAML) or Arduino/PlatformIOProgram the devices
MessagingMQTT (Mosquitto broker) or ESPHome native APIPublish/subscribe state and commands
OrchestrationHome AssistantDashboards, automations, schedules, history
AIAssist + an LLM integration (conversation agent)Commands and status questions in natural language

How MQTT works

MQTT is a lightweight publish/subscribe messaging protocol. Devices don’t call each other directly; they send messages to a broker (usually Mosquitto) on “topics”:

home/living-room/temperature   → 28.4   (ESP32 publishes)
home/living-room/light/set     → ON     (Home Assistant publishes a command)
home/living-room/light/state   → ON     (ESP32 confirms the state)

That makes adding devices easy: just publish/subscribe on the right topics. Home Assistant supports MQTT discovery — devices announce themselves and appear in the UI without manual configuration.

The fastest route: ESPHome

ESPHome lets you describe firmware in YAML instead of writing C++. A node that measures temperature/humidity and controls a light:

esphome:
  name: living-room
esp32:
  board: esp32dev
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
api:          # native connection to Home Assistant
ota:          # firmware updates over Wi-Fi
sensor:
  - platform: dht
    pin: GPIO4
    temperature: { name: "Living room temperature" }
    humidity:    { name: "Living room humidity" }
    update_interval: 60s
switch:
  - platform: gpio
    pin: GPIO26
    name: "Living room light"

Flash it over USB the first time, then update over Wi-Fi (OTA). To use MQTT instead of the native API, add an mqtt: block pointing at your broker.

Automations: keep the logic local

Important rules should be ordinary Home Assistant automations — running locally, independent of the Internet or AI:

  • Motion after 6 pm → hallway light on for 3 minutes.
  • Bathroom humidity > 75% → extractor fan on.
  • Door opens in “away” mode → push notification to your phone.

Adding the AI layer

Home Assistant includes Assist, a built-in voice/text assistant. You can configure an LLM as the conversation agent and choose exactly which entities are exposed to it. Then you can say:

  • “Which room is warmest?” — the AI reads sensor states and answers.
  • “Turn off all downstairs lights except the bedroom.” — the AI calls the matching services.
  • “What time did the front door open last night?” — a history query.

Home Assistant can also act as an MCP server so AI clients (such as Claude Desktop) can control the house over a standard protocol — still limited to the entities you allow.

Safety — don’t skip this

  • Mains voltage: use opto-isolated relay modules in an enclosed box with properly rated wiring. If in doubt, hire an electrician.
  • Don’t expose everything to AI: door locks, alarms and cookers should require confirmation or be excluded from AI control.
  • Network security: password-protect the MQTT broker; don’t expose Home Assistant directly to the Internet — use a VPN or an authenticated remote-access service.
  • Critical logic runs locally: AI and the Internet can be slow or down; important automations shouldn’t depend on them.

Suggested roadmap

  1. Install Home Assistant (Raspberry Pi or mini PC) plus the Mosquitto add-on.
  2. Build one ESPHome temperature node — no mains wiring yet.
  3. Write 2–3 useful automations.
  4. Add relays for low-voltage devices, or use ready-made smart plugs.
  5. Only then enable Assist + an LLM with a restricted entity list.

How to apply it

  • Developers: A great weekend project to learn IoT, MQTT and LLM integration with real systems — with immediate physical feedback.
  • Tech Leads: The “solid local logic + AI as the interface layer” architecture applies to business systems too: AI shouldn’t be a single point of failure.

Sources & references

  1. Home Assistant and ESPHome: MQTT integration — DEV Community
  2. Create a personality with AI (Assist) — Home Assistant