Skip to content

Instantly share code, notes, and snippets.

@Lineflyer
Last active August 6, 2023 21:15
Show Gist options
  • Save Lineflyer/5334c57d906dca808525aa403d2e7ab6 to your computer and use it in GitHub Desktop.
Save Lineflyer/5334c57d906dca808525aa403d2e7ab6 to your computer and use it in GitHub Desktop.
Node Red Flows for Tronity API

This is an example flow, which can be imported into Node Red. It utilizes an example of

  • Authorization towards the Tronity API
  • Request latest vehicle data
  • Request list of all charging session

Based on the documentation on https://app.tronity.tech/ and some unnamed examples of authentication flows found on the internet. It has been tested and is working (as of 06.08.2023).

Pleas note: You have to modify some of the nodes (see node naming and README) and replace placeholders by your individual IDs.

Feedback as comment on this GIST is welcome!

Copy the following lines and use NodeRed - Import, paste the lines and select to import into a new flow:

[
    {
        "id": "20421f6c56f8afc1",
        "type": "tab",
        "label": "Export_Tronity",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "f4e46c109104b84f",
        "type": "http request",
        "z": "20421f6c56f8afc1",
        "name": "Auth-Request to Tronity API",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "query",
        "url": "https://api.tronity.tech/authentication",
        "tls": "91a33722c228e6f1",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 1300,
        "y": 340,
        "wires": [
            [
                "06a1dbc98752dbf1"
            ]
        ]
    },
    {
        "id": "00920f7a07b8ee87",
        "type": "inject",
        "z": "20421f6c56f8afc1",
        "name": "Every 15 minutes",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 480,
        "y": 340,
        "wires": [
            [
                "af0b03efcb962b2f"
            ]
        ]
    },
    {
        "id": "af0b03efcb962b2f",
        "type": "change",
        "z": "20421f6c56f8afc1",
        "name": "Query parameter (!!! Add client_id & client secret here before use !!!)",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"client_id\":\"YOURID\",\"client_secret\":\"YOURSECRET\",\"grant_type\":\"app\"}",
                "tot": "json"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 900,
        "y": 340,
        "wires": [
            [
                "f4e46c109104b84f"
            ]
        ]
    },
    {
        "id": "c352a33202713ea6",
        "type": "comment",
        "z": "20421f6c56f8afc1",
        "name": "Token Refresh (Every 15min)",
        "info": "As the access token is valid for 15 minutes, we need to refresh it from the personal token each 15 minutes.\nDa der Access-Token nur 15 Minuten gültig ist, muss dieser alle 15 Minuten mit dem Personal-Token aktualisiert werden.",
        "x": 500,
        "y": 300,
        "wires": []
    },
    {
        "id": "06a1dbc98752dbf1",
        "type": "change",
        "z": "20421f6c56f8afc1",
        "name": "Store access token as flow variable",
        "rules": [
            {
                "t": "set",
                "p": "tronity_accesstoken",
                "pt": "flow",
                "to": "payload.access_token",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1600,
        "y": 340,
        "wires": [
            []
        ]
    },
    {
        "id": "4b8839dc0386b934",
        "type": "http request",
        "z": "20421f6c56f8afc1",
        "name": "Get charging infos from Tronity API  (!!! Add your vehicle ID to the URL field before use !!!)",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "query",
        "url": "https://api.tronity.tech/tronity/vehicles/VEHICLEID/charges",
        "tls": "91a33722c228e6f1",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 1410,
        "y": 620,
        "wires": [
            [
                "3236205e192ce3e0"
            ]
        ]
    },
    {
        "id": "92e8292ce380dd10",
        "type": "function",
        "z": "20421f6c56f8afc1",
        "name": "Create Header with JSON-Bearer-Token",
        "func": "let token = flow.get('tronity_accesstoken');\n\nmsg.headers = {\n    'Authorization': 'Bearer ' + token\n};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 560,
        "y": 620,
        "wires": [
            [
                "141fc3ad78c26d74"
            ]
        ]
    },
    {
        "id": "9656e3b2bcc1981b",
        "type": "comment",
        "z": "20421f6c56f8afc1",
        "name": "Retrieve array with list of charging sessions",
        "info": "",
        "x": 540,
        "y": 520,
        "wires": []
    },
    {
        "id": "75e3ce2197ad465c",
        "type": "http request",
        "z": "20421f6c56f8afc1",
        "name": "Request vehicle record from Tronity API (!!! Add your vehicle ID to the URL field before use !!!)",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://api.tronity.tech/tronity/vehicles/VEHICLEID/last_record",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 1280,
        "y": 460,
        "wires": [
            [
                "bfdb5611042f129b"
            ]
        ]
    },
    {
        "id": "28c82de28bc202ec",
        "type": "function",
        "z": "20421f6c56f8afc1",
        "name": "Create Header with JSON-Bearer-Token",
        "func": "let token = flow.get('tronity_accesstoken');\n\nmsg.headers = {\n    'Authorization': 'Bearer ' + token\n};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 760,
        "y": 460,
        "wires": [
            [
                "75e3ce2197ad465c"
            ]
        ]
    },
    {
        "id": "08cd7343687a5c7a",
        "type": "inject",
        "z": "20421f6c56f8afc1",
        "name": "Manual trigger",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 470,
        "y": 460,
        "wires": [
            [
                "28c82de28bc202ec"
            ]
        ]
    },
    {
        "id": "50ef9bfb24c3842a",
        "type": "comment",
        "z": "20421f6c56f8afc1",
        "name": "Get latest vehicle data",
        "info": "",
        "x": 480,
        "y": 420,
        "wires": []
    },
    {
        "id": "141fc3ad78c26d74",
        "type": "function",
        "z": "20421f6c56f8afc1",
        "name": "Define parameter of requested charges",
        "func": "// parameter definition\nlet skip = \"0\" // Don't skip any charges\nlet limit = \"200\" // Define max amount of charges returned\n\n// create payload for request\nlet request = {\"limit\":limit,\"skip\":skip}\nmsg.payload = request\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 900,
        "y": 620,
        "wires": [
            [
                "4b8839dc0386b934"
            ]
        ]
    },
    {
        "id": "3236205e192ce3e0",
        "type": "debug",
        "z": "20421f6c56f8afc1",
        "name": "debug",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1790,
        "y": 620,
        "wires": []
    },
    {
        "id": "0523f57e7c04671d",
        "type": "inject",
        "z": "20421f6c56f8afc1",
        "name": "Manual trigger",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 470,
        "y": 560,
        "wires": [
            [
                "92e8292ce380dd10"
            ]
        ]
    },
    {
        "id": "440753a74d472a62",
        "type": "comment",
        "z": "20421f6c56f8afc1",
        "name": "README",
        "info": "This flow needs personalisation before it is ready to use.\nGet your API credentials on https://www.tronity.tech/\n\nYou need the following information:\n- Your Client ID \n- Your Client Secret\n- Your Vehicle ID(s)\n",
        "x": 440,
        "y": 240,
        "wires": []
    },
    {
        "id": "bfdb5611042f129b",
        "type": "debug",
        "z": "20421f6c56f8afc1",
        "name": "debug",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1690,
        "y": 460,
        "wires": []
    },
    {
        "id": "91a33722c228e6f1",
        "type": "tls-config",
        "name": "",
        "cert": "",
        "key": "",
        "ca": "",
        "certname": "",
        "keyname": "",
        "caname": "",
        "servername": "",
        "verifyservercert": true,
        "alpnprotocol": ""
    }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment