{
 "name": "n8n of the week #3: n8n workflow #3: a warning before a TLS certificate expires",
 "nodes": [
  {
   "parameters": {
    "rule": {
     "interval": [
      {
       "field": "cronExpression",
       "expression": "0 8 * * *"
      }
     ]
    }
   },
   "id": "306c4fb6-dd1f-59a8-90ed-051756b020fe",
   "name": "Every morning",
   "position": [
    0,
    0
   ],
   "type": "n8n-nodes-base.scheduleTrigger",
   "typeVersion": 1.2
  },
  {
   "parameters": {
    "mode": "runOnceForAllItems",
    "jsCode": "// one item per domain: the next node runs once for each of them\nreturn ['example.com', 'www.example.com', 'mail.example.com']   // ← your domains\n  .map(domain => ({ json: { domain } }));"
   },
   "id": "f47af6ae-c946-5473-ae70-e411f596e4a0",
   "name": "My domains",
   "position": [
    240,
    0
   ],
   "type": "n8n-nodes-base.code",
   "typeVersion": 2
  },
  {
   "parameters": {
    "url": "={{ 'https://daniele-michelin.com/tools/api/tls.php?h=' + $json.domain }}",
    "sendHeaders": true,
    "headerParameters": {
     "parameters": [
      {
       "name": "User-Agent",
       "value": "n8n workflow (+https://daniele-michelin.com/n8n-workflows/)"
      }
     ]
    },
    "options": {
     "timeout": 20000
    }
   },
   "id": "17a1d2a3-37f1-5f2c-a7f8-93169f2800cb",
   "name": "Check certificate",
   "position": [
    480,
    0
   ],
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "onError": "continueRegularOutput"
  },
  {
   "parameters": {
    "mode": "runOnceForAllItems",
    "jsCode": "const warnDays = 21;                                        // ← warn this many days ahead\nconst domains = $('My domains').all().map(i => i.json.domain);   // same order as the answers\nconst bad = $input.all().map((i, n) => ({ ...i.json, host: i.json.host || domains[n] }))\n  .filter(c => !c.ok || c.status !== 'ok' || c.days < warnDays);\nif (!bad.length) return [];\nconst lines = bad.map(c => c.days !== undefined\n  ? `${c.host}: ${c.days} days left (until ${c.until})`\n  : `${c.host || '?'}: check failed (${c.status || c.error?.message || c.error || 'no answer'})`);\nreturn [{ json: { title: `${bad.length} certificate(s) need attention`, message: lines.join('\\n'), tags: ['lock'] } }];"
   },
   "id": "00f6ae4f-e99f-5330-9b57-9f7cec3d426e",
   "name": "Expiring soon?",
   "position": [
    720,
    0
   ],
   "type": "n8n-nodes-base.code",
   "typeVersion": 2
  },
  {
   "parameters": {
    "method": "POST",
    "url": "https://ntfy.sh",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ topic: 'your-secret-topic', title: $json.title, message: $json.message, click: $json.click, tags: $json.tags, attach: $json.attach }) }}",
    "options": {
     "timeout": 15000
    }
   },
   "id": "570dd53f-5317-503e-9d3c-ac15b7734a8a",
   "name": "Push to phone",
   "position": [
    960,
    0
   ],
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2
  }
 ],
 "connections": {
  "Every morning": {
   "main": [
    [
     {
      "node": "My domains",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "My domains": {
   "main": [
    [
     {
      "node": "Check certificate",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Check certificate": {
   "main": [
    [
     {
      "node": "Expiring soon?",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Expiring soon?": {
   "main": [
    [
     {
      "node": "Push to phone",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "executionOrder": "v1"
 },
 "pinData": {},
 "meta": {
  "source": "https://daniele-michelin.com/n8n-workflows/"
 }
}