Accumulated time decreasing instead of increasing

Hello!
I need some help, I have an issue where the minutes output is decreasing instead of increasing, my task is to display inside the dashboard the total runtime of the machine from 7 AM to 7PM but the total time for example will start at 300 minutes decreasing to 299 min and so on.

[
    {
        "id": "4cfb9de3b7351f79",
        "type": "inject",
        "z": "d73d4ac01d932345",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "14",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 190,
        "y": 280,
        "wires": [
            [
                "c02ebf06318dabe1"
            ]
        ]
    },
    {
        "id": "6f1346d6f1bff64e",
        "type": "debug",
        "z": "d73d4ac01d932345",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 970,
        "y": 280,
        "wires": []
    },
    {
        "id": "d735b08bd510c311",
        "type": "MSSQL",
        "z": "d73d4ac01d932345",
        "mssqlCN": "aba6c4089a8cbd85",
        "name": "Runtime",
        "query": "",
        "outField": "payload",
        "x": 560,
        "y": 280,
        "wires": [
            [
                "96943655b69a0df8",
                "6f945aead4d2d96e"
            ]
        ]
    },
    {
        "id": "c02ebf06318dabe1",
        "type": "function",
        "z": "d73d4ac01d932345",
        "name": "function 1",
        "func": "msg.payload = `\n    SELECT DateTime, Value\n    FROM dbo.SA03\n    WHERE Tag = 'CH.DV01.MAIN01.MachineRunning'\n    AND DateTime >= DATEADD(hour, -12, GETDATE())\n    ORDER BY DateTime ASC\n`;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 280,
        "wires": [
            [
                "d735b08bd510c311"
            ]
        ]
    },
    {
        "id": "0ab9242755992bc5",
        "type": "ui-text",
        "z": "d73d4ac01d932345",
        "group": "2d8c54242c426ed5",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "Uptime",
        "label": "Running",
        "format": "{{msg.payload}}",
        "layout": "col-center",
        "style": true,
        "font": "Arial Black,Arial Black,Gadget,sans-serif",
        "fontSize": 16,
        "color": "#ffffff",
        "wrapText": false,
        "className": "",
        "x": 1080,
        "y": 400,
        "wires": []
    },
    {
        "id": "96943655b69a0df8",
        "type": "debug",
        "z": "d73d4ac01d932345",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 760,
        "y": 140,
        "wires": []
    },
    {
        "id": "6f945aead4d2d96e",
        "type": "function",
        "z": "d73d4ac01d932345",
        "name": "function 5",
        "func": "let totalRuntimeMs = 0;\nlet runningStartTime = null;\n\nfor (let i = 0; i < msg.payload.length; i++) {\n    const currentRecord = msg.payload[i];\n    const currentTime = new Date(currentRecord.DateTime);\n    const currentHours = currentTime.getHours();\n\n    if (currentHours >= 7 && currentHours <= 19) {\n        if (currentRecord.Value === true) {\n            if (runningStartTime === null) {\n                runningStartTime = currentTime.getTime();\n            }\n        } else { // currentRecord.Value === false\n            if (runningStartTime !== null) {\n                totalRuntimeMs += (currentTime.getTime() - runningStartTime);\n                runningStartTime = null;\n            }\n        }\n    } else {\n        // If outside the time window, reset the running start time\n        runningStartTime = null;\n    }\n}\n\nconst totalRuntimeMinutes = totalRuntimeMs / (1000 * 60);\nmsg.payload = `${Math.max(0, totalRuntimeMinutes).toFixed(2)} minutes`;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 760,
        "y": 280,
        "wires": [
            [
                "0ab9242755992bc5",
                "6f1346d6f1bff64e"
            ]
        ]
    },
    {
        "id": "aba6c4089a8cbd85",
        "type": "MSSQL-CN",
        "name": "i4.0",
        "server": "Delta",
        "encyption": true,
        "database": "i4.0"
    },
    {
        "id": "2d8c54242c426ed5",
        "type": "ui-group",
        "name": "Runtime",
        "page": "36d8747804871232",
        "width": "2",
        "height": 1,
        "order": 3,
        "showTitle": true,
        "className": "",
        "visible": "true",
        "disabled": "false",
        "groupType": "default"
    },
    {
        "id": "36d8747804871232",
        "type": "ui-page",
        "name": "Delta SA 03",
        "ui": "e37b297fa64e1312",
        "path": "/page1",
        "icon": "home",
        "layout": "grid",
        "theme": "2be81ef526c8a6de",
        "breakpoints": [
            {
                "name": "Default",
                "px": "0",
                "cols": "3"
            },
            {
                "name": "Tablet",
                "px": "576",
                "cols": "6"
            },
            {
                "name": "Small Desktop",
                "px": "768",
                "cols": "9"
            },
            {
                "name": "Desktop",
                "px": "1024",
                "cols": "12"
            }
        ],
        "order": 1,
        "className": "",
        "visible": true,
        "disabled": false
    },
    {
        "id": "e37b297fa64e1312",
        "type": "ui-base",
        "name": "My Dashboard",
        "path": "/dashboard",
        "appIcon": "",
        "includeClientData": true,
        "acceptsClientConfig": [
            "ui-notification",
            "ui-control"
        ],
        "showPathInSidebar": false,
        "headerContent": "page",
        "navigationStyle": "default",
        "titleBarStyle": "default",
        "showReconnectNotification": true,
        "notificationDisplayTime": 1,
        "showDisconnectNotification": true,
        "allowInstall": true
    },
    {
        "id": "2be81ef526c8a6de",
        "type": "ui-theme",
        "name": "Default Theme",
        "colors": {
            "surface": "#ffffff",
            "primary": "#050505",
            "bgPage": "#b0b0b0",
            "groupBg": "#000000",
            "groupOutline": "#ffffff"
        },
        "sizes": {
            "density": "default",
            "pagePadding": "12px",
            "groupGap": "12px",
            "groupBorderRadius": "4px",
            "widgetGap": "12px"
        }
    }
]

It would help if you could post result of your MSSQL Query output for two pulse. (the contents of debug2 in your example)

@smanjunath211

the data is more that 999 so this is the latest output

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path/value for any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

Thank you for your help, I have found the solution

Would you like to share what the 'solution' was, so other people can benefit from your experience? You could also mark your thread as solved.
wed_solution