{
  "updatedAt": "2026-05-07T03:32:35.930Z",
  "createdAt": "2026-05-07T03:32:32.026Z",
  "id": "GXtkp4xKka90XrXH",
  "name": "Grafana Alert \u2192 AI \u2192 Gotify v1.2",
  "description": null,
  "active": true,
  "isArchived": false,
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "grafana-alert",
        "options": {}
      },
      "id": "a6e7dfb1-8e9b-4048-b028-4383c30a7e0d",
      "name": "Grafana Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "webhookId": "0492788e-db4e-4d15-9572-9d6c3efc80c3"
    },
    {
      "parameters": {
        "jsCode": "// Parse Grafana alert payload into a clean summary for the LLM\nconst alerts = $input.first().json.body?.alerts || $input.first().json.alerts || [];\n\nconst parsed = alerts.map(a => ({\n  status: a.status || 'unknown',\n  alertName: a.labels?.alertname || 'Unnamed Alert',\n  host: a.labels?.host || a.labels?.instance || 'unknown host',\n  severity: a.labels?.severity || 'warning',\n  summary: a.annotations?.summary || '',\n  description: a.annotations?.description || '',\n  value: a.valueString || '',\n  startsAt: a.startsAt || '',\n  endsAt: a.endsAt || ''\n}));\n\nconst alertText = parsed.map(a => \n  `[${a.status.toUpperCase()}] ${a.alertName} on ${a.host}\\nSeverity: ${a.severity}\\nSummary: ${a.summary}\\nDescription: ${a.description}\\nValue: ${a.value}\\nStarted: ${a.startsAt}`\n).join('\\n---\\n');\n\nreturn [{\n  json: {\n    alertText,\n    alertCount: parsed.length,\n    hasResolved: parsed.some(a => a.status === 'resolved'),\n    hasFiring: parsed.some(a => a.status === 'firing'),\n    parsed\n  }\n}];"
      },
      "id": "ea216058-69db-42cc-88d0-8f67e1b8951b",
      "name": "Parse Alert Payload",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        224,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://10.5.30.6:4000/v1/chat/completions",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Authorization",
              "value": "={{'Bearer ' + $env.LITELLM_API_KEY}}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{\n  {\n    \"model\": \"medium\",\n    \"messages\": [\n      {\n        \"role\": \"system\",\n        \"content\": \"You are a concise homelab monitoring assistant. Summarize infrastructure alerts in 2-3 sentences. Include: what happened, which host, severity, and a suggested action. Keep it brief \u2014 this goes to a phone notification.\"\n      },\n      {\n        \"role\": \"user\",\n        \"content\": \"Summarize this Grafana alert:\\n\\n\" + $('Parse Alert Payload').first().json.alertText\n      }\n    ],\n    \"max_tokens\": 200,\n    \"temperature\": 0.3\n  }\n}}",
        "options": {}
      },
      "id": "8ef1aa9d-d5b1-41ea-abc6-703b6491ea33",
      "name": "LiteLLM Summarize",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1792,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const response = $input.first().json;\nconst summary = response.choices?.[0]?.message?.content || 'Could not generate summary';\nconst alertData = $('Parse Alert Payload').first().json;\n\nconst title = alertData.hasFiring \n  ? `\ud83d\udd25 Alert Firing (${alertData.alertCount})` \n  : `\u2705 Alert Resolved (${alertData.alertCount})`;\n\nconst priority = alertData.hasFiring ? 8 : 2;\n\nreturn [{\n  json: {\n    title,\n    message: summary,\n    priority\n  }\n}];"
      },
      "id": "2e969bca-8458-4477-a358-664fd1c50c4f",
      "name": "Format Notification",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2016,
        0
      ]
    },
    {
      "parameters": {
        "message": "={{ $json.message }}",
        "additionalFields": {
          "priority": "={{ $json.priority }}",
          "title": "={{ $json.title }}"
        },
        "options": {
          "contentType": "text/plain"
        }
      },
      "id": "65c2b53a-6e03-4460-9aa6-5d967d244de0",
      "name": "Push to Gotify",
      "type": "n8n-nodes-base.gotify",
      "typeVersion": 1,
      "position": [
        2224,
        0
      ],
      "credentials": {
        "gotifyApi": {
          "id": "DXdYZDfVZecDTaNU",
          "name": "Gotify account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const parsed = $('Parse Alert Payload').first().json;\nconst now = new Date();\nconst hour = now.getHours();\nconst fingerprint = (parsed.parsed || [])\n  .map(a => `${a.alertName}:${a.host}`)\n  .sort().join('|') || 'unknown';\nreturn [{ json: { ...parsed, fingerprint, isNightTime: hour >= 0 && hour < 7 } }];\n"
      },
      "id": "df26d9ca-c72c-4dee-9327-14a0f5fcf3a0",
      "name": "Build Fingerprint",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        448,
        0
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT fingerprint, last_seen FROM grafana_alert_dedup WHERE fingerprint = '{{ $json.fingerprint }}' AND last_seen > NOW() - INTERVAL '30 minutes' LIMIT 1;",
        "options": {}
      },
      "id": "3fd78a53-7885-4739-a4b7-b441a27e993e",
      "name": "Lookup Dedup",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        672,
        0
      ],
      "credentials": {
        "postgres": {
          "id": "n9svoXemqSZoNNUB",
          "name": "Postgres account"
        }
      },
      "continueOnFail": true
    },
    {
      "parameters": {
        "jsCode": "const alertData = $('Build Fingerprint').first().json;\nconst rows = $input.all().filter(r => r.json && r.json.fingerprint);\nconst alreadySeen = rows.length > 0;\nconst isLowPriority = !alertData.hasFiring;\nconst suppressNight = alertData.isNightTime && isLowPriority;\nconst suppress = alreadySeen || suppressNight;\nreturn [{ json: { ...alertData, suppress, shouldNotify: suppress ? 'no' : 'yes', suppressReason: alreadySeen ? 'dedup' : suppressNight ? 'night_suppress' : null } }];\n"
      },
      "id": "c34b4b0e-1a47-4624-b1c4-9ef6b5cdeb6c",
      "name": "Check Should Notify",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        896,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "cc41d77c-b659-4c6f-8e71-96a13dced242",
              "leftValue": "={{ $json.shouldNotify }}",
              "rightValue": "yes",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "29929dc8-17f6-4cc8-90c2-c501eaba8be5",
      "name": "Should Notify?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1120,
        0
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO grafana_alert_dedup (fingerprint, last_seen, alert_name, host) VALUES ('{{ $json.fingerprint }}', NOW(), '{{ $json.dedupAlertName }}', '{{ $json.dedupHost }}') ON CONFLICT (fingerprint) DO UPDATE SET last_seen = NOW();",
        "options": {}
      },
      "id": "25a14a6a-db17-49ca-a6d2-6ed2f97d2889",
      "name": "Upsert Dedup Record",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1568,
        0
      ],
      "credentials": {
        "postgres": {
          "id": "n9svoXemqSZoNNUB",
          "name": "Postgres account"
        }
      },
      "continueOnFail": true
    },
    {
      "parameters": {
        "jsCode": "const d = $input.first().json;\nconst first = (d.parsed || [])[0] || {};\nreturn [{ json: {\n  fingerprint: d.fingerprint,\n  dedupAlertName: first.alertName || 'unknown',\n  dedupHost: first.host || 'unknown'\n} }];"
      },
      "id": "54705c76-512d-4479-889f-dc0a3c24d1b3",
      "name": "Prepare Dedup Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1344,
        0
      ]
    }
  ],
  "connections": {
    "Grafana Webhook": {
      "main": [
        [
          {
            "node": "Parse Alert Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Alert Payload": {
      "main": [
        [
          {
            "node": "Build Fingerprint",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Fingerprint": {
      "main": [
        [
          {
            "node": "Lookup Dedup",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Lookup Dedup": {
      "main": [
        [
          {
            "node": "Check Should Notify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Should Notify": {
      "main": [
        [
          {
            "node": "Should Notify?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Should Notify?": {
      "main": [
        [
          {
            "node": "Prepare Dedup Data",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Upsert Dedup Record": {
      "main": [
        [
          {
            "node": "LiteLLM Summarize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "LiteLLM Summarize": {
      "main": [
        [
          {
            "node": "Format Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Notification": {
      "main": [
        [
          {
            "node": "Push to Gotify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Dedup Data": {
      "main": [
        [
          {
            "node": "Upsert Dedup Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "pinData": {},
  "versionId": "ba3a4eab-60b8-4b1c-ac9f-07a0fdcec9c0",
  "activeVersionId": "ba3a4eab-60b8-4b1c-ac9f-07a0fdcec9c0",
  "versionCounter": 10,
  "triggerCount": 1,
  "tags": [
    {
      "updatedAt": "2026-05-06T22:38:51.678Z",
      "createdAt": "2026-05-06T22:38:51.678Z",
      "id": "S9FxzVfHLsHmyzyt",
      "name": "ai"
    },
    {
      "updatedAt": "2026-05-06T22:38:51.660Z",
      "createdAt": "2026-05-06T22:38:51.660Z",
      "id": "0ETpkL5jJ5wwgt8k",
      "name": "monitoring"
    }
  ],
  "shared": [
    {
      "updatedAt": "2026-05-07T03:32:32.026Z",
      "createdAt": "2026-05-07T03:32:32.026Z",
      "role": "workflow:owner",
      "workflowId": "GXtkp4xKka90XrXH",
      "projectId": "dxCRnBdX5uJizCGa",
      "project": {
        "updatedAt": "2026-05-06T01:10:37.484Z",
        "createdAt": "2026-05-06T01:08:07.721Z",
        "id": "dxCRnBdX5uJizCGa",
        "name": "Wilfred Fizzlepoof <admin@paccoco.com>",
        "type": "personal",
        "icon": null,
        "description": null,
        "creatorId": "47b2227f-2fc2-4a08-bd35-ea157b92df0d"
      }
    }
  ],
  "versionMetadata": {
    "name": "Version ba3a4eab",
    "description": ""
  }
}