# How do I let an AI agent read my sleep and HRV data?

Source: https://healthsql.app/sleep-hrv · Updated 2026-11-30

Give the agent HealthSQL: it keeps your Apple Health record in a local SQLite database where the `nights` view has one row per night (minutes asleep, deep, REM, core and awake, when you fell asleep and woke) and the `daily` view has each day's heart rate variability and resting heart rate. The agent reads them with SQL, from a shell or over MCP.

HealthSQL is an iPhone app and a command-line tool that copy your whole Apple Health record into a local SQLite database on your computer, so AI agents can query it with SQL or MCP. Setup takes one command on your Mac: [setup](https://healthsql.app/setup).

## What the agent gets

- **`nights`**: one row per night, from one source: the one with sleep stages, and if two have stages, the longer. Columns: `night`, `asleep_min`, `deep_min`, `rem_min`, `core_min`, `awake_min`, `in_bed_min`, `fell_asleep`, `woke`, `source_name`.
- **`nights_by_source`**: the same columns for every source that recorded the night, such as a Watch and a ring.
- **`daily`**: one row per day with `hrv_ms` (Apple Health's heart rate variability, SDNN, in milliseconds), `resting_hr`, `respiratory_rate`, `wrist_temp_c`, and the sleep columns from `nights`.
- **`sleep`**: every sleep-stage interval, from every source, for questions about the time of night.

Night N is the night that ends on the morning of day N; a stage that starts at 18:00 or later belongs to the next day's night. Every view and column: [schema reference](https://healthsql.app/docs/schema).

## Queries the agent can run

The last two weeks of sleep:

```sql
SELECT night, asleep_min, deep_min, rem_min, awake_min, fell_asleep, woke FROM nights ORDER BY night DESC LIMIT 14
```

HRV and resting heart rate, this week against the four weeks before:

```sql
SELECT CASE WHEN day >= date('now', '-7 day') THEN 'last 7 days' ELSE 'the 4 weeks before' END AS period,
       round(avg(hrv_ms), 1) AS hrv_ms, round(avg(resting_hr), 1) AS resting_hr
FROM daily WHERE day >= date('now', '-35 day') GROUP BY period
```

Sleep against the next morning's HRV:

```sql
SELECT day, asleep_min, deep_min, hrv_ms, resting_hr FROM daily
WHERE day >= date('now', '-60 day') AND asleep_min IS NOT NULL ORDER BY day
```

Nights after a late workout:

```sql
SELECT w.day, w.activity, time(w.start_local) AS started, n.asleep_min, n.deep_min
FROM workouts w JOIN nights n ON n.night = date(w.day, '+1 day')
WHERE time(w.start_local) >= '19:00' ORDER BY w.day DESC LIMIT 20
```

## Questions to ask

- "How did I sleep this week compared with the last month?"
- "Is my HRV lower on days after I sleep less than six hours?"
- "Do late workouts change my deep sleep?"
- "When do I fall asleep on average, weekdays against weekends?"

## Where the numbers come from

- **HRV** in Apple Health is SDNN, from an Apple Watch or another device that writes it. Some wearables measure HRV another way and do not write it to Health; [WHOOP's help page](https://healthsql.app/whoop) says so of WHOOP, and [Oura's list](https://healthsql.app/oura) does not include it.
- **Sleep** comes from whatever writes sleep to Health: a Watch, a ring, an app. `nights` uses one source per night so minutes are not added twice.
- **Freshness:** last night's sleep reaches your Mac after you unlock your iPhone in the morning; every answer says when the phone last synced.

HealthSQL is not a medical device. An agent can describe what your data shows; questions about your health belong with a clinician.

## Questions

### Which agents can do this?

Any agent with a shell or MCP: [Claude Code](https://healthsql.app/claude-code), [Codex](https://healthsql.app/codex), [Cursor](https://healthsql.app/cursor), Claude Desktop, [local models](https://healthsql.app/local-models), or an [always-on agent](https://healthsql.app/personal-agents) that sends you a morning summary.

### My HRV column is empty. Why?

`hrv_ms` holds Apple Health's own HRV. If no device writes HRV to Health, the column stays empty, because unknown is not zero.

### Can I see one device's sleep?

Yes: `nights_by_source` has a row for each source, and `WHERE source_name LIKE '%Oura%'` (or your device's name) picks one.
