IoT automation means letting sensors, rules and actuators handle the routine decisions in a physical space, from switching off lights in an empty office to opening an irrigation valve when the soil is dry. This guide gives you one architecture that works for homes, buildings, farms and energy systems, shows working Home Assistant and Python automations over MQTT, and explains how to keep the whole thing reliable and secure.
What IoT Automation Is and Why MQTT Sits at the Center
Every automation, no matter how complex, follows the same loop: sense, decide, act, confirm. A sensor measures something, a rule decides whether action is needed, an actuator does the work, and a state report confirms that it happened.
Traditional automation systems built this loop inside one vendor's controller. That works until you want a Zigbee motion sensor to control a Wi-Fi relay, or a solar inverter's output to decide when a water heater runs. IoT automation breaks the loop into independent parts that talk through a common messaging layer, and that layer is almost always MQTT.
MQTT fits because:
- Publish/subscribe decouples devices from logic. A sensor publishes once; any number of rules, dashboards and loggers can subscribe.
- It is light enough for microcontrollers like the ESP32 and ESP8266, and supported natively by Tasmota, ESPHome, Zigbee2MQTT, Shelly devices and Home Assistant.
- Retained messages let a rule engine that restarts know the current state of every device immediately.
- Last Will messages announce when a device goes offline, so automations can fail safe.
If the broker is new to you, start with what an MQTT broker does, then come back here.
The Four-Layer IoT Automation Architecture
The same structure applies whether you are automating a flat, a 20-storey office building or a greenhouse.
| Layer | Role | Common tools |
|---|---|---|
| Devices | Sense and act: sensors, relays, valves, meters, thermostats | ESP32 with ESPHome, Tasmota, Shelly, Zigbee via Zigbee2MQTT, LoRaWAN nodes |
| Messaging | Carry state and commands between everything | MQTT broker, local or cloud |
| Logic | Decide what to do: rules, schedules, scenes | Home Assistant, Node-RED, openHAB, Python services |
| Interface | Show state, let people override | Dashboards, mobile apps, voice assistants |
Keeping these layers separate is what makes the system maintainable. You can replace a Zigbee sensor with a Wi-Fi one, or move rules from Home Assistant to Node-RED, without rewiring the rest, as long as the topics stay the same.
Local broker, cloud broker or both?
A broker on a local Raspberry Pi keeps automations working when the internet is down, which matters for lights and heating. A cloud broker makes devices reachable from anywhere, connects sites that are not on the same network (a farm with several fields, a company with several buildings), and removes the job of patching and backing up a broker yourself. Many installations combine both: time-critical rules run locally, while monitoring, remote control and multi-site coordination go through a cloud MQTT broker. The Home Assistant with an external MQTT broker tutorial walks through that setup.
Topic Design for Automation
Clear topic names make rules readable and prevent accidental cross-talk between devices. A practical layout:
<site>/<area>/<device>/<channel>
<site>/<area>/<device>/<channel>/set (commands)
home/livingroom/thermostat/temperature
home/livingroom/thermostat/setpoint/set
office/floor3/zone-b/occupancy
farm/greenhouse1/soil/moisture
farm/greenhouse1/valve/state
energy/house/solar/power
Rules of thumb that save hours of debugging:
- Separate state topics (what the device reports) from command topics (what you ask it to do), typically with a
/setsuffix. - Publish state as retained so dashboards and rule engines see it instantly after a restart.
- Use JSON payloads with units and timestamps when a device reports several values.
- Give every device a
statustopic with a Last Will ofoffline.
The smart home MQTT topic structure guide goes deeper, including how to handle groups and scenes.
IoT Automation by Environment
The architecture stays constant, but each environment has its own priorities.
Homes
Home automation is the most mature area. Home Assistant is the usual logic layer, with MQTT bridging devices from many ecosystems: Zigbee sensors through Zigbee2MQTT, Wi-Fi plugs flashed with Tasmota, and custom ESP32 boards built with ESPHome. Typical automations are presence-based lighting, heating schedules, leak alerts and notifications. A common pitfall is relying on the cloud for things that must work offline, such as a light switch; keep those local.
Commercial buildings
Building automation focuses on HVAC, lighting, occupancy and air quality, usually across many zones. Existing systems speak BACnet, KNX or Modbus, and gateways translate them into MQTT so newer applications can use the data without touching the building management system. Read building automation with MQTT for the architecture and BACnet to MQTT for the gateway step. Occupancy-driven HVAC and lighting are the classic quick wins because empty rooms are easy to detect.
Farms and greenhouses
Agricultural automation deals with long distances, harsh conditions and battery-powered sensors. Soil moisture, air temperature, humidity and water levels drive irrigation, ventilation and shading. LoRaWAN is common in the field, with network servers such as The Things Network forwarding uplinks over MQTT, as covered in The Things Network and MQTT. A cloud broker is particularly useful here because fields and greenhouses rarely share a local network. The smart agriculture IoT guide and the greenhouse automation tutorial cover the details.
Energy
Energy automation balances generation, storage and consumption: running a water heater when solar output is high, charging an EV off-peak, or shedding non-critical loads. Inverters, batteries and smart meters often expose Modbus or local APIs that small bridges publish to MQTT. See home energy monitoring with MQTT and solar inverter monitoring for concrete builds.
Hands-On: A Soil-Moisture Irrigation Automation
Let us build a complete sense-decide-act loop. A device publishes soil moisture, Home Assistant decides when to water, and a valve reports its state. You can test everything with the free public area of broker.connectmq.com before connecting real hardware. Choose a unique prefix such as connectmq/demo/k7x2p9 and use it everywhere below; the public area is readable by anyone, so only test data belongs there.
Step 1: Simulate the sensor and valve from the command line
Publish a retained moisture reading:
mosquitto_pub -h broker.connectmq.com -p 1883 -i soil-k7x2p9 \
-t "connectmq/demo/k7x2p9/greenhouse1/soil" -r \
-m '{"moisture": 42.5, "tempC": 19.8}'
Watch everything under the prefix in a second terminal:
mosquitto_sub -h broker.connectmq.com -p 1883 -i watch-k7x2p9 \
-t "connectmq/demo/k7x2p9/#" -v
Step 2: Define the entities in Home Assistant
Add the MQTT integration in Home Assistant (Settings, Devices and services, Add integration, MQTT) with broker broker.connectmq.com, port 1883 and a unique client ID for testing. Then declare the sensor and valve in configuration.yaml:
mqtt:
sensor:
- name: "Greenhouse Soil Moisture"
unique_id: greenhouse1_soil_moisture
state_topic: "connectmq/demo/k7x2p9/greenhouse1/soil"
unit_of_measurement: "%"
value_template: "{{ value_json.moisture }}"
switch:
- name: "Greenhouse Valve"
unique_id: greenhouse1_valve
command_topic: "connectmq/demo/k7x2p9/greenhouse1/valve/set"
state_topic: "connectmq/demo/k7x2p9/greenhouse1/valve/state"
payload_on: "ON"
payload_off: "OFF"
retain: false
Step 3: Write the automation rule
automation:
- alias: "Irrigate greenhouse when soil is dry"
mode: single
triggers:
- trigger: numeric_state
entity_id: sensor.greenhouse_soil_moisture
below: 30
for: "00:05:00"
conditions:
- condition: time
after: "06:00:00"
before: "20:00:00"
actions:
- action: switch.turn_on
target:
entity_id: switch.greenhouse_valve
- delay: "00:10:00"
- action: switch.turn_off
target:
entity_id: switch.greenhouse_valve
The for clause ignores brief dips caused by sensor noise, the time condition avoids watering at night, and the fixed delay guarantees the valve closes even if the moisture reading stops arriving. Publish a moisture value of 25 and keep it there for five minutes; you will see ON appear on the valve/set topic in your mosquitto_sub window, followed by OFF ten minutes later. A real valve controller would subscribe to valve/set and publish its actual position to valve/state.
The same rule without Home Assistant
Not every site runs Home Assistant. Node-RED (see the Node-RED MQTT tutorial) or a small Python service can host the same logic. This version uses paho-mqtt 2.x and adds hysteresis so the valve does not chatter around the threshold:
import json
import threading
import uuid
import paho.mqtt.client as mqtt
PREFIX = "connectmq/demo/k7x2p9/greenhouse1"
DRY, WET, MAX_RUN_S = 30.0, 45.0, 600
valve_on = False
timer = None
def set_valve(client, on):
global valve_on, timer
valve_on = on
client.publish(f"{PREFIX}/valve/set", "ON" if on else "OFF", qos=1)
if timer:
timer.cancel()
if on: # safety: never run longer than MAX_RUN_S
timer = threading.Timer(MAX_RUN_S, set_valve, args=(client, False))
timer.start()
print("valve", "ON" if on else "OFF")
def on_connect(client, userdata, flags, reason_code, properties):
client.subscribe(f"{PREFIX}/soil", qos=1)
def on_message(client, userdata, msg):
moisture = json.loads(msg.payload)["moisture"]
if not valve_on and moisture < DRY:
set_valve(client, True)
elif valve_on and moisture > WET:
set_valve(client, False)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=f"irrigation-{uuid.uuid4().hex[:8]}")
client.on_connect = on_connect
client.on_message = on_message
client.connect("broker.connectmq.com", 1883, keepalive=30)
client.loop_forever()
IoT Automation Design Checklist
Use this list before trusting an automation with anything that matters:
- Fail safe. Decide what each actuator should do if the rule engine, broker or sensor disappears. Valves close, heaters turn off, doors stay locked.
- Debounce and hysteresis. Use time windows or separate on and off thresholds so noise does not cause rapid switching.
- Confirm state. Command a device on
/set, but drive dashboards and follow-up rules from its reportedstate. - Handle offline devices. Subscribe to Last Will
statustopics and alert when a critical sensor goes silent. - Keep manual overrides. People must be able to override automation locally, and rules should respect it.
- Choose QoS deliberately. QoS 1 for commands and alarms, QoS 0 for frequent readings. The MQTT QoS levels article explains why.
- Secure it. Use TLS, unique credentials per device or gateway, and never expose a broker without authentication. The MQTT security best practices checklist applies directly.
- Log decisions. Publish what the rule did and why to a log topic, so you can debug "why did the heater turn on at 3 a.m.?".
Testing and Maintaining Automations Over Time
An automation that works on day one can quietly break months later: a sensor battery runs flat, a firmware update renames an entity, or a device gets a new IP address and never reconnects. Treat automations like any other software you depend on.
Start by testing rules with simulated messages before they touch real actuators. Publishing a fake low moisture value or a fake occupancy event from the command line is the quickest way to see whether a rule fires, and it costs nothing. Keep a short list of these test messages for each important rule so you can repeat them after updates.
Next, watch the health of the inputs, not just the outputs. A heating rule that relies on a temperature sensor is only as good as that sensor's last report. Track the time since each critical device last published and raise an alert when it exceeds a few reporting intervals. Retained status topics with a Last Will make this easy, because a crashed device announces itself.
Finally, review automations a few times a year. Remove rules nobody remembers creating, check that thresholds still match the season or the crop, and confirm that manual overrides still work. A small, well-understood set of rules is more valuable than a large one that nobody trusts.
Common Pitfalls
- Rules spread across many tools. If some logic lives in a plug's firmware, some in Home Assistant and some in Node-RED, nobody can predict behavior. Pick a primary rule engine.
- Retained commands. A retained
ONon a command topic will switch the device on again every time it reconnects. Retain state, not commands. - Shared client IDs. Two clients with the same ID kick each other off the broker in a loop. Make every ID unique.
- Cloud-only critical paths. Keep safety-relevant and comfort-critical rules local, and use the cloud for monitoring and remote access.
- Ignoring protocol choice. Matter, Zigbee and Wi-Fi each have trade-offs; Matter vs MQTT explains how they can coexist.
Scaling IoT Automation Across Sites with ConnectMQ
As soon as you have more than one location, or you need remote access without opening ports on your router, a hosted broker becomes the simplest hub. ConnectMQ gives every site the same endpoint over TLS on port 8883, or secure WebSocket on port 8084 for browser dashboards, so Home Assistant instances, Node-RED flows, gateways and devices in different places all meet on one broker. The portal's live topic explorer helps you check what devices are actually publishing, and threshold alert rules with an alerts log can warn you when a reading crosses a limit. The automation logic itself stays in Home Assistant, Node-RED or your own service, connected as ordinary MQTT clients.
The free Serverless plan supports up to 25 connections, which comfortably covers a typical home or small greenhouse. Larger buildings and multi-site deployments can move to the paid Serverless plan with up to 1,000 connections, or to a dedicated cluster. Compare the options in the managed MQTT broker overview.
Get Started with ConnectMQ
Prototype your automations on the public MQTT test broker, then move to a private broker when real devices go live. Sign up at app.connectmq.com on the free Serverless plan, no credit card required, create a broker, and copy the generated username, password and client ID into Home Assistant, Node-RED or your devices. Switch the port to 8883 with TLS and your rules keep working unchanged.
Frequently Asked Questions
Do I need Home Assistant for IoT automation?
No. Home Assistant is the most common logic layer for homes, but Node-RED, openHAB or a small Python or Node.js service connected to the same MQTT broker can run identical rules. The broker and topics stay the same whichever you choose.
Should my automation broker be local or in the cloud?
Run time-critical and safety-relevant rules locally so they work during internet outages. Use a cloud broker for remote access, multi-site setups and monitoring, or combine both with local rules and cloud visibility.
Does ConnectMQ run my automation rules?
No. ConnectMQ is the messaging hub and offers threshold alert rules for monitoring, but automation logic runs in tools such as Home Assistant, Node-RED or your own services, which connect to the broker as MQTT clients.
Why does my device switch on every time it reconnects?
You probably published the command with the retain flag. The broker stores retained messages and delivers them to every new subscriber, so retain state topics but never command topics.
Can I test IoT automation without hardware?
Yes. Simulate sensors with mosquitto_pub against broker.connectmq.com, using a unique client ID and topic prefix, and watch the resulting commands with mosquitto_sub before connecting real devices.



