TheThingsNetwork gateway monitor with Telegram alarms

Description

This a flow to monitor the status of LoRaWan gateways connected to The Things Network (TTN) using the undocumented NOC API. Periodically inject a message with the gateway EUI as payload and connect the output to a Telegram bot.

Configuration

  • You will need to edit/add inject nodes with the name of the gateway in the topic and the EUI in the payload. Configure it to check the status every X minutes.

  • Create a telegram bot (using the @BotFather) and configure the token in the telegram configuration node.

  • You will also need to add the chatId to send the notification to in the "telegram payload" function node. To get the chatId use another bot like @my_id_bot.

[
  {
    "id": "96beb4bd.b0d418",
    "type": "comment",
    "z": "f2a3fe14.e7db8",
    "name": "Gateway Management",
    "info": "",
    "x": 160,
    "y": 240,
    "wires": []
  },
  {
    "id": "efe2cc40.4ab8b",
    "type": "inject",
    "z": "f2a3fe14.e7db8",
    "name": "ttn-gw01",
    "topic": "ttn-gw01",
    "payload": "eui-0000000000000001",
    "payloadType": "str",
    "repeat": "300",
    "crontab": "",
    "once": true,
    "onceDelay": 0.1,
    "x": 140,
    "y": 320,
    "wires": [
      [
        "fc377781.1447e8"
      ]
    ]
  },
  {
    "id": "a99602aa.eb307",
    "type": "http request",
    "z": "f2a3fe14.e7db8",
    "name": "request",
    "method": "GET",
    "ret": "obj",
    "url": "",
    "tls": "",
    "x": 520,
    "y": 380,
    "wires": [
      [
        "bb2b60e2.5f72b"
      ]
    ]
  },
  {
    "id": "fc377781.1447e8",
    "type": "template",
    "z": "f2a3fe14.e7db8",
    "name": "build url",
    "field": "url",
    "fieldType": "msg",
    "format": "handlebars",
    "syntax": "mustache",
    "template": "http://noc.thethingsnetwork.org:8085/api/v2/gateways/{{payload}}",
    "output": "str",
    "x": 360,
    "y": 380,
    "wires": [
      [
        "a99602aa.eb307"
      ]
    ]
  },
  {
    "id": "bb2b60e2.5f72b",
    "type": "function",
    "z": "f2a3fe14.e7db8",
    "name": "status",
    "func": "var status = 'offline';\n\nif (200 == msg.statusCode) {\n    if (!(\"error\" in msg.payload)) {\n        var last = (new Date(msg.payload.timestamp)).getTime() / 1000;\n        var now = new Date();\n        var current = (now.getTime() + (now.getTimezoneOffset() * 60)) / 1000;        \n        var difference = current - last;\n        msg.last = last;\n        msg.current = current;\n        msg.delta = difference;\n        if (difference < 180) status = 'online';\n    }\n}\n\nmsg.status = status;\nmsg.payload = msg.topic + \"-\" + status;\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "x": 670,
    "y": 380,
    "wires": [
      [
        "401526f4.b52348",
        "7a0405b1.bd2e3c"
      ]
    ]
  },
  {
    "id": "7a0405b1.bd2e3c",
    "type": "rbe",
    "z": "f2a3fe14.e7db8",
    "name": "has_changed",
    "func": "rbei",
    "gap": "",
    "start": "",
    "inout": "out",
    "property": "payload",
    "x": 380,
    "y": 460,
    "wires": [
      [
        "8406037c.072cd"
      ]
    ]
  },
  {
    "id": "401526f4.b52348",
    "type": "debug",
    "z": "f2a3fe14.e7db8",
    "name": "status",
    "active": false,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "true",
    "x": 810,
    "y": 380,
    "wires": []
  },
  {
    "id": "8406037c.072cd",
    "type": "template",
    "z": "f2a3fe14.e7db8",
    "name": "message template",
    "field": "payload",
    "fieldType": "msg",
    "format": "handlebars",
    "syntax": "mustache",
    "template": "Gateway {{ topic }} status has changed to {{ status }}.\n",
    "output": "str",
    "x": 590,
    "y": 460,
    "wires": [
      [
        "25622438.bdc0ec",
        "4711b876.27f488"
      ]
    ]
  },
  {
    "id": "25622438.bdc0ec",
    "type": "debug",
    "z": "f2a3fe14.e7db8",
    "name": "status",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "true",
    "x": 770,
    "y": 520,
    "wires": []
  },
  {
    "id": "6fe755d7.2e338c",
    "type": "inject",
    "z": "f2a3fe14.e7db8",
    "name": "ttn-gw02",
    "topic": "ttn-gw02",
    "payload": "eui-0000000000000002",
    "payloadType": "str",
    "repeat": "300",
    "crontab": "",
    "once": true,
    "onceDelay": 0.1,
    "x": 140,
    "y": 380,
    "wires": [
      [
        "fc377781.1447e8"
      ]
    ]
  },
  {
    "id": "e906e0bd.383b1",
    "type": "inject",
    "z": "f2a3fe14.e7db8",
    "name": "ttn-gw03",
    "topic": "ttn-gw03",
    "payload": "eui-0000000000000003",
    "payloadType": "str",
    "repeat": "300",
    "crontab": "",
    "once": true,
    "onceDelay": 0.1,
    "x": 140,
    "y": 440,
    "wires": [
      [
        "fc377781.1447e8"
      ]
    ]
  },
  {
    "id": "78b71800.62f598",
    "type": "telegram sender",
    "z": "f2a3fe14.e7db8",
    "name": "telegram",
    "bot": "c559e4a3.56fef8",
    "x": 1000,
    "y": 460,
    "wires": [
      []
    ]
  },
  {
    "id": "4711b876.27f488",
    "type": "template",
    "z": "f2a3fe14.e7db8",
    "name": "telegram payload",
    "field": "payload",
    "fieldType": "msg",
    "format": "handlebars",
    "syntax": "mustache",
    "template": "{\n    \"chatId\": -220000000,\n    \"type\": \"message\",\n    \"content\": \"{{payload}}\"\n}\n\n",
    "output": "json",
    "x": 810,
    "y": 460,
    "wires": [
      [
        "78b71800.62f598"
      ]
    ]
  },
  {
    "id": "c559e4a3.56fef8",
    "type": "telegram bot",
    "z": "",
    "botname": "Telegram bot",
    "usernames": "",
    "chatids": "",
    "baseapiurl": "",
    "pollinterval": "300"
  }
]
xoseperez

Flow Info

created 2 weeks, 2 days ago
updated 2 weeks, 1 day ago

Node Types

Core
  • comment (x1)
  • debug (x2)
  • function (x1)
  • http request (x1)
  • inject (x3)
  • template (x3)
Other

Tags

  • thethingsnetwork
  • ttn
  • gateway
  • monitor
  • lorawan
  • telegram
  • alarms
  • notifications
  • offline
  • online
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option