-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Summary
Trying to move from AAPS to Trio/LF, using Libre 3+ CGM. At the moment, when using Nightscout as the only data source (no Dexcom Share configured), BG data fails to load and the main screen displays blank values. This occurs because the API query filters on the dateString field, which may not exist or be consistently populated in all Nightscout configurations.
Expected Behavior
- BG value, delta, direction, and "minutes ago" should display on the home tab
- The app should fetch entries from the Nightscout /api/v1/entries.json endpoint successfully
Actual Behavior
- BG display elements remain blank
- Error logged: Failed to fetch bg data: typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode
Array but found a dictionary instead.", underlyingError: nil)) - Alternatively, the API returns an empty array [] even when entries exist
Root Cause
In BGData.swift, the webLoadNSBGData() function constructs a query using:
parameters["find[dateString][$gte]"] = utcISODateFormatter.string(from: date)
This MongoDB-style query filters entries where dateString >= . However:
- Not all Nightscout entries have a dateString field populated
- Some CGM integrations only populate the date field (milliseconds timestamp)
- When dateString doesn't exist on entries, the query returns zero results
Investigation Steps
- Confirmed Nightscout connectivity worked (device status endpoint returned IOB/COB successfully)
- Tested /api/v1/entries.json?count=5&token=xxx in browser - returned valid entry array
- Tested with full app parameters including find[dateString][$gte]=... - returned empty array []
- Inspected entry data - confirmed entries had date (milliseconds) and created_at but dateString was not reliably present
- Identified the filter parameter as the cause of empty results
Fix
Change the filter in BGData.swift from dateString (ISO string) to date (milliseconds timestamp):
Before:
var parameters: [String: String] = [:]
let utcISODateFormatter = ISO8601DateFormatter()
let date = Calendar.current.date(byAdding: .day, value: -1 * Storage.shared.downloadDays.value, to: Date())!
parameters["count"] = "(Storage.shared.downloadDays.value * 2 * 24 * 60 / 5)"
parameters["find[dateString][$gte]"] = utcISODateFormatter.string(from: date)
After:
var parameters: [String: String] = [:]
let date = Calendar.current.date(byAdding: .day, value: -1 * Storage.shared.downloadDays.value, to: Date())!
parameters["count"] = "(Storage.shared.downloadDays.value * 2 * 24 * 60 / 5)"
parameters["find[date][$gte]"] = "(Int(date.timeIntervalSince1970 * 1000))"
Why This Fix Works
- The date field (milliseconds since epoch) is consistently present on all Nightscout SGV entries
- Using milliseconds matches the native format stored by Nightscout
- This approach is more reliable across different CGM sources and Nightscout configurations
Testing
- Verified BG data loads successfully after the fix
- Graph displays correctly
- Delta and direction calculate properly
- Minutes ago updates as expected
Affected Users
Users running Nightscout-only configuration (no Dexcom Share) where their CGM/uploader doesn't populate the dateString field on entries.