Search code examples
node-red

Node-Red: Re-run different nodes at different times


I have the following Flow created to login with an API and then fetch data from a second endpoint.

[
    {
        "id": "4d1f98b0.642758",
        "type": "http request",
        "z": "c155aa476d7d3fe2",
        "name": "/login",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "https://eu5.fusionsolar.huawei.com/thirdData/login",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 490,
        "y": 180,
        "wires": [
            [
                "9e0cdaf0c53abc05"
            ]
        ]
    },
    {
        "id": "62206b38.707924",
        "type": "function",
        "z": "c155aa476d7d3fe2",
        "name": "",
        "func": "msg.headers = {}\nmsg.url = \"https://eu5.fusionsolar.huawei.com/thirdData/login\";\nmsg.headers[\"content-type\"] = \"application/json\"\n\n\nmsg.payload = {\n    \"userName\": \"API\",\n    \"systemCode\": \"1337\"\n}\n\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 340,
        "y": 180,
        "wires": [
            [
                "4d1f98b0.642758"
            ]
        ]
    },
    {
        "id": "fda2c97918f48fc3",
        "type": "inject",
        "z": "c155aa476d7d3fe2",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 180,
        "y": 180,
        "wires": [
            [
                "62206b38.707924"
            ]
        ]
    },
    {
        "id": "0ce4ec69bf9d8b8a",
        "type": "debug",
        "z": "c155aa476d7d3fe2",
        "name": "debug msg",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1570,
        "y": 180,
        "wires": []
    },
    {
        "id": "9e0cdaf0c53abc05",
        "type": "function",
        "z": "c155aa476d7d3fe2",
        "name": "flow save token",
        "func": "flow.set('token', msg.headers[\"xsrf-token\"]);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 660,
        "y": 180,
        "wires": [
            [
                "8d4abce006eb56da"
            ]
        ]
    },
    {
        "id": "7a3e4af0ea3ffca9",
        "type": "function",
        "z": "c155aa476d7d3fe2",
        "name": "",
        "func": "msg.headers = {}\nmsg.url = \"https://eu5.fusionsolar.huawei.com/thirdData/getDevRealKpi\";\nmsg.headers[\"content-type\"] = \"application/json\"\nmsg.headers[\"XSRF-TOKEN\"] = flow.get(\"token\");\n\nmsg.payload = {\n    \"devIds\": \"123\",\n    \"devTypeId\": \"1\"\n}\n\nnode.log(msg.payload);\n\nreturn msg",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1020,
        "y": 180,
        "wires": [
            [
                "45bbc3580423f628"
            ]
        ]
    },
    {
        "id": "45bbc3580423f628",
        "type": "http request",
        "z": "c155aa476d7d3fe2",
        "name": "/getDevRealKpi",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "https://eu5.fusionsolar.huawei.com/thirdData/getDevRealKpi",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 1200,
        "y": 180,
        "wires": [
            [
                "0fdbf21d9218945b"
            ]
        ]
    },
    {
        "id": "f2fa10e69d612fbb",
        "type": "inject",
        "z": "c155aa476d7d3fe2",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "token",
        "payloadType": "flow",
        "x": 840,
        "y": 220,
        "wires": [
            [
                "7a3e4af0ea3ffca9"
            ]
        ]
    },
    {
        "id": "8d4abce006eb56da",
        "type": "delay",
        "z": "c155aa476d7d3fe2",
        "name": "",
        "pauseType": "delay",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 840,
        "y": 180,
        "wires": [
            [
                "7a3e4af0ea3ffca9"
            ]
        ]
    },
    {
        "id": "0fdbf21d9218945b",
        "type": "json",
        "z": "c155aa476d7d3fe2",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 1370,
        "y": 180,
        "wires": [
            [
                "0ce4ec69bf9d8b8a",
                "c91763435a1a9f49"
            ]
        ]
    },
    {
        "id": "c91763435a1a9f49",
        "type": "loxone-control-out",
        "z": "c155aa476d7d3fe2",
        "name": "Miniserver",
        "miniserver": "294f4e359f889cd9",
        "control": "",
        "x": 1530,
        "y": 300,
        "wires": []
    },
    {
        "id": "294f4e359f889cd9",
        "type": "loxone-miniserver",
        "host": "192.168.1.1",
        "port": "80",
        "enctype": "0",
        "active": true,
        "keepalive": "30000"
    }
]

flow

What I dont understand is how I can schedule the /login http request every 4 hours but the getDevRealKpi should get called every 20 seconds as I need the response as UDP message.


Solution

  • Break the flow into 2.

    Have the login part store the cookie/token in the context.

    The second half can then grab the latest version of the authentication token from the context as needed.