node-red elapsedtime

Written by:

To measure time spent, you can use a function node or use the “hourglass” node.

In the example below, I have made a simple variant using the “flow set” to initiate the start time.

Add reset with a change node

See below for “function node” code:

var CurrentTime = new Date().getTime();
var StartTime = flow.get("InitTime"); 
var msdiff = CurrentTime - StartTime;

 const days = Math.floor(msdiff / (24*60*60*1000));
  const daysms = msdiff % (24*60*60*1000);
  const hours = Math.floor(daysms / (60*60*1000));
  const hoursms = msdiff % (60*60*1000);
  const minutes = Math.floor(hoursms / (60*1000));
  const minutesms = msdiff % (60*1000);
  const sec = Math.floor(minutesms / 1000);
//  msg.payload = sec; 
  msg.payload = { "difftime": days + " days, " + hours + " hours, "  + minutes + " minutes, " + sec + " seconds "} 


return msg

Complete flow for both examples: (source flow hourglass here: https://forums.opto22.com/t/uptime-runtime-with-hourglass-node/3106)

[
    {
        "id": "a8f11cec7345b08f",
        "type": "group",
        "z": "d176ededfb63f91d",
        "name": "node Hourgrlass",
        "style": {
            "fill": "#ffff7f",
            "label": true
        },
        "nodes": [
            "f4c0fc7b.0d8ba",
            "9dbff6e2.f4fa78",
            "913ec325.43e08",
            "3bc76c68.e85f64",
            "c857c4c5.942628",
            "3fa35199.275e3e",
            "b4de9086.a01dc",
            "cc439402.40cd98",
            "9d254350838646f0",
            "3ce36182d5fb7b1c",
            "c776afd72084f4ae",
            "18e84ff7d34c9010",
            "147d9e8880ed381f"
        ],
        "x": 34,
        "y": 299,
        "w": 992,
        "h": 342
    },
    {
        "id": "f4c0fc7b.0d8ba",
        "type": "hourglass",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "",
        "persistId": "",
        "humanizeLocale": "nb",
        "x": 640,
        "y": 460,
        "wires": [
            [
                "cc439402.40cd98",
                "3ce36182d5fb7b1c"
            ]
        ]
    },
    {
        "id": "9dbff6e2.f4fa78",
        "type": "inject",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "Auto Start",
        "props": [
            {
                "p": "payload",
                "v": "",
                "vt": "date"
            },
            {
                "p": "topic",
                "v": "",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 150,
        "y": 412,
        "wires": [
            [
                "913ec325.43e08"
            ]
        ]
    },
    {
        "id": "913ec325.43e08",
        "type": "change",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "Start",
        "rules": [
            {
                "t": "set",
                "p": "command",
                "pt": "msg",
                "to": "start",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 410,
        "y": 412,
        "wires": [
            [
                "f4c0fc7b.0d8ba"
            ]
        ]
    },
    {
        "id": "3bc76c68.e85f64",
        "type": "change",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "Status",
        "rules": [
            {
                "t": "set",
                "p": "command",
                "pt": "msg",
                "to": "status",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 410,
        "y": 460,
        "wires": [
            [
                "f4c0fc7b.0d8ba"
            ]
        ]
    },
    {
        "id": "c857c4c5.942628",
        "type": "change",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "Stop",
        "rules": [
            {
                "t": "set",
                "p": "command",
                "pt": "msg",
                "to": "stop",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 410,
        "y": 508,
        "wires": [
            [
                "f4c0fc7b.0d8ba"
            ]
        ]
    },
    {
        "id": "3fa35199.275e3e",
        "type": "inject",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "Read every 15 seconds",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1800",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 190,
        "y": 460,
        "wires": [
            [
                "3bc76c68.e85f64"
            ]
        ]
    },
    {
        "id": "b4de9086.a01dc",
        "type": "inject",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "Manual Stop",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 150,
        "y": 509,
        "wires": [
            [
                "c857c4c5.942628"
            ]
        ]
    },
    {
        "id": "cc439402.40cd98",
        "type": "debug",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 870,
        "y": 540,
        "wires": []
    },
    {
        "id": "9d254350838646f0",
        "type": "ui-text",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "group": "9e43330ce5441af6",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "TimeElapsedHourglass",
        "format": "{{msg.payload}}",
        "layout": "col-center",
        "style": true,
        "font": "Arial Black,Arial Black,Gadget,sans-serif",
        "fontSize": "20",
        "color": "#1a106a",
        "className": "",
        "x": 890,
        "y": 360,
        "wires": []
    },
    {
        "id": "3ce36182d5fb7b1c",
        "type": "change",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "elapsed.time.days & \" days, \" & elapsed.time.hours & \" hours, \" & elapsed.time.minutes & \" minutes, \" & elapsed.time.seconds & \" seconds\" ",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 860,
        "y": 440,
        "wires": [
            [
                "9d254350838646f0",
                "cc439402.40cd98"
            ]
        ]
    },
    {
        "id": "c776afd72084f4ae",
        "type": "inject",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "reset",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 150,
        "y": 600,
        "wires": [
            [
                "18e84ff7d34c9010"
            ]
        ]
    },
    {
        "id": "18e84ff7d34c9010",
        "type": "change",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "Stop",
        "rules": [
            {
                "t": "set",
                "p": "command",
                "pt": "msg",
                "to": "reset",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 410,
        "y": 600,
        "wires": [
            [
                "f4c0fc7b.0d8ba"
            ]
        ]
    },
    {
        "id": "147d9e8880ed381f",
        "type": "comment",
        "z": "d176ededfb63f91d",
        "g": "a8f11cec7345b08f",
        "name": "see link",
        "info": "[https://forums.opto22.com/t/uptime-runtime-with-hourglass-node/3106]()",
        "x": 110,
        "y": 340,
        "wires": []
    },
    {
        "id": "9e43330ce5441af6",
        "type": "ui-group",
        "name": "Time",
        "page": "21786a9595cac0d8",
        "width": "6",
        "height": "1",
        "order": 2,
        "showTitle": true,
        "className": "",
        "visible": "true",
        "disabled": "false"
    },
    {
        "id": "21786a9595cac0d8",
        "type": "ui-page",
        "name": "tiots",
        "ui": "cb79bc4520925e32",
        "path": "/tiots",
        "icon": "home",
        "layout": "grid",
        "theme": "129e99574def90a3",
        "order": 14,
        "className": "",
        "visible": "true",
        "disabled": "false"
    },
    {
        "id": "cb79bc4520925e32",
        "type": "ui-base",
        "name": "My UI",
        "path": "/dashboard",
        "includeClientData": true,
        "acceptsClientConfig": [
            "ui-notification",
            "ui-control"
        ],
        "showPathInSidebar": false
    },
    {
        "id": "129e99574def90a3",
        "type": "ui-theme",
        "name": "Another Theme",
        "colors": {
            "surface": "#ffffff",
            "primary": "#0094ce",
            "bgPage": "#eeeeee",
            "groupBg": "#ffffff",
            "groupOutline": "#cccccc"
        }
    },
    {
        "id": "b24e168829f8faf8",
        "type": "group",
        "z": "d176ededfb63f91d",
        "name": "TimeElapsed",
        "style": {
            "fill": "#e3f3d3",
            "label": true
        },
        "nodes": [
            "8c8b4fb5.726f8",
            "663b45619dfaa66c",
            "7beb801e99221482",
            "7e86dcd9b6b2fcf8",
            "85d7a692d0e182e0"
        ],
        "x": 34,
        "y": 59,
        "w": 992,
        "h": 182
    },
    {
        "id": "8c8b4fb5.726f8",
        "type": "inject",
        "z": "d176ededfb63f91d",
        "g": "b24e168829f8faf8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "test",
        "payload": "",
        "payloadType": "date",
        "x": 160,
        "y": 140,
        "wires": [
            [
                "7e86dcd9b6b2fcf8"
            ]
        ]
    },
    {
        "id": "663b45619dfaa66c",
        "type": "debug",
        "z": "d176ededfb63f91d",
        "g": "b24e168829f8faf8",
        "name": "debug 444",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 670,
        "y": 200,
        "wires": []
    },
    {
        "id": "7beb801e99221482",
        "type": "ui-text",
        "z": "d176ededfb63f91d",
        "g": "b24e168829f8faf8",
        "group": "9e43330ce5441af6",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "TimeElapsed: ",
        "format": "{{msg.payload}}",
        "layout": "col-center",
        "style": true,
        "font": "Arial Black,Arial Black,Gadget,sans-serif",
        "fontSize": "20",
        "color": "#0a0a0a",
        "className": "",
        "x": 920,
        "y": 140,
        "wires": []
    },
    {
        "id": "7e86dcd9b6b2fcf8",
        "type": "function",
        "z": "d176ededfb63f91d",
        "g": "b24e168829f8faf8",
        "name": "Time elapsed (same message)",
        "func": "var CurrentTime = new Date().getTime();\nvar StartTime = flow.get(\"InitTime\"); \nvar msdiff = CurrentTime - StartTime;\n\n const days = Math.floor(msdiff / (24*60*60*1000));\n  const daysms = msdiff % (24*60*60*1000);\n  const hours = Math.floor(daysms / (60*60*1000));\n  const hoursms = msdiff % (60*60*1000);\n  const minutes = Math.floor(hoursms / (60*1000));\n  const minutesms = msdiff % (60*1000);\n  const sec = Math.floor(minutesms / 1000);\n//  msg.payload = sec; \n  msg.payload = { \"difftime\": days + \" days, \" + hours + \" hours, \"  + minutes + \" minutes, \" + sec + \" seconds \"} \n\n\nreturn msg",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "// Code added here will be run once\n// whenever the node is started.\nvar StartTime = new Date().getTime();\nflow.set('InitTime', StartTime);\n",
        "finalize": "",
        "libs": [],
        "x": 430,
        "y": 140,
        "wires": [
            [
                "663b45619dfaa66c",
                "85d7a692d0e182e0"
            ]
        ],
        "outputLabels": [
            "message"
        ],
        "icon": "node-red/timer.png"
    },
    {
        "id": "85d7a692d0e182e0",
        "type": "change",
        "z": "d176ededfb63f91d",
        "g": "b24e168829f8faf8",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.difftime",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 680,
        "y": 100,
        "wires": [
            [
                "7beb801e99221482"
            ]
        ]
    }
]

Discover more from Node-RED LoRaWAN CouchDB and more

Subscribe to get the latest posts sent to your email.

Leave a comment