This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Updated version of the very useful post: https://sixtymeters.com/automations/exporting-apple-health-data-to-home-assistant/ | |
// preferred key names | |
const fieldKeyLookup = { | |
qty: 'value', | |
Avg: 'avg', | |
Min: 'min', | |
Max: 'max', | |
} | |
msg.payload = msg.payload.data.metrics.map(metric => { | |
return metric.data | |
.map(measurement => { | |
const timestamp = Date.parse(measurement.date); | |
delete measurement.date; | |
var fields = {}; | |
Object.keys(measurement).forEach(e => { | |
if (typeof(measurement[e]) !== 'object') fields[fieldKeyLookup[e] || e] = measurement[e]; | |
}); | |
if (fields.min && fields.max && fields.min == fields.max) { | |
delete fields.min; | |
delete fields.max; | |
} | |
return { | |
measurement: metric.name, //active_energy | |
fields: fields, // {value: 5} or {systolic:120, diastolic: 70}, etc | |
tags: { | |
unit: metric.units //kJ | |
}, | |
timestamp: timestamp //2021-03-16 00:00:00 +0100 | |
} | |
}) | |
}) | |
.flat() // we've created array for each metric consisting of array of measurements | |
.filter(e => Object.keys(e.fields).length) // just in case, only return entries with fields | |
return msg; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note, this does not add measurements for notification metrics:
irregular_heart_rate_notifications
,high_heart_rate_notifications
,low_heart_rate_notifications
.These consist of arrays of
heartRate
and/orheartRateVariation
, which consist of: