From 3e68a0dc02ed38e1309103ce911bd4f5e95188e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Sat, 21 Mar 2026 22:42:53 +0100 Subject: [PATCH] Deduplicate Nightscout treatment entries by id field Nightscout can return duplicate treatment entries with the same "id" (Trio/Loop UUID) but different "_id" (MongoDB ObjectId), causing duplicate boluses, carbs, etc. on the graph. Filter duplicates at the top of updateTreatments before classification. --- LoopFollow/Controllers/Nightscout/Treatments.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/LoopFollow/Controllers/Nightscout/Treatments.swift b/LoopFollow/Controllers/Nightscout/Treatments.swift index 8ff20df87..2996c59da 100644 --- a/LoopFollow/Controllers/Nightscout/Treatments.swift +++ b/LoopFollow/Controllers/Nightscout/Treatments.swift @@ -35,6 +35,13 @@ extension MainViewController { // Process and split out treatments to individual tasks func updateTreatments(entries: [[String: AnyObject]]) { + // Deduplicate entries by "id" field (Trio/Loop UUID) + var seenIDs = Set() + let uniqueEntries = entries.filter { entry in + guard let id = entry["id"] as? String else { return true } + return seenIDs.insert(id).inserted + } + var tempBasal: [[String: AnyObject]] = [] var bolus: [[String: AnyObject]] = [] var smb: [[String: AnyObject]] = [] @@ -49,7 +56,7 @@ extension MainViewController { var cgmSensorStart: [sageData] = [] var insulinCartridge: [iageData] = [] - for entry in entries { + for entry in uniqueEntries { guard let eventType = entry["eventType"] as? String else { continue }