The Things Network - New community gateways

This flow tracks newly registered gateways to the The Things Network and sends the data to a Telegram chat.

Usage

After copying the flow to a clean tab in your Node-RED instance, you will have to configure several bits:

  • Zone requests. Two example requests are inicluded: a zone-based (latitude, longitude and radius) and a country request. Change them depending on your needs.

  • Change the file where EUIs are persisted in the read and save nodes. This will depend on your OS.

  • The flow uses the Open Caged Data geocoding API based on OpenStreetMaps. You will need to create an account to get an access key. It is free up to 2500 requests per day. Use your key in the geocoding url node. You can also change the output language there using the 2-char ISO code.

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

  • Edit the message node and change the two chatId codes to target your chat. To get the chatId use another bot like @my_id_bot.

[
  {
    "id": "d266f615.d8b5b8",
    "type": "comment",
    "z": "d22cc156.2b13b",
    "name": "Community Gateway Monitoring",
    "info": "",
    "x": 190,
    "y": 80,
    "wires": []
  },
  {
    "id": "724494ce.9ffbfc",
    "type": "http request",
    "z": "d22cc156.2b13b",
    "name": "request",
    "method": "GET",
    "ret": "obj",
    "url": "https://www.thethingsnetwork.org/gateway-data/location?latitude=41.3870734&longitude=2.1700660&distance=50000",
    "tls": "",
    "x": 320,
    "y": 140,
    "wires": [
      [
        "eb6b7f68.4aa0d"
      ]
    ]
  },
  {
    "id": "405bbf8c.c188e",
    "type": "inject",
    "z": "d22cc156.2b13b",
    "name": "Espanya",
    "topic": "espanya",
    "payload": "",
    "payloadType": "date",
    "repeat": "900",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "x": 150,
    "y": 180,
    "wires": [
      [
        "61e9256b.0b3cdc"
      ]
    ]
  },
  {
    "id": "61e9256b.0b3cdc",
    "type": "http request",
    "z": "d22cc156.2b13b",
    "name": "request",
    "method": "GET",
    "ret": "obj",
    "url": "https://www.thethingsnetwork.org/gateway-data/country/es",
    "tls": "",
    "x": 320,
    "y": 180,
    "wires": [
      []
    ]
  },
  {
    "id": "eb6b7f68.4aa0d",
    "type": "function",
    "z": "d22cc156.2b13b",
    "name": "euis",
    "func": "msg.existing = msg.payload;\nmsg.scope = msg.topic.charAt(0).toUpperCase() + msg.topic.slice(1);\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "x": 470,
    "y": 160,
    "wires": [
      [
        "a5295578.82c058"
      ]
    ]
  },
  {
    "id": "a5295578.82c058",
    "type": "file in",
    "z": "d22cc156.2b13b",
    "name": "read",
    "filename": "/tmp/euis.json",
    "format": "utf8",
    "chunk": false,
    "sendError": false,
    "x": 470,
    "y": 220,
    "wires": [
      [
        "7c57e68c.1f7cc8"
      ]
    ]
  },
  {
    "id": "7c57e68c.1f7cc8",
    "type": "json",
    "z": "d22cc156.2b13b",
    "name": "",
    "property": "payload",
    "action": "",
    "pretty": false,
    "x": 610,
    "y": 220,
    "wires": [
      [
        "58d85fe1.bc63",
        "81c802dc.77af7"
      ]
    ]
  },
  {
    "id": "81c802dc.77af7",
    "type": "function",
    "z": "d22cc156.2b13b",
    "name": "diff",
    "func": "var diff = Array();\n\n//delete msg.payload[msg.payload.length - 1];\n\nfor (var eui in msg.existing) {\n    if (msg.payload.indexOf(eui) < 0) {\n        diff.push({\n            scope: msg.scope,\n            data: msg.existing[eui]\n        });\n    }\n}\nif (diff.length === 0) return null;\nmsg.payload = diff;\nreturn msg;\n",
    "outputs": 1,
    "noerr": 0,
    "x": 750,
    "y": 220,
    "wires": [
      [
        "9da57ad5.029388"
      ]
    ]
  },
  {
    "id": "9da57ad5.029388",
    "type": "yield",
    "z": "d22cc156.2b13b",
    "x": 890,
    "y": 220,
    "wires": [
      [
        "403738e8.3d0818"
      ]
    ]
  },
  {
    "id": "323a9562.3d3dca",
    "type": "http request",
    "z": "d22cc156.2b13b",
    "name": "request",
    "method": "GET",
    "ret": "obj",
    "url": "",
    "tls": "",
    "x": 660,
    "y": 280,
    "wires": [
      [
        "3cf8b58c.93cb6a"
      ]
    ]
  },
  {
    "id": "403738e8.3d0818",
    "type": "template",
    "z": "d22cc156.2b13b",
    "name": "geocode url",
    "field": "url",
    "fieldType": "msg",
    "format": "handlebars",
    "syntax": "mustache",
    "template": "https://api.opencagedata.com/geocode/v1/json?key=<access-key>&q={{ data.location.latitude }},{{ data.location.longitude }}&pretty=1&no_annotations=1&language=en",
    "output": "str",
    "x": 490,
    "y": 280,
    "wires": [
      [
        "323a9562.3d3dca"
      ]
    ]
  },
  {
    "id": "b0e5adb1.b1f42",
    "type": "telegram sender",
    "z": "d22cc156.2b13b",
    "name": "Telegram Bot",
    "bot": "85636841.1c8348",
    "x": 810,
    "y": 340,
    "wires": [
      []
    ]
  },
  {
    "id": "3cf8b58c.93cb6a",
    "type": "template",
    "z": "d22cc156.2b13b",
    "name": "message",
    "field": "payload",
    "fieldType": "msg",
    "format": "handlebars",
    "syntax": "mustache",
    "template": "[\n{ \n    \"payload\": {\n        \"chatId\": -220000000,\n        \"type\": \"message\",\n        \"content\": \"New gateway in {{ scope }}!!\\nOwner: {{ data.owner }}\\nAddress: {{ payload.results.0.formatted }}\\nDescription: {{ data.description }}\\nEUI: {{ data.id }}\"\n    }\n},\n{\n    \"payload\": {\n        \"chatId\": -220000000,\n        \"type\": \"location\",\n        \"content\": {\n            \"latitude\" : {{ data.location.latitude }},\n            \"longitude\" : {{ data.location.longitude }}\n        },\n        \"options\": {\n            \"live_period\": \"{{ data.last_seen }}\"\n        }\n    }\n}\n]\n",
    "output": "json",
    "x": 480,
    "y": 340,
    "wires": [
      [
        "b511d59f.b7b5c8"
      ]
    ]
  },
  {
    "id": "b511d59f.b7b5c8",
    "type": "yield",
    "z": "d22cc156.2b13b",
    "x": 650,
    "y": 340,
    "wires": [
      [
        "b0e5adb1.b1f42"
      ]
    ]
  },
  {
    "id": "12bee28.8ae621e",
    "type": "file",
    "z": "d22cc156.2b13b",
    "name": "save",
    "filename": "/tmp/euis.json",
    "appendNewline": true,
    "createDir": false,
    "overwriteFile": "true",
    "x": 890,
    "y": 180,
    "wires": []
  },
  {
    "id": "58d85fe1.bc63",
    "type": "function",
    "z": "d22cc156.2b13b",
    "name": "pre",
    "func": "msg.payload = Object.keys(msg.existing);\nreturn msg;\n",
    "outputs": 1,
    "noerr": 0,
    "x": 750,
    "y": 180,
    "wires": [
      [
        "12bee28.8ae621e"
      ]
    ]
  },
  {
    "id": "d99e1056.9222",
    "type": "inject",
    "z": "d22cc156.2b13b",
    "name": "Barcelona",
    "topic": "barcelona",
    "payload": "",
    "payloadType": "date",
    "repeat": "900",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "x": 150,
    "y": 140,
    "wires": [
      [
        "724494ce.9ffbfc"
      ]
    ]
  },
  {
    "id": "85636841.1c8348",
    "type": "telegram bot",
    "z": "",
    "botname": "Telegram Bot",
    "usernames": "",
    "chatids": "",
    "baseapiurl": "",
    "pollinterval": "300"
  }
]
xoseperez

Flow Info

created 2 weeks, 1 day ago

Node Types

Core
  • comment (x1)
  • file (x1)
  • file in (x1)
  • function (x3)
  • http request (x3)
  • inject (x2)
  • json (x1)
  • template (x2)
Other

Tags

  • thethingsnetwork
  • ttn
  • telegram
  • geocode
  • gateway
  • monitoring
  • activations
  • bot
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option