adds code

Controlling Everything with Node-RED Dashboard via MQTT

Originally published: 2021-02-27

A Node-RED Dashboard running on a Raspberry Pi can switch relays and show live sensor readings from every corner of your home — with MQTT as the messenger in between. In the video below, ESP32 and ESP8266 boards act as MQTT clients driving relays and sensors, while the Pi hosts both the broker and the dashboard. This guide explains the message pattern that makes it work and the two settings that make it reliable.

Quick answer: Dashboard nodes give you buttons, switches, and gauges in a browser; MQTT carries the traffic. Every device publishes its current state to a state topic, the dashboard sends changes to a command topic, and Node-RED flows connect the two. One broker — typically Mosquitto on the Pi — ties the whole system together.

Watch: Node-RED Dashboard and MQTT in action

The video shows the complete loop on real hardware: appliance control through dashboard switches and live sensor data on dashboard gauges, with ESP32/ESP8266 boards as MQTT clients and the Raspberry Pi acting as the broker. Watch how each board both publishes readings and subscribes to commands — that two-way pattern is the core of the whole design.

How does the state-and-command model work?

Clean MQTT setups separate two topic types per device:

  • State topic (e.g. home/livingroom/light/state) — the device publishes what it actually is: on, off, 23.4 °C, 62 % humidity. Nothing else ever writes here.
  • Command topic (e.g. home/livingroom/light/set) — the dashboard or a flow publishes what it wants: on, off, a brightness value. The device applies the command, then reports the result back to the state topic.

With that split, flows never guess: a dashboard switch shows the device’s last reported state, and automations react to confirmed reality instead of assumed commands.

What should you build first?

Start with one of everything: install the Mosquitto broker on the Pi (mosquitto.org), flash one ESP32 with a relay and a sensor, and give it both topics. In Node-RED, add a dashboard group with a switch wired to the command topic and a gauge wired to the state topic, plus debug nodes on both sides. Once that single device behaves correctly, every additional device is a copy of the same pattern with new topic names.

Which MQTT settings make the dashboard reliable?

Two settings separate a toy from a system you can trust:

  • Retain messages on state topics. A retained message makes the broker remember the last state and hand it to every new subscriber instantly. Without it, your dashboard shows blank widgets after every Node-RED or broker restart until a device happens to publish again.
  • Last Will and Testament (LWT) for availability. Each device registers a will message when it connects — typically “offline” on an availability topic. If a board drops off the network, the broker publishes the will automatically, and your dashboard can show the device as offline instead of silently displaying stale data.

Practical tip: publish state with the retain flag set, and keep commands non-retained — you do not want a rebooting device re-executing an old command the broker kept for it.

What gives a dashboard instant visible feedback?

A dashboard begs for something worth controlling, and lights deliver the most satisfying feedback of all: press a switch and the room itself changes. When adding bulbs or strips, check which radio they speak, because it decides how they join your MQTT world — Zigbee devices arrive through a coordinator bridge such as Zigbee2MQTT, Wi-Fi bulbs connect to your network directly, and Matter devices join through controllers that bridge into MQTT setups. Our verified smart-light comparisons list the radio, brightness, and measured specs side by side so you know how a bulb will connect before you buy it.

If you buy through links on the sites we recommend, we may earn a small commission.

Frequently Asked Questions

Do I need a Raspberry Pi for this setup?

No. Any always-on computer can host the broker, Node-RED, and the dashboard — the Pi is simply the most common choice because it is cheap, low-power, and has GPIO for local sensors.

Why did my dashboard show blank widgets after a restart?

Almost always non-retained state topics. Publish device states with the retain flag so the broker can replay the last known value to the dashboard the moment it reconnects.

Can ESP8266 and ESP32 boards mix on the same broker?

Yes. Both speak standard MQTT, so they share the same broker and topic conventions — pick the board per project based on Wi-Fi strength, pins, and processing needs.

Further reading

Sources

Previous Post Next Post