{
  "name": "10 Kill switch and daily cap",
  "nodes": [
    {
      "id": "c7f7b1e3-24f5-46b5-9579-bedcfe54353d",
      "name": "Called by another flow",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        300
      ],
      "parameters": {
        "inputSource": "workflowInputs",
        "workflowInputs": {
          "values": [
            {
              "name": "flow"
            },
            {
              "name": "count",
              "type": "number"
            }
          ]
        }
      }
    },
    {
      "id": "0fceec26-18f2-4937-a87d-97b6c44800e1",
      "name": "Read switch",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        240,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": "switches"
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyName": "flow",
              "condition": "eq",
              "keyValue": "={{ $json.flow }}"
            }
          ]
        },
        "returnAll": true
      },
      "alwaysOutputData": true
    },
    {
      "id": "c47cc700-dddd-4e11-bd28-1eaf2b997731",
      "name": "Decide",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        480,
        300
      ],
      "parameters": {
        "jsCode": "// No row = off. Off = off. Above the daily cap = off.\nconst s = $input.first().json; const req = $('Called by another flow').first().json;\nconst today = $now.toFormat('yyyy-MM-dd');\nconst counter = s.counter_date === today ? (s.counter || 0) : 0;\nconst extra = Number(req.count) || 1;\nlet allowed = !!s.id && s.on === true;\nlet reason = !s.id ? 'no switch for this flow' : (!s.on ? (s.stop_reason || 'switched off') : '');\nif (allowed && counter + extra > s.per_day) { allowed = false; reason = `daily cap of ${s.per_day} reached`; }\nreturn [{ json: { allowed, reason, row: s.id || null, counter: allowed ? counter + extra : counter, today } }];"
      }
    },
    {
      "id": "0082cd17-d3fd-42e5-a6a6-c64b81ba7a61",
      "name": "Update counter",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        720,
        300
      ],
      "parameters": {
        "resource": "row",
        "operation": "update",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": "switches"
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyName": "id",
              "condition": "eq",
              "keyValue": "={{ $json.row }}"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "counter": "={{ $json.counter }}",
            "counter_date": "={{ $json.today }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "counter",
              "displayName": "counter",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true
            },
            {
              "id": "counter_date",
              "displayName": "counter_date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {}
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "c7a2d697-0779-4a47-bfd4-0d7158c89e54",
      "name": "Response",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        960,
        300
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "m",
              "name": "allowed",
              "value": "={{ $('Decide').item.json.allowed }}",
              "type": "boolean"
            },
            {
              "id": "r",
              "name": "reason",
              "value": "={{ $('Decide').item.json.reason }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "8d14ee00-fac8-4060-b96b-dbc178370019",
      "name": "Read me",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -160
      ],
      "parameters": {
        "content": "## Kill switch and daily cap\nTested on November 11, 2026, with n8n 2.47.3.\n\n**Fill in:** the *switches* table (flow, on, per_day, counter_date, counter, stop_reason), one row per flow.\n\nNo row, or on = false: the calling flow stops and gets the reason.",
        "height": 220,
        "width": 520,
        "color": 7
      }
    }
  ],
  "connections": {
    "Called by another flow": {
      "main": [
        [
          {
            "node": "Read switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read switch": {
      "main": [
        [
          {
            "node": "Decide",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Decide": {
      "main": [
        [
          {
            "node": "Update counter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update counter": {
      "main": [
        [
          {
            "node": "Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  }
}