Quick answer: for an 8-relay Node-RED dashboard, add eight ui-switch widgets (Dashboard 2.0) with on/off values ON / OFF and Passthrough turned off. Send each switch's output through one function node that builds the topic solderhub/lab/relaybox01/command/channel/N to an mqtt out node (not retained). Feed the switches from an mqtt in node on …/state/channel/+, so each switch shows what the relay board reports, not what you clicked. Add the board's …/status topic as an online/offline indicator and an All off button. The firmware side is in ESP32 8-channel relay over MQTT; both guides use the same topics.
The most common bug in relay dashboards is a feedback loop: the state message updates the switch, the switch re-sends a command, the board answers with state again… Turning Passthrough off breaks the loop, and driving the switches from the reported state means the dashboard can never show a relay as ON when the board didn't switch it.
| You need | Notes |
|---|---|
| Node-RED 3.x or 4.x | Install on a Pi or on Windows |
@flowfuse/node-red-dashboard (Dashboard 2.0) | The old node-red-dashboard is deprecated |
| Mosquitto broker | Install Mosquitto |
| ESP32 + 8-channel relay board running the matching sketch | ESP32 relay firmware |
1. The topic contract
| Topic | Published by | Payload | Retained |
|---|---|---|---|
solderhub/lab/relaybox01/command/channel/1 … /8 | Node-RED | ON / OFF | No |
solderhub/lab/relaybox01/command/all | Node-RED (All off button) | OFF | No |
solderhub/lab/relaybox01/state/channel/1 … /8 | ESP32 | ON / OFF | Yes |
solderhub/lab/relaybox01/status | ESP32 / broker Last Will | online / offline | Yes |
Commands must never be retained: a retained ON is replayed every time the board reconnects. Use explicit ON/OFF rather than TOGGLE, so a duplicated or retried message can't flip a relay the wrong way.
2. Build the dashboard (8 switches)
- Manage palette → Install
@flowfuse/node-red-dashboard. - Create a page (
/relays) and a group (Relay box 01). - Add eight ui-switch nodes in that group. For each one:
| ui-switch setting | Value |
|---|---|
| Label | A real name, e.g. Workshop light, Fan, Pump |
| On payload / Off payload | string ON / string OFF |
| Passthrough | Off (prevents the command → state → command loop) |
| Topic | The channel number: 1 … 8 (or set msg.topic with a Change node after the switch) |
- Add a ui-button All off with payload
OFFand topicall. - Add a ui-text Relay box for the online/offline status.
Name the switches after the load, not "Relay 3". Keep a written channel-to-load list next to the board too.
3. Wire the flow
Commands: all 8 switches and the All off button → function "to command topic" → mqtt out (broker set, topic empty, QoS 1, retain false).
// function "to command topic": builds .../command/channel/N or .../command/all
const BASE = "solderhub/lab/relaybox01/command/";
const payload = String(msg.payload).toUpperCase();
if (payload !== "ON" && payload !== "OFF") {
node.warn("Rejected payload: " + msg.payload);
return null;
}
if (msg.topic === "all") {
if (payload !== "OFF") return null; // "all" only ever switches OFF
return { topic: BASE + "all", payload: "OFF" };
}
const ch = Number(msg.topic);
if (!Number.isInteger(ch) || ch < 1 || ch > 8) {
node.warn("Rejected channel: " + msg.topic);
return null;
}
return { topic: BASE + "channel/" + ch, payload: payload };State back to the switches: mqtt in on solderhub/lab/relaybox01/state/channel/+ (output: a String) → function "route state" with 8 outputs → output N wired to switch N:
// function "route state": 8 outputs, one per ui-switch
const ch = Number(msg.topic.split("/").pop());
if (!Number.isInteger(ch) || ch < 1 || ch > 8) return null;
if (msg.payload !== "ON" && msg.payload !== "OFF") return null;
const out = new Array(8).fill(null);
out[ch - 1] = { payload: msg.payload };
return out;Because the state topics are retained, opening the dashboard (or restarting Node-RED) immediately shows all eight real states.
Online/offline: mqtt in on solderhub/lab/relaybox01/status → ui-text. The ESP32's Last Will publishes offline if it loses power, so you know when switches can't do anything. Optionally colour it with a Change node or a ui-template.
4. What happens in each situation
| Situation | What the user sees |
|---|---|
| Tap Fan ON, board online | Switch moves, relay clicks, state ON confirms it (well under a second on a LAN) |
| Tap Fan ON, board offline | Status shows offline; the switch jumps back when the next state message arrives, or stays where the last report left it after a refresh |
| Board reboots | All switches go OFF (the board boots OFF and republishes state) |
| Node-RED restarts | Switches restore from the retained state topics |
| Two phones open | Both stay in sync, since both follow the reported state |
Useful extras
Auto-off timer for a channel
Pumps, heaters and irrigation valves should never stay on forever because someone forgot. Add a trigger node on the state of that channel:
- mqtt in on
solderhub/lab/relaybox01/state/channel/4→ switch node: payload ==ON(output 1), ==OFF(output 2). - Output 1 → trigger node: send nothing, then wait for 10 minutes, then send payload
OFF, with extend delay if new message arrives off. Output 2 → the same trigger node withmsg.reset = trueset by a Change node, so turning it off by hand cancels the timer. - Trigger output → change node setting
msg.topicto4→ the same to command topic function.
Because the timer watches the reported state, it also covers channel 4 being switched on from Home Assistant, a schedule or another phone. For something safety-critical, put the maximum run time in the ESP32 firmware too, so it still applies when Node-RED is down.
Show when each relay last changed
In the route state function, add a ninth output that sends { payload: "Ch " + ch + " " + msg.payload + " at " + new Date().toLocaleTimeString() } to a ui-text or a ui-table log. That small line answers "who switched the pump on at 3 a.m.?" surprisingly often.
Confirm before switching dangerous loads
For a heater or a gate, add an Arm switch next to it. Store its value with a Change node in flow.armed4, and in the to command topic function reject ON for that channel unless flow.get("armed4") is true, then reset the flag after one use. Two taps are a small price for not opening the garage by accident.
Group and label clearly
Use one Dashboard group per room or per relay box, put the online/offline text at the top of each group, and keep labels identical to the physical labels on the relay board's enclosure. Clear naming prevents more mistakes than any amount of code.
5. Test before connecting real loads
Use LEDs or a 12 V lamp on the relay contacts while testing.
- Watch the broker:
mosquitto_sub -h <broker> -u <user> -P '<pass>' -t 'solderhub/lab/relaybox01/#' -v. - Toggle each switch and confirm one
command/channel/Nmessage and onestate/channel/Nreply, with no repeats (a repeating pair means Passthrough is on). - Press All off: eight OFF states arrive.
- Unplug the ESP32: status changes to offline within about 20 seconds.
- Restart Node-RED: switches come back showing the real states.
- Check the broker has no retained commands:
mosquitto_sub -t 'solderhub/lab/relaybox01/command/#' -v -W 2should print nothing.
Protect the dashboard and broker before using it beyond the bench: httpNodeAuth for the dashboard, MQTT users and ACLs, and no port forwarding. See Secure Node-RED.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Relays flicker or MQTT floods with messages | Passthrough on, so state messages re-emit commands | Turn Passthrough off on every ui-switch |
| Switch moves but nothing is published | Topic not set, so the function rejects the channel | Set the switch Topic to 1–8; check the debug sidebar warning |
| Switch jumps back after tapping | Board didn't apply the command (offline, wrong topic) | Check status, topic spelling and the ESP32 Serial Monitor |
| Wrong relay switches | Switch topic doesn't match the physical channel | Fix the Topic numbers or the board's pin order |
| Relays switch on after a reconnect | A retained command is stored on the broker | Clear it: mosquitto_pub -t '…/command/channel/N' -r -n |
| Switches show OFF after Node-RED restart | State not retained by the board | Firmware must publish state with retain = true |
| Status never shows offline | Board doesn't set a Last Will | Use the LWT in the firmware's connect() call |
| Dashboard 404 | Old /ui URL | Dashboard 2.0 lives at /dashboard |
FAQ
Why not use a single JSON command for all eight relays?
You can, e.g. {"ch":3,"state":"ON"} on one topic. Per-channel topics are easier to secure with ACLs, easier to watch with mosquitto_sub, and map directly to Home Assistant MQTT switches.
Can I control the same relays from Home Assistant too? Yes. Because everyone follows the retained state topics, Node-RED, Home Assistant and phones all stay in sync.
Can I add schedules?
Yes: an Inject node at a set time sending {topic: "3", payload: "ON"} into the same function. See Node-RED scheduler and context.
Is a dashboard switch safe for mains equipment? Only as a convenience. Use rated relays in an enclosure, fusing and a local manual off; a dashboard is not an emergency stop.
Next steps
- ESP32 8-channel relay over MQTT: the matching firmware.
- Node-RED Dashboard 2.0 setup.
- Node-RED scheduler, context and LDR automation.
- MQTT topic naming best practices and retained messages and Last Will.
- Reliable flows with Catch nodes.
Official reference: Dashboard 2.0 ui-switch.




