Cumulative running time

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

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

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.

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

Please upload something I could try.

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

[{"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.

Wow that is WAAY Cool - very nice !

Craig

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:

    "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": [
       ...

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

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.

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

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

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

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

``` 
   code goes here 
```

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

See this post for more details - How to share code or flow json

other resources...

Good luck.

1 Like