{
  "id": "O4tdYnKCcAztd79t",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Elderwatch",
  "tags": [],
  "nodes": [
    {
      "id": "dbabdb9b-d503-4a9a-8c4b-9edddddfb7c3",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        0,
        0
      ],
      "parameters": {
        "path": "elderwatch",
        "options": {},
        "httpMethod": "POST"
      },
      "typeVersion": 2
    },
    {
      "id": "b69a0465-3219-44b2-b5ec-d27354fe41dd",
      "name": "Process & Flag Health",
      "type": "n8n-nodes-base.code",
      "position": [
        220,
        0
      ],
      "parameters": {
        "jsCode": "function safeParse(value, fallback = 0) {\n  const num = parseFloat(value);\n  return isNaN(num) ? fallback : num;\n}\n\nconst heartRate = safeParse($input.first().json.body.heartrate, 0);\nconst bloodOxygen = safeParse($input.first().json.body.bloodoxygen, 0);\nconst walkSymmetry = safeParse($input.first().json.body.walksymmetry, 0);\n\nlet alert = false;\nlet issues = [];\n\nif (heartRate > 100 || heartRate < 50) {\n  alert = true;\n  issues.push(`\u2764\ufe0f Heart rate out of range: ${heartRate.toFixed(0)} bpm`);\n}\nif (bloodOxygen < 92) {\n  alert = true;\n  issues.push(`\ud83e\ude78 Blood oxygen low: ${bloodOxygen.toFixed(0)}%`);\n}\nif (walkSymmetry < 0.9) {\n  alert = true;\n  issues.push(`\ud83d\udeb6 Walking symmetry off: ${walkSymmetry}`);\n}\n\nconst summary = `\ud83d\udccb Daily Health Report\n\n\u2764\ufe0f Heart Rate: ${heartRate.toFixed(0)} bpm\n\ud83e\ude78 Blood Oxygen: ${bloodOxygen.toFixed(0)}%\n\ud83d\udeb6 Walking Symmetry: ${walkSymmetry >= 0.9 ? \"Good\" : \"Needs Attention\"}\n\n${alert ? `\u26a0\ufe0f Attention Required:\\n${issues.join(\"\\n\")}` : \"\u2705 All vitals within range.\"}\n`;\n\nreturn [\n  {\n    json: {\n      summary,\n      attentionRequired: alert,\n      issues,\n      heartRate,\n      bloodOxygen,\n      walkSymmetry\n    }\n  }\n];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "8fcae627-33fd-424b-b4a2-d460727c6e17",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -40,
        -100
      ],
      "parameters": {
        "color": 4,
        "width": 400,
        "height": 260,
        "content": "## Webhook called from phone.  \nProcessor determines if health scores need immediate attention. "
      },
      "typeVersion": 1
    },
    {
      "id": "f8eee5b3-194b-474c-9e35-5b6b1f8837aa",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        680,
        -260
      ],
      "parameters": {
        "color": 4,
        "width": 540,
        "height": 260,
        "content": "## Twilio HTTP Calls API to make a call and also email."
      },
      "typeVersion": 1
    },
    {
      "id": "9c4ee01d-ca6d-4c0e-8a03-b6aee6233504",
      "name": "Attention Required?",
      "type": "n8n-nodes-base.if",
      "position": [
        420,
        0
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "fb74a5b5-d821-4f18-91fe-8f6b8a42cc45",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $json.attentionRequired }}",
              "rightValue": "=true"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "69e01ada-25e7-4b1b-961f-91053d827ba8",
      "name": "Twilio Call",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        740,
        -180
      ],
      "parameters": {
        "url": "https://api.twilio.com/2010-04-01/Accounts/<accountid>/Calls.json",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "sendQuery": true,
        "sendHeaders": true,
        "authentication": "genericCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "From",
              "value": "+1234567890"
            },
            {
              "name": "To",
              "value": "+1234567890"
            },
            {
              "name": "Url",
              "value": "=https://n8n.domain.com/webhook/twilio-call"
            }
          ]
        },
        "genericAuthType": "httpBasicAuth",
        "queryParameters": {
          "parameters": [
            {
              "name": "lead",
              "value": "=+447933713864"
            },
            {
              "name": "summary",
              "value": "={{ $('Process & Flag Health').item.json.summary }}"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "content-type",
              "value": "application/x-www-form-urlencoded"
            }
          ]
        }
      },
      "credentials": {
        "httpBasicAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "eb03f4b3-6d1a-499c-acaa-3705ea87aff0",
      "name": "Warning Email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1020,
        -180
      ],
      "parameters": {
        "sendTo": "user@example.com",
        "message": "=<h2>\ud83d\udccb Elder Watch \u2013 Daily Health Summary</h2>\n\n<p>Hello,</p>\n\n<p>Here is today\u2019s health update for your loved one:</p>\n\n<hr />\n\n<pre style=\"font-size: 15px; line-height: 1.6em;\">\n{{ $json.summary }}\n</pre>\n\n<hr />\n\n<p>\n{{ $json.attentionRequired === true \n  ? \"\u26a0\ufe0f One or more readings require attention. A phone alert has been triggered.\" \n  : \"\u2705 All vitals appear within normal range.\" \n}}\n</p>\n\n<p style=\"color: gray; font-size: 12px;\">\nThis report was automatically generated by the Elder Watch system.<br />\nBuilt with love and n8n by a human who has probably not blinked since the first webhook failed.\n</p>\n",
        "options": {},
        "subject": "=\u26a0\ufe0f Alert: Attention Needed \u2013 Vitals Out of Range for {{ $now.format('yyyy-MM-dd') }}"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "4ed32238-9a7d-4ccb-af71-60d402b817c8",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        700,
        60
      ],
      "parameters": {
        "color": 4,
        "width": 540,
        "height": 260,
        "content": "## Health vitals are ok -email to catetaker."
      },
      "typeVersion": 1
    },
    {
      "id": "2494fdb2-748f-4cf6-a51e-c986ce87f795",
      "name": "All Clear Gmail",
      "type": "n8n-nodes-base.gmail",
      "position": [
        760,
        160
      ],
      "parameters": {
        "sendTo": "user@example.com",
        "message": "=<h2>\ud83d\udccb Elder Watch \u2013 Daily Health Summary</h2>\n\n<p>Hello,</p>\n\n<p>Here is today\u2019s health update for your loved one:</p>\n\n<hr />\n\n<pre style=\"font-size: 15px; line-height: 1.6em;\">\n{{ $json.summary }}\n</pre>\n\n<hr />\n\n<p>\n{{ $json.attentionRequired === true \n  ? \"\u26a0\ufe0f One or more readings require attention. A phone alert has been triggered.\" \n  : \"\u2705 All vitals appear within normal range.\" \n}}\n</p>\n\n<p style=\"color: gray; font-size: 12px;\">\nThis report was automatically generated by the Elder Watch system.<br />\nBuilt with love and n8n by a human who has probably not blinked since the first webhook failed.\n</p>\n",
        "options": {},
        "subject": "=\u2705 All Clear: Health Summary for {{ $now.format('yyyy-MM-dd') }}"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    }
  ],
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "9c0a882c-27bc-4263-9fa0-11f6343957a9",
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Process & Flag Health",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Twilio Call": {
      "main": [
        [
          {
            "node": "Warning Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Attention Required?": {
      "main": [
        [
          {
            "node": "Twilio Call",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "All Clear Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process & Flag Health": {
      "main": [
        [
          {
            "node": "Attention Required?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}