Node-RED collects run state and counts, writes one row per line per minute to PostgreSQL, and Grafana queries it. Calculate OEE as SUM(good_count x ideal_cycle_s) / SUM(planned_s), so any time range or group of lines is weighted correctly. Run the 3-row test in Step 5 before you trust a panel.
The build at a glance
This is a working pattern for a screen at the line or a shift view, built on open source parts. Node-RED is Apache 2.0, and Grafana's core is AGPLv3.
Run state, a total counter and a reject counter from a PLC or sensor.
Turns signals into one row per line, per minute.
Stores those rows, the job list and the planned time.
Queries the rows and draws OEE, the trend and the stop list.
For which screens to build, read OEE dashboard examples. This page covers the plumbing and the queries.
Here is what we did and did not run. All the SQL ran in PGlite (PostgreSQL 16) with Grafana's macros replaced by hand, and the two Function nodes ran in plain Node.js with a stand-in for context.
We have not run the flow inside Node-RED or the panels inside Grafana. Step 5 lists what to test in each.
Step 1: tables
CREATE TABLE line_minute (
ts timestamptz NOT NULL DEFAULT now(),
line_id text NOT NULL,
product text NOT NULL,
ideal_cycle_s numeric NOT NULL,
planned_s numeric NOT NULL,
run_s numeric NOT NULL,
total_count numeric NOT NULL,
good_count numeric NOT NULL,
stop_reason text
);
CREATE INDEX ON line_minute (line_id, ts);
CREATE TABLE job (
line_id text NOT NULL,
product text NOT NULL,
ideal_cycle_s numeric NOT NULL,
end_ts timestamptz
);
CREATE TABLE plan_window (
line_id text NOT NULL,
start_ts timestamptz NOT NULL,
end_ts timestamptz NOT NULL
);
INSERT INTO job (line_id, product, ideal_cycle_s)
VALUES ('L1', 'Bottle 500', 3);
INSERT INTO plan_window (line_id, start_ts, end_ts)
VALUES ('L1', now() - interval '1 hour', now() + interval '8 hours');
The open row in job holds what runs now, and plan_window holds planned production time with breaks left out as gaps. Copying the ideal into each row keeps old results correct after you change it.
Use the best proven rate for the ideal, as in how to find the ideal run rate. Numeric columns also avoid integer division, which would turn your OEE into zero.
Step 2: Node-RED collects the signals
In Node-RED, open Manage palette and install node-red-contrib-postgresql. For Modbus devices, add node-red-contrib-modbus, while the mqtt in node comes built in.
Bring in three signals: a cumulative total count, a cumulative reject count and a run state of 0 or 1. PLC signals for OEE and retrofit sensors cover getting them, and Modbus machine data covers register reads.
Function 1: keep the latest signals
Wire your inputs to this Function node, with topics ending in count, reject, running and reason. A ui-dropdown from Dashboard 2.0 can send the reason, since it puts the chosen value in msg.payload, but add a change node that sets msg.topic to line1/reason.
const s = flow.get("sig") || {};
const k = msg.topic.split("/").pop();
const v = k === "reason" ? msg.payload : Number(msg.payload);
// a restart of the line clears the last reason
if (k === "running" && v && !s.running) s.reason = null;
s[k] = v;
flow.set("sig", s);
return null;
Function 2: one row per minute
Set an inject node to repeat every 1 second, and wire it to this Function. It emits a message once every 60 ticks.
const s = flow.get("sig");
if (!s || s.count === undefined) return null;
const m = context.get("m") ||
{ tick: 0, run: 0, c: s.count, r: s.reject || 0, why: null };
m.tick++;
if (s.running) m.run++;
else if (s.reason) m.why = s.reason;
if (m.tick < 60) { context.set("m", m); return null; }
// a counter that fell has been reset, so count from zero
const delta = (now, last) => now >= last ? now - last : now;
const total = delta(s.count, m.c);
const bad = delta(s.reject || 0, m.r);
msg.params = ["L1", m.run, total, total - bad, m.why];
context.set("m",
{ tick: 0, run: 0, c: s.count, r: s.reject || 0, why: null });
return msg;
A reset loses the few counts since the last row. Context is memory only by default, so a restart loses the current minute unless you enable the localfilesystem store.
The insert
Wire Function 2 to a postgresql node with this query, and set your connection in its server config. The node reads the array in msg.params as $1, $2 and so on.
INSERT INTO line_minute (line_id, product, ideal_cycle_s,
planned_s, run_s, total_count, good_count, stop_reason)
SELECT j.line_id, j.product, j.ideal_cycle_s,
CASE WHEN EXISTS (SELECT 1 FROM plan_window p
WHERE p.line_id = j.line_id
AND now() >= p.start_ts AND now() < p.end_ts)
THEN 60 ELSE 0 END,
$2::numeric, $3::numeric, $4::numeric, $5::text
FROM job j
WHERE j.line_id = $1 AND j.end_ts IS NULL;
No open job means no row, so keep the job table current. The casts state each parameter's type outright, so nothing depends on how the driver sends it.
Step 3: connect Grafana
To run Grafana in a container, use docker run -d -p 3000:3000 --name=grafana grafana/grafana. Then go to Connections > Add new connection, pick PostgreSQL and fill in the Host URL and Database name.
Grafana's docs say to give this user only SELECT rights, since it does not check query safety. Click Save & test.
Add a dashboard variable named line of type Query, with SELECT DISTINCT line_id FROM line_minute, and allow multi-value. Grafana quotes the values for you when you write IN ($line).
Step 4: the queries
All of these use Grafana's PostgreSQL macros. The planned_s > 0 filter keeps counts made outside planned time from pushing OEE up.
Stat panels: OEE and its factors (Format: Table)
SELECT
SUM(good_count * ideal_cycle_s)
/ NULLIF(SUM(planned_s), 0) AS "OEE",
SUM(run_s) / NULLIF(SUM(planned_s), 0) AS "Availability",
SUM(total_count * ideal_cycle_s)
/ NULLIF(SUM(run_s), 0) AS "Performance",
SUM(good_count * ideal_cycle_s)
/ NULLIF(SUM(total_count * ideal_cycle_s), 0) AS "Quality"
FROM line_minute
WHERE $__timeFilter(ts) AND line_id IN ($line)
AND planned_s > 0
In Standard options, set Unit to Percent (0.0-1.0), not Percent (0-100), with Min at 0 and Max at 1. Pick your thresholds from your own OEE target.
Time series: OEE per hour (Format: Time series)
SELECT
$__timeGroupAlias(ts,'1h'),
SUM(good_count * ideal_cycle_s)
/ NULLIF(SUM(planned_s), 0) AS "OEE"
FROM line_minute
WHERE $__timeFilter(ts) AND line_id IN ($line)
AND planned_s > 0
GROUP BY 1
ORDER BY 1
Bar chart: stop minutes by reason (Format: Table)
SELECT
COALESCE(stop_reason, 'Not tagged') AS reason,
ROUND(SUM(planned_s - run_s) / 60, 1) AS stop_min
FROM line_minute
WHERE $__timeFilter(ts) AND line_id IN ($line)
AND planned_s > 0 AND run_s < planned_s
GROUP BY 1
ORDER BY 2 DESC
LIMIT 10
Set Orientation to Horizontal. These minutes add up to the availability loss, so the list and the OEE panel always agree, and reading a downtime Pareto covers the next step.
State timeline: one line (Format: Table)
SELECT ts AS time,
CASE WHEN planned_s = 0 THEN 'Not planned'
WHEN run_s = 0 THEN 'Stopped'
WHEN run_s < planned_s THEN 'Short stop'
ELSE 'Running' END AS state
FROM line_minute
WHERE $__timeFilter(ts) AND line_id = 'L1'
ORDER BY ts
Turn on Merge equal consecutive values in the State timeline panel. Short stops show up here long before they appear in a shift report, as micro-stops explains.
Step 5: test it with three rows
Paste this into psql, then set the dashboard to the last 15 minutes. If the stat panels do not match the table, the query or the time range is wrong.
INSERT INTO line_minute (ts, line_id, product, ideal_cycle_s,
planned_s, run_s, total_count, good_count, stop_reason)
VALUES
(now() - interval '3 min', 'L1', 'TEST', 3, 60, 60, 18, 18, NULL),
(now() - interval '2 min', 'L1', 'TEST', 3, 60, 45, 13, 12, NULL),
(now() - interval '1 min', 'L1', 'TEST', 3, 60, 0, 0, 0, 'Label misfeed');
| Row | planned_s | run_s | total | good | Note |
|---|---|---|---|---|---|
| 1 | 60 | 60 | 18 | 18 | Full minute |
| 2 | 60 | 45 | 13 | 12 | Short stop, one reject |
| 3 | 60 | 0 | 0 | 0 | Stopped, "Label misfeed" |
| Sum | 180 | 105 | 31 | 30 | A 58.3%, P 88.6%, Q 96.8%, OEE 50.0% |
What to test, in order
- Stat panels: OEE 50.0%, Availability 58.3%, Performance 88.6%, Quality 96.8%. A reading of 0.5% means the unit is wrong.
- Bar chart: Label misfeed 1.0 and Not tagged 0.3, which is 15 seconds rounded.
- State timeline: Running, Short stop, then Stopped, one minute each.
- Node-RED rows: with signals live, expect one new row a minute, and run_s near 60 while running.
- A real stop: hold run state at 0 for 30 seconds and tag a reason. That minute should show run_s near 30 and your reason.
- A counter reset: drop the counter to zero. total_count must stay positive.
- Clean up: run
DELETE FROM line_minute WHERE product = 'TEST';
Grafana can also aggregate on its own, and that is the trap. If a panel calculation averages hourly OEE, a short hour counts as much as a full one, so let SQL do the weighting, as how to average OEE explains.
Use Node-RED's own dashboard instead?
For one gauge at one line, you can. Install @flowfuse/node-red-dashboard, since the older node-red-dashboard is deprecated.
Run the OEE query from a repeating inject, with ts >= now() - interval '8 hours' in place of the Grafana macros. Then send OEE x 100 to a ui-gauge as a number in msg.payload, with the range set from 0 to 100.
For history, several lines and shared views, Grafana is the better tool. The Power BI version suits weekly management reports.
What this build does not do
- One line per flow. The code hard-codes "L1". Keep state per line, or copy the flow.
- No security review. Don't expose Node-RED or Grafana to the internet as is, and keep the PLC network protected.
- No maintenance of itself. Someone must own the job list, plan windows and backups. If nobody does, the numbers drift.
- One reason per minute. That resolution suits a dashboard, not a detailed root cause analysis.
If you would rather not maintain flows, tables and queries yourself, Fabrico is a light MES for OEE. It takes stops, speed and counts live from PLCs or IoT sensors, with add-on AI cameras counting output where a machine has no usable signal, with operators tagging the cause at the line. See how Fabrico connects to machines.
What people ask next
Can I build an OEE dashboard with Grafana for free?
Grafana's open source edition, PostgreSQL and Node-RED carry no license fee, but you do pay in build and upkeep time. Check that against your needs using OEE without software and free OEE software.
Does Grafana have a built-in OEE panel?
We found none in Grafana's standard panels, and you do not need one. Stat, Time series, Bar chart and State timeline panels with the queries above do the job. For layouts, see OEE dashboard examples.
Can I use InfluxDB or TimescaleDB instead of PostgreSQL?
TimescaleDB runs on PostgreSQL, so the SQL above should carry over, and Grafana's data source has a TimescaleDB option. InfluxDB queries depend on its version, so rewrite them, and the same weighting rule applies. How to average OEE gives the rule.
How do I get run state from a machine with no PLC access?
Use an external sensor, for example a current sensor on the main motor. Stack light monitoring is another common way in.
Why not calculate OEE inside Node-RED?
You could, but then the weighting only works for the period Node-RED chose. Storing raw seconds and counts lets Grafana ask for any range or group of lines. Time vs count OEE method explains the two forms.
Fabrico is a light MES for OEE. It takes stops, speed and counts from PLCs or IoT sensors, with add-on AI cameras counting output where a machine has no usable signal, and operators tag the cause at the line.