Code
js
// n8n Code node. Mode: "Run Once for All Items".
// Place it right after the node that fetches the list.
const WINDOW = 7; // healthy runs kept as the baseline
const MIN_HISTORY = 3; // runs needed before the "below half" rule applies
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;
let reason = null;
if (count === 0) reason = 'zero rows';
else if (baseline !== null && count < baseline / 2) reason = 'below half of median';
const alert = reason !== null;
// Only healthy runs enter the baseline, so a long outage cannot lower it.
const nextHistory = alert ? history : [...history, count].slice(-WINDOW);
return { count, baseline, alert, reason, nextHistory };
}
// $input exists only inside n8n. Outside n8n this block is skipped.
if (typeof $input !== 'undefined') {
const store = $getWorkflowStaticData('global');
// "Always Output Data" sends one empty item when the list is empty.
const rows = $input.all().filter((item) => Object.keys(item.json).length > 0);
const result = checkRowCount(rows.length, store.rowCounts || []);
store.rowCounts = result.nextHistory;
return [{
json: {
count: result.count,
baseline: result.baseline,
alert: result.alert,
reason: result.reason,
},
}];
}Why it happens
n8n marks a run green when no node throws an error. An API that answers 200 with an empty list is not an error, so nothing fails. Worse, when a node outputs no items, the nodes after it do not run at all. The workflow ends early, still green, and nobody is told that the data stopped coming.
Steps
- Open the fetch node, go to Settings, and turn on "Always Output Data". The next node now runs even when the list is empty.
- Add a Code node after it, set the mode to "Run Once for All Items", and paste the code.
- The code counts the items that are not empty.
- It reads the saved counts from workflow static data and takes their median as the baseline.
- It sets
alertto true when the count is zero, or below half of the baseline. - Add an IF node on
{{ $json.alert }}. Send the true branch to Slack, email or Telegram, and includecount,baselineandreasonin the message.
Pitfalls
- Workflow static data is saved only for production runs started by an active trigger. Manual test runs do not keep the history.
- The "below half" rule needs three healthy runs first. Before that, only a count of zero alerts.
- Runs that alert are not added to the baseline. If the volume drops for a real, permanent reason, clear
rowCountsonce so the baseline can reset. - If the API returns one item that holds an array, add a Split Out node before the Code node so each row is one item.
- Half of the median is a starting point. Sources with strong weekday and weekend swings may need a lower threshold or a longer window.
Seen on: n8n Community ·
Related service: n8n, Make and Zapier workflow automation