{
  "name": "Home Assistant Automation Monitor",
  "nodes": [
    {
      "parameters": {},
      "id": "160feea6-5fbe-4ac7-a62d-3afe01cdc2c5",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [336, 208]
    },
    {
      "parameters": {
        "resource": "state",
        "operation": "getAll",
        "returnAll": true
      },
      "id": "40464eca-4945-4989-bcb0-d2434ba11512",
      "name": "Get All Automations",
      "type": "n8n-nodes-base.homeAssistant",
      "typeVersion": 1,
      "position": [560, 208],
      "credentials": {
        "homeAssistantApi": {
          "id": "1qd5uzNGUb4R1zq9",
          "name": "Home Assistant account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Format automation data for display\nconst automations = $input.all();\n\nconst formatted = automations.map(item => {\n  const entity = item.json;\n  const entityId = entity.entity_id;\n  const state = entity.state;\n  const attributes = entity.attributes || {};\n\n  const friendlyName = attributes.friendly_name || entityId;\n  const lastTriggered = attributes.last_triggered || null;\n\n  let lastTriggeredFormatted = 'Never triggered';\n  if (lastTriggered) {\n    lastTriggeredFormatted = new Date(lastTriggered).toLocaleString();\n  }\n\n  return {\n    json: {\n      entityId,\n      friendlyName,\n      state,\n      isActive: state === 'on',\n      lastTriggered,\n      lastTriggeredFormatted,\n      attributes\n    }\n  };\n});\n\n// Sort by last triggered (most recent first)\nformatted.sort((a,b) => {\n  const aTime = a.json.lastTriggered ? new Date(a.json.lastTriggered) : 0;\n  const bTime = b.json.lastTriggered ? new Date(b.json.lastTriggered) : 0;\n  return bTime - aTime;\n});\n\nreturn formatted;"
      },
      "id": "2928c3b3-7121-4f6e-9116-e397bc1dcf5d",
      "name": "Format Automation List",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [784, 208]
    },
    {
      "parameters": {
        "jsCode": "const automations = $input.all();\n\nfunction escapeMarkdown(text){\n  if(!text) return 'N/A';\n  return String(text).replace(/([_*[\\]()~`>#+\\-=|{}.!])/g,'\\\\$1');\n}\n\nlet output = '🏠 *Active Home Assistant Automations*\\n';\noutput += '══════════════════════════\\n\\n';\n\nconst activeAutomations = automations\n  .filter(a => a.json.state === 'on')\n  .sort((a,b) => {\n    const aTime = a.json.lastTriggered ? new Date(a.json.lastTriggered) : 0;\n    const bTime = b.json.lastTriggered ? new Date(b.json.lastTriggered) : 0;\n    return bTime - aTime;\n  });\n\nif(activeAutomations.length===0){\n  output += 'No active automations found.\\n';\n}else{\n  activeAutomations.forEach((a,i)=>{\n    const name = escapeMarkdown(a.json.friendlyName || a.json.entityId);\n    const entityId = escapeMarkdown(a.json.entityId);\n    const lastFormatted = escapeMarkdown(a.json.lastTriggeredFormatted || 'Never triggered');\n\n    output += `*${i+1}\\\\. ${name}*\\n`;\n    output += `Entity: ${entityId}\\n`;\n    output += `Last Run: ${lastFormatted}\\n\\n`;\n  });\n}\n\noutput += '══════════════════════════\\n';\noutput += `📊 Total Active: ${activeAutomations.length}\\n`;\n\n// Split long message for Telegram\nconst MAX_LENGTH = 4000;\nlet messages = [];\nlet currentMessage = '';\nconst lines = output.split('\\n');\nfor(const line of lines){\n  if((currentMessage + line + '\\n').length > MAX_LENGTH){\n    messages.push({ json:{ output: currentMessage } });\n    currentMessage = '';\n  }\n  currentMessage += line + '\\n';\n}\nif(currentMessage){\n  messages.push({ json:{ output: currentMessage } });\n}\n\nreturn messages;"
      },
      "id": "b32fbb2a-a42a-409d-be76-134e2c94fd01",
      "name": "Display Results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1088, 208]
    },
    {
      "parameters": {
        "chatId": "-1003870868518",
        "text": "={{$json.output}}",
        "additionalFields": {
          "parse_mode": "MarkdownV2"
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [1360, 208],
      "id": "c2cb1baa-3f60-4218-961e-4876bcabdef5",
      "name": "Send a text message",
      "webhookId": "a3de3c16-8a09-4487-9e3c-aff08debfaa1",
      "credentials": {
        "telegramApi": {
          "id": "WXTyetoJLOgPlvCo",
          "name": "Home Assistant Update"
        }
      }
    }
  ],
  "connections": {
    "Manual Trigger": { "main": [[{ "node": "Get All Automations", "type": "main", "index": 0 }]] },
    "Get All Automations": { "main": [[{ "node": "Format Automation List", "type": "main", "index": 0 }]] },
    "Format Automation List": { "main": [[{ "node": "Display Results", "type": "main", "index": 0 }]] },
    "Display Results": { "main": [[{ "node": "Send a text message", "type": "main", "index": 0 }]] }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "all",
    "saveManualExecutions": true,
    "saveExecutionProgress": true,
    "callerPolicy": "workflowsFromSameOwner",
    "availableInMCP": false
  }
}