Code
js
// n8n Code node, mode "Run Once for All Items".
// Place it right after the node that fetches the rows.
const HISTORY_SIZE = 7; // healthy runs kept for the baseline
const MIN_HISTORY = 3; // runs needed before the median is trusted
const DROP_RATIO = 0.5; // alert below half of the median
function median(values) {
const sorted = [...values].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
}
function checkRowCount(count, history) {
const baseline = history.length >= MIN_HISTORY ? median(history) : null;
if (count === 0) {
return { alert: true, reason: 'zero rows', baseline };
}
if (baseline === null) {
return { alert: false, reason: 'not enough history', baseline };
}
if (count < baseline * DROP_RATIO) {
return { alert: true, reason: 'below half of median', baseline };
}
return { alert: false, reason: 'ok', baseline };
}
function guard(items, staticData) {
// An empty fetch can arrive as zero items or as one empty item.
const rows = items.filter(
(item) => item && item.json && Object.keys(item.json).length > 0
);
const count = rows.length;
const history = staticData.rowCounts || [];
const result = checkRowCount(count, history);
// Only healthy runs feed the baseline, so an outage never becomes "normal".
if (!result.alert) {
staticData.rowCounts = [...history, count].slice(-HISTORY_SIZE);
}
return [{ json: { count, ...result } }];
}
// In n8n, uncomment the next line. It is the only n8n-specific line.
// return guard($input.all(), $getWorkflowStaticData('global'));Why it happens
n8n marks an execution as successful when no node throws an error. An HTTP 200 with an empty list is a valid response, so nothing throws. The workflow then writes nothing, reports green, and can stay wrong for days. Only a check on the data itself can catch this, and a median of recent runs gives a baseline that adapts to your normal volume.
Steps
- Open the fetch node, go to Settings, and turn on "Always Output Data". Without it, an empty result stops the branch and the check never runs.
- Add a Code node after the fetch, set the mode to "Run Once for All Items", and paste the code.
- Uncomment the last line so the node returns the result.
- Add an IF node on
{{ $json.alert }}. Send the true branch to your alert channel withcount,baselineandreason. - Activate the workflow. The first three runs only build history, except that zero rows always alerts.
The node keeps the last seven healthy counts in the workflow static data. Each run is compared with their median, then added to the history if it passed.
Pitfalls
- Static data is saved only for production executions of an active workflow. Manual test runs do not keep the history.
- If the source legitimately shrinks by more than half, the alert repeats on every run. Reset
rowCountsonce, or lowerDROP_RATIO. - The code counts items. If your API returns one item that holds an array, add a Split Out node before the check.
- Static data is small and shared by the whole workflow. Use a separate key for each fetch you guard.
Seen on: n8n Community ·
Related service: n8n, Make and Zapier workflow automation