Fix body battery array parsing - use index 1 not 2
All checks were successful
Deploy / deploy (push) Successful in 2m39s

The Garmin API returns bodyBatteryValuesArray as [timestamp, level]
tuples (2 elements), not [timestamp, status, level, quality] (4 elements).
Was accessing index 2 which doesn't exist, now correctly using index 1.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-15 14:19:27 +00:00
parent c080e7054d
commit 923b5fdb01
2 changed files with 12 additions and 16 deletions

View File

@@ -342,9 +342,9 @@ describe("fetchBodyBattery", () => {
charged: 45, charged: 45,
drained: 30, drained: 30,
bodyBatteryValuesArray: [ bodyBatteryValuesArray: [
[1705190400000, "charging", 25, 1.0], [1705190400000, 25],
[1705194000000, "draining", 40, 1.0], [1705194000000, 40],
[1705197600000, "charging", 35, 1.0], [1705197600000, 35],
], ],
}, },
{ {
@@ -352,8 +352,8 @@ describe("fetchBodyBattery", () => {
charged: 85, charged: 85,
drained: 60, drained: 60,
bodyBatteryValuesArray: [ bodyBatteryValuesArray: [
[1705276800000, "charging", 65, 1.0], [1705276800000, 65],
[1705280400000, "draining", 85, 1.0], [1705280400000, 85],
], ],
}, },
]), ]),
@@ -432,7 +432,7 @@ describe("fetchBodyBattery", () => {
mockJsonResponse([ mockJsonResponse([
{ {
date: "2024-01-15", date: "2024-01-15",
bodyBatteryValuesArray: [[1705276800000, "charging", 70, 1.0]], bodyBatteryValuesArray: [[1705276800000, 70]],
}, },
]), ]),
); );

View File

@@ -137,31 +137,27 @@ export async function fetchBodyBattery(
); );
return { current: null, yesterdayLow: null }; return { current: null, yesterdayLow: null };
} }
// Response structure: bodyBatteryValuesArray is [[timestamp, level], ...]
// Confirmed by bodyBatteryValueDescriptorDTOList in API response
const data = JSON.parse(text) as Array<{ const data = JSON.parse(text) as Array<{
date: string; date: string;
bodyBatteryValuesArray?: Array<[number, string, number, number]>; bodyBatteryValuesArray?: Array<[number, number]>;
}>; }>;
// Debug: log the raw response structure
logger.info(
{ rawData: JSON.stringify(data).slice(0, 2000), date, yesterday },
"Body battery raw API response",
);
// Find today's and yesterday's data from the response array // Find today's and yesterday's data from the response array
const todayData = data?.find((d) => d.date === date); const todayData = data?.find((d) => d.date === date);
const yesterdayData = data?.find((d) => d.date === yesterday); const yesterdayData = data?.find((d) => d.date === yesterday);
// Current = last value in today's bodyBatteryValuesArray (index 2 is the level) // Current = last value in today's bodyBatteryValuesArray (index 1 is the level)
const todayValues = todayData?.bodyBatteryValuesArray ?? []; const todayValues = todayData?.bodyBatteryValuesArray ?? [];
const current = const current =
todayValues.length > 0 ? todayValues[todayValues.length - 1][2] : null; todayValues.length > 0 ? todayValues[todayValues.length - 1][1] : null;
// Yesterday low = minimum level in yesterday's bodyBatteryValuesArray // Yesterday low = minimum level in yesterday's bodyBatteryValuesArray
const yesterdayValues = yesterdayData?.bodyBatteryValuesArray ?? []; const yesterdayValues = yesterdayData?.bodyBatteryValuesArray ?? [];
const yesterdayLow = const yesterdayLow =
yesterdayValues.length > 0 yesterdayValues.length > 0
? Math.min(...yesterdayValues.map((v) => v[2])) ? Math.min(...yesterdayValues.map((v) => v[1]))
: null; : null;
logger.info( logger.info(