{
  "name": "Daily Wisen Report (Edificio Pernet & Cartagena)",
  "nodes": [
    {
      "parameters": {},
      "id": "e45cd0a2-2b6d-49d7-83eb-275d2ccde120",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        220,
        240
      ]
    },
    {
      "parameters": {
        "url": "https://dashboard-keller.tailc6b821.ts.net/src/pernet_data.json"
      },
      "id": "2719a71a-297c-4ab4-abaa-ecf0de5ba887",
      "name": "HTTP Request Pernet",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        440,
        140
      ]
    },
    {
      "parameters": {
        "url": "https://dashboard-keller.tailc6b821.ts.net/src/cartagena_data.json"
      },
      "id": "dbf2fbea-98ab-4ae9-ab08-011ca14de5b3",
      "name": "HTTP Request Cartagena",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        440,
        340
      ]
    },
    {
      "parameters": {
        "mode": "multiplex"
      },
      "id": "3be9faeb-72ee-4874-9b2f-981881723f5b",
      "name": "Merge Data",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2,
      "position": [
        660,
        240
      ]
    },
    {
      "parameters": {
        "jsCode": "const pernet = $input.item(0) ? $input.item(0).json : null;\nconst cartagena = $input.item(1) ? $input.item(1).json : null;\nconst cartagenaTilts = $input.item(2) ? $input.item(2).json : null;\n\n// Helpers para analizar el cambio en 24h\nfunction detectSignificantChanges(dataArray, projectName) {\n  if (!dataArray || !Array.isArray(dataArray) || dataArray.length < 2) return `No hay suficientes datos para ${projectName}.`;\n  \n  const now = dataArray[dataArray.length - 1];\n  const past = dataArray[0]; // Hace 24h aprox\n  \n  let report = `\\n--- An\u00e1lisis de ${projectName} ---\\n`;\n  let foundChanges = false;\n  \n  for (let i = 0; i < now.cells.length; i++) {\n    const diff = now.cells[i] - past.cells[i];\n    if (Math.abs(diff) > 0.2) {\n      report += `Sensor/Celda ${i+1}: Lectura Actual: ${now.cells[i].toFixed(2)}, Lectura Previa (24h): ${past.cells[i].toFixed(2)}, Variaci\u00f3n: ${diff.toFixed(2)}\\n`;\n      foundChanges = true;\n    }\n  }\n  \n  if (!foundChanges) {\n    report += \"No se detectaron movimientos significativos en las \u00faltimas 24 horas.\\n\";\n  }\n  \n  return report;\n}\n\nlet finalText = \"\";\nif (pernet) finalText += detectSignificantChanges(pernet, \"Edificio Pernet\");\nif (cartagena) finalText += detectSignificantChanges(cartagena, \"Cartagena (Rambla Jet)\");\nif (cartagenaTilts) finalText += detectSignificantChanges(cartagenaTilts, \"Cartagena (Inclin\u00f3metros / Tilts)\");\n\nreturn {\n  json: {\n    analysisText: finalText\n  }\n};"
      },
      "id": "0d9c1db1-fa08-4e8c-a110-38fc7e98a32d",
      "name": "Analyze Changes (Code)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        860,
        240
      ]
    },
    {
      "parameters": {
        "model": "gpt-4",
        "messages": {
          "messageValues": [
            {
              "content": "Eres un ingeniero geot\u00e9cnico experto analizando datos de auscultaci\u00f3n (movimientos de tierra, asientos, etc) para Keller Cimentaciones.\nTe pasar\u00e9 los datos de las variaciones de las \u00faltimas 24 horas de los sensores en obra. \n\nDebes generar un informe formal en HTML (con etiquetas h2, p, ul, b) que yo pueda enviar directamente por email a los responsables. Estructura exactamente con estas secciones:\n\n<h2>Resumen Ejecutivo</h2>\n<h2>Interpretaci\u00f3n T\u00e9cnica</h2>\n<h2>Recomendaciones</h2>\n<h2>Riesgo Estimado</h2>\n\nNo incluyas markdown (como ```html), solo el HTML puro. Usa colores para enfatizar alertas si el riesgo es alto.\n\nAqu\u00ed tienes los datos detectados:\n{{ $json.analysisText }}"
            }
          ]
        }
      },
      "id": "12fc5b80-874e-4e31-90a6-42d627ee28fb",
      "name": "OpenAI Report Generator",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1,
      "position": [
        1080,
        240
      ],
      "credentials": {
        "openAiApi": {
          "id": "",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "fromEmail": "tu-correo@gmail.com",
        "toEmail": "daniel.galiano@keller.com",
        "subject": "={{ 'Reporte de Auscultaci\u00f3n Diario - ' + $now.format('yyyy-MM-dd') }}",
        "html": "={{ $json.message.content }}"
      },
      "id": "bcda8099-a1fc-4bf8-aac6-3c66f97ef8b6",
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1300,
        240
      ],
      "credentials": {
        "smtp": {
          "id": "",
          "name": "SMTP account"
        }
      }
    },
    {
      "parameters": {
        "url": "https://dashboard-keller.tailc6b821.ts.net/src/cartagena_tilts.json"
      },
      "id": "733457e8-fcc4-430f-8fbe-0a1f717e7834",
      "name": "HTTP Request Cartagena Tilts",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        440,
        540
      ]
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "HTTP Request Pernet",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP Request Cartagena",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP Request Cartagena Tilts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request Pernet": {
      "main": [
        [
          {
            "node": "Merge Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request Cartagena": {
      "main": [
        [
          {
            "node": "Merge Data",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge Data": {
      "main": [
        [
          {
            "node": "Analyze Changes (Code)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analyze Changes (Code)": {
      "main": [
        [
          {
            "node": "OpenAI Report Generator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Report Generator": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request Cartagena Tilts": {
      "main": [
        [
          {
            "node": "Merge Data",
            "type": "main",
            "index": 2
          }
        ]
      ]
    }
  },
  "settings": {}
}