// High Scrap Detection - Add to complete-work-order handler // This code should be added to the "Work Order buttons" function // in the "complete-work-order" case // After calculating final counts, check for high scrap const targetQty = Number(order.target) || 0; const scrapCount = Number(order.scrap) || 0; const scrapPercent = targetQty > 0 ? (scrapCount / targetQty) * 100 : 0; // Trigger: Scrap > 10% of target quantity if (scrapPercent > 10 && targetQty > 0) { const severity = scrapPercent > 25 ? 'critical' : 'warning'; const highScrapAnomaly = { anomaly_type: 'high-scrap', severity: severity, title: `High Waste Detected`, description: `Work order completed with ${scrapCount} scrap parts (${scrapPercent.toFixed(1)}% of target ${targetQty}). Why is there so much waste?`, data: { scrap_count: scrapCount, target_quantity: targetQty, scrap_percent: Math.round(scrapPercent * 10) / 10, good_parts: Number(order.good) || 0, total_cycles: global.get("cycleCount") || 0 }, kpi_snapshot: { oee: (msg.kpis && msg.kpis.oee) || 0, availability: (msg.kpis && msg.kpis.availability) || 0, performance: (msg.kpis && msg.kpis.performance) || 0, quality: (msg.kpis && msg.kpis.quality) || 0 }, work_order_id: order.id, cycle_count: global.get("cycleCount") || 0, timestamp: Date.now(), status: 'active' }; // Send to Event Logger // This would be a separate output from the complete-work-order handler return { topic: "anomaly-detected", payload: [highScrapAnomaly] }; } return null;