Skip to content

Instantly share code, notes, and snippets.

@wivaku
Last active April 29, 2024 01:14
Show Gist options
  • Save wivaku/2d0dde069b4575a85e2d0c8f90342043 to your computer and use it in GitHub Desktop.
Save wivaku/2d0dde069b4575a85e2d0c8f90342043 to your computer and use it in GitHub Desktop.
// 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;
@wivaku
Copy link
Author

wivaku commented May 17, 2021

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/or heartRateVariation, which consist of:

  • hr: 70 (or hrv: 100)
  • units: bps (or: units: ms)
  • timestamp: {start, interval, end}

@mayorgergich
Copy link

This appears to be broken, unsure if just me but it was working fine until a few days ago.

@jsheph
Copy link

jsheph commented Apr 29, 2024

Thanks for creating and sharing this. This worked great in Node Red to consume data from Auto Health Export.

If others are following the guide from, https://sixtymeters.com/automations/exporting-apple-health-data-to-home-assistant/, make sure to set "Time Precision" under Advanced Query Options in Node Red's influx batch to Nanoseconds. Otherwise, time will be treated as null and your data will display around the 1970's rather than whatever date your export was set to.

I forked this gist with a small change to compensate for the nanoseconds time requirement on influx 1.x if ever needed.

https://gist.github.com/jsheph/a86abfceb31740409c101134c138d218

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment