# Cumulative running time

**URL:** <https://discourse.nodered.org/t/cumulative-running-time/3278>\
**Category:** General\
**Created:** [19 September 2018 17:37 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278 "2018-09-19T17:37:01Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfehl](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jfehl](https://discourse.nodered.org/u/jfehl)\
**Post date:** [19 September 2018 17:37 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/1 "2018-09-19T17:37:01Z")

</div>

I am looking for suggestions on a node, or way to capture cumulative running time of equipment. For example: a machine runs for 2 hrs, then is down for 20 minutes, then starts again for 3 hrs, so the cumulative time is currently 5 hrs. I would also like to access this value at anytime say 5 hrs 4 minutes, 5 minutes, 6 minutes etc...

thanks

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [19 September 2018 20:25 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/2 "2018-09-19T20:25:14Z")

</div>

How are you capturing the running time i.e. is it initiated from a flow in your code i.e. Start Stop - or is it an external event you are being notified of (through MQTT or some other mechanism).

I am not aware of a node as i went looking for one a while ago.

How long do you want to keep the info and does it have to persist across Node-red restarts, reloads etc ?

Is it only a single device you wish to monitor ?

Craig

---

<div class="post-metadata">

**Author:** ![jfehl](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jfehl](https://discourse.nodered.org/u/jfehl)\
**Post date:** [19 September 2018 20:38 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/3 "2018-09-19T20:38:49Z")

</div>

I would be initiating the start and stops based on the value of a single input.....if "true" start keeping track of time and it "false" stop the tally and just hold the value.

It would not have to store it for restarts or reloads.

Yes, a single device at a time.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [19 September 2018 20:40 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/4 "2018-09-19T20:40:05Z")

</div>

This can be done using a state machine node like `dsm`.  
I'd upload a flow if you want to try.

---

<div class="post-metadata">

**Author:** ![jfehl](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jfehl](https://discourse.nodered.org/u/jfehl)\
**Post date:** [19 September 2018 20:50 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/5 "2018-09-19T20:50:02Z")

</div>

Please upload something I could try.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [19 September 2018 20:59 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/6 "2018-09-19T20:59:23Z")

</div>

This is a basic example but it can be modified / improved:

```auto
[{"id":"5b83329.fe12dcc","type":"inject","z":"895bbff8.0e6c4","name":"on","topic":"on","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3420,"wires":[["cb31d3c.377153"]]},{"id":"2a77b639.990e2a","type":"inject","z":"895bbff8.0e6c4","name":"off","topic":"off","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3460,"wires":[["cb31d3c.377153"]]},{"id":"9770ea8e.ef7358","type":"inject","z":"895bbff8.0e6c4","name":"reset","topic":"reset","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3500,"wires":[["cb31d3c.377153"]]},{"id":"cb31d3c.377153","type":"dsm","z":"895bbff8.0e6c4","name":"operating time","sm_config":"{\n \"currentState\": \"stopped\",\n \"states\": {\n \"stopped\": {\n \"on\": \"started\"\n },\n \"started\": {\n \"inc\": \"counting\",\n \"off\": \"stopped\"\n },\n \"counting\": {\n \"inc\": \"counting\",\n \"off\": \"stopped\"\n }\n },\n \"data\": {\n \"time\": 0,\n \"step\": 1,\n \"prev_time\": null\n },\n \"methods\": {\n \"on\": [\n \"if (sm.currentState === 'started') {\",\n \" sm.data.prev_time = Date.now();\",\n \" node.send(msg);\",\n \" resume('inc', msg);\",\n \"}\"\n],\n \"inc\": [\n \"timeout.interval = setTimeout(function() {\",\n \" var now = Date.now();\",\n \" sm.data.time += now - sm.data.prev_time;\",\n \" sm.data.prev_time = now;\",\n \" resume('inc', msg);\",\n \"}, sm.data.step*1000);\",\n \"output = false;\"\n],\n \"off\": [\n \"clearTimeout(timeout.interval);\"\n],\n \"reset\": [\n \"sm.data.time = 0;\"\n],\n \"onTransition\": [\n \"sm.text = 'time '+ Math.round(sm.data.time / 1000);\"\n],\n \"status\": {\n \"fill\": {\n \"get\": \"sm.currentState === 'counting' ? 'green' : 'grey';\"\n },\n \"shape\": \"dot\",\n \"text\": {\n \"get\": \"sm.text;\"\n }\n }\n }\n}","x":330,"y":3460,"wires":[["9d1fae36.a0661"]]},{"id":"9d1fae36.a0661","type":"debug","z":"895bbff8.0e6c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":3460,"wires":[]}]

```

You need to install the latest `dsm` version.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [19 September 2018 21:15 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/7 "2018-09-19T21:15:36Z")

</div>

> [@cflurin](#):
>
> [{"id":"5b83329.fe12dcc","type":"inject","z":"895bbff8.0e6c4","name":"on","topic":"on","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3420,"wires":[["cb31d3c.377153"]]},{"id":"2a77b639.990e2a","type":"inject","z":"895bbff8.0e6c4","name":"off","topic":"off","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3460,"wires":[["cb31d3c.377153"]]},{"id":"9770ea8e.ef7358","type":"inject","z":"895bbff8.0e6c4","name":"reset","topic":"reset","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":3500,"wires":[["cb31d3c.377153"]]},{"id":"cb31d3c.377153","type":"dsm","z":"895bbff8.0e6c4","name":"operating time","sm\_config":"{\n "currentState": "stopped",\n "states": {\n "stopped": {\n "on": "started"\n },\n "started": {\n "inc": "counting",\n "off": "stopped"\n },\n "counting": {\n "inc": "counting",\n "off": "stopped"\n }\n },\n "data": {\n "time": 0,\n "step": 1,\n "prev\_time": null\n },\n "methods": {\n "on": [\n "if (sm.currentState === 'started') {",\n " sm.data.prev\_time = Date.now();",\n " node.send(msg);",\n " resume('inc', msg);",\n "}"\n],\n "inc": [\n "timeout.interval = setTimeout(function() {",\n " var now = Date.now();",\n " sm.data.time += now - sm.data.prev\_time;",\n " sm.data.prev\_time = now;",\n " resume('inc', msg);",\n "}, sm.data.step\*1000);",\n "output = false;"\n],\n "off": [\n "clearTimeout(timeout.interval);"\n],\n "reset": [\n "sm.data.time = 0;"\n],\n "onTransition": [\n "sm.text = 'time '+ Math.round(sm.data.time / 1000);"\n],\n "status": {\n "fill": {\n "get": "sm.currentState === 'counting' ? 'green' : 'grey';"\n },\n "shape": "dot",\n "text": {\n "get": "sm.text;"\n }\n }\n }\n}","x":330,"y":3460,"wires":[["9d1fae36.a0661"]]},{"id":"9d1fae36.a0661","type":"debug","z":"895bbff8.0e6c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":3460,"wires":}]

Wow that is WAAY Cool - very nice !

Craig

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [19 September 2018 23:12 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/8 "2018-09-19T23:12:55Z")

</div>

That is a neat solution... is there a way to initialize some data when the node is deployed, before any state transition methods are ever executed? I was trying to add a total elapsed time counter, but need to set `sm.data.init_time = Date.now();` and couldn't seem to find a place to put that.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [20 September 2018 03:24 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/9 "2018-09-20T03:24:14Z")

</div>

> [@shrickus](#):
>
> That is a neat solution... is there a way to initialize some data when the node is deployed, before any state transition methods are ever executed? I was trying to add a total elapsed time counter, but need to set `sm.data.init_time = Date.now();` and couldn't seem to find a place to put that.

Good question! I'll put it on my todo list.  
A workaround:

add `init` into the data and execute the statement once in the `onBeforeTransition` method:

```auto
    "data": {
        "init": true,
        "init_time": null,
        "time": 0,
        "step": 1,
        "prev_time": null
    },
    "methods": {
        "onBeforeTransition": [
            "if (sm.data.init) {",
            " sm.data.init_time = Date.now();",
            " sm.data.init = false;",
            "}"
        ],
        "on": [
       ...

```

---

<div class="post-metadata">

**Author:** ![jfehl](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jfehl](https://discourse.nodered.org/u/jfehl)\
**Post date:** [20 September 2018 12:21 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/10 "2018-09-20T12:21:52Z")

</div>

Excellent. Thank you for sharing. That should work out nicely.

---

<div class="post-metadata">

**Author:** ![jfehl](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jfehl](https://discourse.nodered.org/u/jfehl)\
**Post date:** [20 September 2018 18:13 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/11 "2018-09-20T18:13:05Z")

</div>

I am trying to utilize one of the dsm examples and modify one to fit what I am trying to accomplish. Looking at the CO2 example, it has "value" arriving in as msg.payload.value. I am trying to use a function Node and set the number that is coming in to the function node as msg.payload to msg.payload.value so that is looks the same to the dsm node. I was trying something like:

if (msg.payload \> 5)  
{ msg.payload.value = msg.payload};  
return msg;

However, I still only get the value to be in msg.payload

Can you tell me where I am going wrong?

My ultimate goal is to read in a value (Total weight) divide it by the elapsed time determined by the dsm, and return the answer every second or so.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [20 September 2018 19:07 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/12 "2018-09-20T19:07:49Z")

</div>

Can you post your flow here or send me a direct message (click on my user icon and click 'message')

I'll have a look.

Note: [how to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![emilk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/emilk/32/69242_2.png) [@emilk](https://discourse.nodered.org/u/emilk)\
**Post date:** [16 October 2022 10:18 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/13 "2022-10-16T10:18:29Z")

</div>

Hello, Apologies in advance for lame question - everyone starts at some time, now is the time for me to start with NodeRed.  
Searching for same solution I found your suggestion and good comments of others.  
I wish I could implement and play with this in NodeRed under Home HomeAssistant but I don't know what shall I do with the code you propose in order to see functional node in the flow.  
Lame, isn't it?  
Could you, please, help me starting?  
Thanks in advance, pointing me to relevent instruction / documentation to read and learn (RTFM) is fully expected.  
Emil

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 October 2022 11:03 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/14 "2022-10-16T11:03:49Z")

</div>

Hi @emilk

Welcome to the forum.

Let me recommend (via canned text) a few things that should put you in good stead. Once you've covered these, come back and evaluate this 4-year-old thread. It may not be what you want & you may have other/better ideas. I say that because the DSM node those guys talk about is not _really_ visual / low-code programming. In fact, it's not even really functional programming. Anyhow, you can judge for yourself once you have a bit knowledge

PS. I will be closing this old thread.

* * *

### first stop...

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

### JSON? Sting? Object?

[This post](https://dev.to/desoga/javascript-object-vs-json-demystified-494j) should help to clarify the difference between Javascript objects and JSON.

### When it is an object, it is _REALLY EASY_ to grab parts of the msg...

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 to 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)

### Forum stuff...

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

### other resources...

- [https://flows.nodered.org/](https://flows.nodered.org/) - nodes, readmes, demo flows
- [https://cookbook.nodered.org/](https://cookbook.nodered.org/) - common patterns.

Good luck.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 October 2022 11:06 UTC](https://discourse.nodered.org/t/cumulative-running-time/3278/15 "2022-10-16T11:06:38Z")

</div>


