{
  "name": "VitalTrends morning readiness brief",
  "nodes": [
    {
      "parameters": {
        "content": "## VitalTrends morning readiness brief\n\nRead-only template. It sends GET requests to the VitalTrends REST API and posts a three-line brief to Telegram.\n\n**Before you activate it**\n1. Create a **Header Auth** credential named `VitalTrends API key`: Name `Authorization`, Value `Bearer YOUR_VITALTRENDS_API_KEY`. Select it on both HTTP Request nodes.\n2. Create a **Telegram** credential with your bot token, select it on the Telegram node and replace `YOUR_TELEGRAM_CHAT_ID`.\n3. In **Workflow settings**, set the timezone to your VitalTrends profile timezone.\n4. Run it once manually, then activate it.\n\nKeep \"Allow workout write access\" off for the key this workflow uses. Not medical advice.\n\nDocs: https://vitaltrends.net/docs/automations",
        "height": 520,
        "width": 460,
        "color": 5
      },
      "id": "4f0c2d7e-1a3b-4c5d-8e9f-0a1b2c3d4e01",
      "name": "Read me",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -520,
        -180
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "triggerAtHour": 7,
              "triggerAtMinute": 30
            }
          ]
        }
      },
      "id": "4f0c2d7e-1a3b-4c5d-8e9f-0a1b2c3d4e02",
      "name": "Every morning at 07:30",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "url": "https://vitaltrends.net/api/v1/whoop/daily",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "start",
              "value": "={{ $now.minus({ days: 1 }).toFormat('yyyy-MM-dd') }}"
            },
            {
              "name": "per_page",
              "value": "5"
            }
          ]
        },
        "options": {
          "timeout": 30000
        }
      },
      "id": "4f0c2d7e-1a3b-4c5d-8e9f-0a1b2c3d4e03",
      "name": "Get WHOOP daily",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        220,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "VitalTrends API key"
        }
      }
    },
    {
      "parameters": {
        "url": "https://vitaltrends.net/api/v1/oura/daily-readiness",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "start",
              "value": "={{ $now.toFormat('yyyy-MM-dd') }}"
            },
            {
              "name": "end",
              "value": "={{ $now.toFormat('yyyy-MM-dd') }}"
            }
          ]
        },
        "options": {
          "timeout": 30000
        }
      },
      "id": "4f0c2d7e-1a3b-4c5d-8e9f-0a1b2c3d4e04",
      "name": "Get Oura readiness",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "VitalTrends API key"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "// Builds a green/yellow/red morning brief from VitalTrends data.\n// WHOOP recovery first (WHOOP's colors: green 67-100, yellow 34-66, red 0-33),\n// then Oura readiness as a fallback. The Oura thresholds below are template\n// defaults, not Oura's official bands. Edit them to suit you. Not medical advice.\nconst OURA_GREEN_MIN = 85;\nconst OURA_YELLOW_MIN = 70;\nconst SHORT_SLEEP_MINUTES = 360;\n\n// $now uses the workflow timezone. Set it to your VitalTrends profile timezone\n// in Workflow settings so \"today\" matches your local day.\nconst today = $now.toFormat('yyyy-MM-dd');\nconst whoopRows = $('Get WHOOP daily').first().json.data || [];\nconst ouraRows = $('Get Oura readiness').first().json.data || [];\n\nconst suggestions = {\n  GREEN: 'Suggestion: a normal or hard session fits today if you feel good.',\n  YELLOW: 'Suggestion: train if you like, but keep the hardest work moderate.',\n  RED: 'Suggestion: favor easy movement, recovery and an early night.',\n};\n\nconst fmtMinutes = (m) => (m === null || m === undefined)\n  ? 'unknown'\n  : `${Math.floor(m / 60)}h ${String(Math.round(m % 60)).padStart(2, '0')}m`;\n\nlet status = null;\nlet reason = null;\nlet details = null;\nlet source = null;\n\n// Use the row whose cycle_start_date is today and that has both recovery and sleep.\nconst whoop = whoopRows.find((r) => r.cycle_start_date === today\n  && r.recovery_score !== null && r.sleep_duration_minutes !== null);\n\nif (whoop) {\n  const score = Number(whoop.recovery_score);\n  status = score >= 67 ? 'GREEN' : score >= 34 ? 'YELLOW' : 'RED';\n  reason = `WHOOP recovery ${Math.round(score)}%`;\n  if (status === 'GREEN' && whoop.sleep_duration_minutes < SHORT_SLEEP_MINUTES) {\n    status = 'YELLOW';\n    reason += ' but short sleep';\n  }\n  const parts = [`Sleep ${fmtMinutes(whoop.sleep_duration_minutes)}`];\n  if (whoop.hrv_rmssd_milli !== null) parts.push(`HRV ${Math.round(whoop.hrv_rmssd_milli)} ms`);\n  if (whoop.resting_heart_rate !== null) parts.push(`resting HR ${whoop.resting_heart_rate} bpm`);\n  details = parts.join(', ');\n  source = 'whoop';\n} else {\n  const oura = ouraRows.find((r) => r.date === today && r.score !== null);\n  if (oura) {\n    status = oura.score >= OURA_GREEN_MIN ? 'GREEN' : oura.score >= OURA_YELLOW_MIN ? 'YELLOW' : 'RED';\n    reason = `Oura readiness ${oura.score}`;\n    const parts = [];\n    if (oura.temperature_deviation !== null) parts.push(`temperature deviation ${oura.temperature_deviation > 0 ? '+' : ''}${oura.temperature_deviation.toFixed(1)} °C`);\n    if (oura.contributor_hrv_balance !== null) parts.push(`HRV balance ${oura.contributor_hrv_balance}`);\n    if (oura.contributor_sleep_balance !== null) parts.push(`sleep balance ${oura.contributor_sleep_balance}`);\n    details = parts.length ? `Oura contributors: ${parts.join(', ')}` : 'No Oura contributor details yet.';\n    source = 'oura';\n  }\n}\n\nlet message;\nif (status) {\n  message = [\n    `Readiness ${today}: ${status} (${reason})`,\n    details,\n    `${suggestions[status]} Not medical advice.`,\n  ].join('\\n');\n} else {\n  message = [\n    `Readiness ${today}: NO DATA YET`,\n    'No WHOOP recovery or Oura readiness for today has synced to VitalTrends.',\n    'Try again after your device syncs.',\n  ].join('\\n');\n}\n\n// Only the short message leaves n8n. Raw API responses stay in this workflow.\nreturn [{ json: { date: today, status: status || 'NO_DATA', source, message } }];\n"
      },
      "id": "4f0c2d7e-1a3b-4c5d-8e9f-0a1b2c3d4e05",
      "name": "Build readiness brief",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        0
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "sendMessage",
        "chatId": "YOUR_TELEGRAM_CHAT_ID",
        "text": "={{ $json.message }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "id": "4f0c2d7e-1a3b-4c5d-8e9f-0a1b2c3d4e06",
      "name": "Send to Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        880,
        0
      ],
      "credentials": {
        "telegramApi": {
          "id": "REPLACE_WITH_YOUR_CREDENTIAL",
          "name": "Telegram bot"
        }
      }
    }
  ],
  "connections": {
    "Every morning at 07:30": {
      "main": [
        [
          {
            "node": "Get WHOOP daily",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get WHOOP daily": {
      "main": [
        [
          {
            "node": "Get Oura readiness",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Oura readiness": {
      "main": [
        [
          {
            "node": "Build readiness brief",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build readiness brief": {
      "main": [
        [
          {
            "node": "Send to Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
