# Accumulated time decreasing instead of increasing

**URL:** https://discourse.nodered.org/t/accumulated-time-decreasing-instead-of-increasing/96916
**Category:** General
**Tags:** function-node
**Created:** [6 May 2025 06:13 UTC](https://discourse.nodered.org/t/accumulated-time-decreasing-instead-of-increasing/96916 "2025-05-06T06:13:46Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Dar](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@Dar](https://discourse.nodered.org/u/Dar)
#### Post date: [6 May 2025 06:13 UTC](https://discourse.nodered.org/t/accumulated-time-decreasing-instead-of-increasing/96916/1 "2025-05-06T06:13:46Z")

</div>

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.

```auto
[
    {
        "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"
        }
    }
]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/66309c3a126119d6db66def52944458ca0d6e6ac.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/661cba06841cb8c864fce8ebc4313388b4bff1ab.png)

---

<div class="post-metadata">

### Author: ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)
#### Post date: [6 May 2025 06:22 UTC](https://discourse.nodered.org/t/accumulated-time-decreasing-instead-of-increasing/96916/2 "2025-05-06T06:22:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Dar](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@Dar](https://discourse.nodered.org/u/Dar)
#### Post date: [6 May 2025 06:25 UTC](https://discourse.nodered.org/t/accumulated-time-decreasing-instead-of-increasing/96916/3 "2025-05-06T06:25:15Z")

</div>

@smanjunath211

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30a8f7bd3790bbf5f3abb7f08797a176f3c9ed97.png)

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

---

<div class="post-metadata">

### Author: ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)
#### Post date: [6 May 2025 06:39 UTC](https://discourse.nodered.org/t/accumulated-time-decreasing-instead-of-increasing/96916/4 "2025-05-06T06:39:49Z")

</div>

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

### Author: ![Dar](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@Dar](https://discourse.nodered.org/u/Dar)
#### Post date: [7 May 2025 06:28 UTC](https://discourse.nodered.org/t/accumulated-time-decreasing-instead-of-increasing/96916/5 "2025-05-07T06:28:02Z")

</div>

Thank you for your help, I have found the solution

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [7 May 2025 06:34 UTC](https://discourse.nodered.org/t/accumulated-time-decreasing-instead-of-increasing/96916/6 "2025-05-07T06:34:26Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c16c9e5cdac9b976f98ec02cb7b97769e59ce110.png)

---

<div class="post-metadata">

### Author: ![Dar](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@Dar](https://discourse.nodered.org/u/Dar)
#### Post date: [8 May 2025 06:26 UTC](https://discourse.nodered.org/t/accumulated-time-decreasing-instead-of-increasing/96916/7 "2025-05-08T06:26:41Z")

</div>

Instead of adding the total time, I changed my approach with a timer that start at 7am and stop at 7pm. The timer will only count the true state and pause at false state.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [22 May 2025 06:27 UTC](https://discourse.nodered.org/t/accumulated-time-decreasing-instead-of-increasing/96916/8 "2025-05-22T06:27:13Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
