DEV Community

Cover image for How I Use AI Agents with n8n to Automate Medical Data Analysis (PostgreSQL + Apple Health)
Ciphernutz
Ciphernutz

Posted on

How I Use AI Agents with n8n to Automate Medical Data Analysis (PostgreSQL + Apple Health)

Hey folks,
Today I want to share something cool that I recently built, a fully automated workflow that pulls my Apple Health data & stores it in PostgreSQL.

It started with a simple goal,
I wanted to stop manually digging through health dashboards and CSV exports just to understand how my body was performing week to week.

So, I decided to automate it and the combo of n8n, local LLMs, and structured data made it surprisingly smooth.

In this blog, I’ll show how I used these tools together to build an AI-powered health analysis workflow.

Tech Stack Overview

Step-by-Step Architecture

1. Export Apple Health Data
Apple doesn’t provide real-time HealthKit APIs to third parties. But you can export your full health dataset manually:

  • Open Apple Health app → Profile → Export Health Data
  • It will generate a ZIP with export.xml inside

2. Parse XML & Store in PostgreSQL
We used a simple Python script (called by an n8n workflow) to parse the XML into a usable format.

import xml.etree.ElementTree as ET
import psycopg2

def parse_health_data(file_path, conn):
    tree = ET.parse(file_path)
    root = tree.getroot()
    cursor = conn.cursor()

    for record in root.findall('Record'):
        record_type = record.attrib['type']
        value = record.attrib.get('value', None)
        start_date = record.attrib.get('startDate', None)

        cursor.execute(
            "INSERT INTO apple_health_data (type, value, start_date) VALUES (%s, %s, %s)",
            (record_type, value, start_date)
        )

    conn.commit()
Enter fullscreen mode Exit fullscreen mode

We scheduled this to run every time we drop a new export.xml in a watched Dropbox folder (via n8n).

3. n8n Workflow: Health Data Pipeline

Here’s what the workflow does:

1. Trigger – Watch a folder for new Apple Health exports
2. Unzip & Parse – Call the Python parser above
3. Store – Insert into PostgreSQL using the PostgreSQL node
4. Call AI Agent – Send parsed data summary to Ollama for analysis

  1. Use Ollama for AI Health Insights Once the data is in PostgreSQL, we query it using n8n and send it to Ollama’s local LLM instance:

n8n HTTP Node → Ollama

curl http://localhost:11434/api/generate -d '{
  "model": "mistral",
  "prompt": "Based on this dataset:\n\n<insert data>\n\nWhat are some health patterns or trends?"
}'

Enter fullscreen mode Exit fullscreen mode

Example response from Ollama:

“On Thursdays, your step count consistently drops while heart rate variability increases, this may indicate stress accumulation. Consider shifting workout intensity earlier in the week.”

That insight was spot-on, it helped me change my schedule.

Sample Query in PostgreSQL

SELECT
  type,
  AVG(CAST(value AS FLOAT)) as avg_value,
  DATE(start_date) as date
FROM
  apple_health_data
WHERE
  type = 'HKQuantityTypeIdentifierStepCount'
GROUP BY
  date, type
ORDER BY
  date DESC;

Enter fullscreen mode Exit fullscreen mode

You can schedule this as a cron-based report via n8n and attach a monthly summary with charts.

Results & Observations

- Trends I’d never noticed: e.g., low sleep affecting weekend performance
- AI-generated summaries: Fast, contextual feedback
- Fully local + privacy-first: No third-party data leaks

Challenges

  • Apple Health export is not real-time and the XML is deeply nested
  • Token limits when sending too much data to Ollama (chunking helps)
  • Building prompt templates that don’t “hallucinate” trends

Next Steps

  • Sync with real-time trackers (e.g., Oura Ring, Apple Watch APIs via iOS Shortcuts)
  • Visualize trends via Supabase dashboards or Metabase
  • Create AI health assistants that talk to you — “You’ve been sitting too long.”

And if you're planning to build something similar for your clients or internal apps, you might want to hire AI agent developers who can plug into this stack easily.

Wrapping Up

This stack gave me actionable, AI-powered health insights, without giving up data privacy or writing thousands of lines of code.

If you’re a builder who loves automation and wants to explore AI + health, I’d love to hear your take or collaborate.

Top comments (0)