# IOT - Engine run time

**URL:** <https://discourse.nodered.org/t/iot-engine-run-time/21075>\
**Category:** Share Your Projects\
**Created:** [31 January 2020 10:09 UTC](https://discourse.nodered.org/t/iot-engine-run-time/21075 "2020-01-31T10:09:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [31 January 2020 10:09 UTC](https://discourse.nodered.org/t/iot-engine-run-time/21075/1 "2020-01-31T10:09:50Z")

</div>

If you ever have to record engine run time or have to record the time valves stay open etc. this little flow does it.

```auto
[{"id":"d5ebfa8d.412cf8","type":"tab","label":"Engine runtime","disabled":false,"info":""},{"id":"d1605568.7de958","type":"function","z":"d5ebfa8d.412cf8","name":"init engine runtime","func":"//var total_time = parseInt(msg.payload);\nvar t1 = Date.now();\n//flow.set('total_time',total_time);\nflow.set('total_time',0);\nflow.set('t1', t1);\nflow.set('t2', t1);\nreturn msg;\n","outputs":1,"noerr":0,"x":630,"y":100,"wires":[[]]},{"id":"52216aae.036d04","type":"function","z":"d5ebfa8d.412cf8","name":"stop","func":"var t2 = Date.now();\nvar t1 = flow.get('t1');\nvar delta = t2 -t1;\nvar total_time = parseInt(flow.get('total_time'));\ntotal_time = total_time + delta;\nflow.set('total_time',total_time);\nmsg.payload = total_time;\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":260,"wires":[["c713ac88.de924"]]},{"id":"c713ac88.de924","type":"function","z":"d5ebfa8d.412cf8","name":"engine runtime","func":"var ms = parseInt(msg.payload);\nvar t = ms/1000;\nvar h = (Math.floor(t / 3600)).toString();\nvar m = (\"0\" + (Math.floor(t%3600 / 60))).slice(-2);\nvar s = (\"0\" + (Math.floor(t%3600 % 60))).slice(-2);\n\nvar ts = h + \":\" + m + \":\" + s;\nflow.set('DG-WZ-total',{'ms':ms,'time':ts});\nreturn [\n {payload:{'ms':ms,'time':ts}},\n {payload:ms,filename:'/home/pi/duration/DG-WZ-Dauer.log'}\n];","outputs":2,"noerr":0,"x":600,"y":260,"wires":[["5554cf81.77314"],["9ecabb23.d8f278"]]},{"id":"5554cf81.77314","type":"debug","z":"d5ebfa8d.412cf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":240,"wires":[]},{"id":"5a25ad91.6ae9a4","type":"file","z":"d5ebfa8d.412cf8","name":"save engine time","filename":"","appendNewline":false,"createDir":true,"overwriteFile":"true","x":970,"y":280,"wires":[[]]},{"id":"9ecabb23.d8f278","type":"debug","z":"d5ebfa8d.412cf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":280,"wires":[]},{"id":"e9dc425a.7c913","type":"function","z":"d5ebfa8d.412cf8","name":"start","func":"var t1 = Date.now();\nflow.set('t1', t1);\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":200,"wires":[[]]},{"id":"240b10a7.1799c","type":"inject","z":"d5ebfa8d.412cf8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":320,"y":200,"wires":[["e9dc425a.7c913"]]},{"id":"7b86756d.26fc6c","type":"inject","z":"d5ebfa8d.412cf8","name":"load at program start","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":220,"y":40,"wires":[["ce968ea6.d8dcb"]]},{"id":"d5b21314.a8fff","type":"inject","z":"d5ebfa8d.412cf8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":320,"y":260,"wires":[["52216aae.036d04"]]},{"id":"fbc67eab.e8d97","type":"inject","z":"d5ebfa8d.412cf8","name":"init when testing","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":420,"y":140,"wires":[["d1605568.7de958"]]},{"id":"ce968ea6.d8dcb","type":"function","z":"d5ebfa8d.412cf8","name":"path/file","func":"return {filename:'/home/pi/duration/Engine_Time.log'};\n","outputs":1,"noerr":0,"x":400,"y":40,"wires":[[]]},{"id":"cd0850f9.b3d18","type":"file in","z":"d5ebfa8d.412cf8","name":"load engine time","filename":"","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":420,"y":100,"wires":[["d1605568.7de958"]]},{"id":"4929bb84.4c6af4","type":"inject","z":"d5ebfa8d.412cf8","name":"load total time at programm start","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":100,"wires":[[]]}]

```

---

<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:** [31 March 2020 10:09 UTC](https://discourse.nodered.org/t/iot-engine-run-time/21075/2 "2020-03-31T10:09:52Z")

</div>

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