I wrote this flow recently to try and help track my daily computer usage.
The flow:
[{"id":"39fd3a77.cbb756","type":"group","z":"26262ba1.62dcbc","name":"Uptime","style":{"label":true},"nodes":["ece2d27e.1926e8","4511a9a0.0b2e38","97bdf5b5.57e4c8","611b65a9.71b0f4","ac7fc38f.acb178","1f6371fd.ba4d56","2fa4bddf.cb243a","15466412.7808ac","1644881a.fb9ab","abd001b.e0ee4","fadf9736.69d95","11f8a8d.dd42957","6a30385a.7bb1f","ad90ee9d.430698","8042da6d.3015d"],"x":4204,"y":39,"w":792,"h":242},{"id":"5a81f3be.561bdc","type":"subflow","name":"Time Stamp","info":"**3 outputs. 1 - msg.payload holds the time. 2 - msg.time holds the time in a way to be used for reading time in a log file. 3 - outputs nsg.time in a format usable for file names**","category":"","in":[{"x":80,"y":100,"wires":[{"id":"682ac304.0590d4"}]}],"out":[{"x":640,"y":180,"wires":[{"id":"cf554e88.9fff18","port":0},{"id":"682ac304.0590d4","port":0}]},{"x":640,"y":230,"wires":[{"id":"682ac304.0590d4","port":0},{"id":"3db4df41.634848","port":0}]},{"x":640,"y":280,"wires":[{"id":"e4ca4227.3109c8","port":0},{"id":"682ac304.0590d4","port":0}]}],"env":[],"color":"#FF8888","outputLabels":["For use in log files","msg.time","For filename use"],"icon":"node-red/timer.svg"},{"id":"cf554e88.9fff18","type":"moment","z":"5a81f3be.561bdc","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm:ss","locale":"en_AU","output":"payload","outputType":"msg","outTz":"Australia/Sydney","x":390,"y":180,"wires":[["e4ca4227.3109c8","c810aa73.a5706"]]},{"id":"e4ca4227.3109c8","type":"string","z":"5a81f3be.561bdc","name":"","methods":[{"name":"replaceAll","params":[{"type":"str","value":":"},{"type":"str","value":""}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":440,"y":280,"wires":[[]]},{"id":"c810aa73.a5706","type":"change","z":"5a81f3be.561bdc","name":"TOPIC","rules":[{"t":"move","p":"payload","pt":"msg","to":"time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":230,"wires":[["3db4df41.634848"]]},{"id":"be4fd533.c9abb","type":"change","z":"5a81f3be.561bdc","name":"Save","rules":[{"t":"set","p":"payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":140,"wires":[["c7ad4b3c.3a5368"]]},{"id":"3db4df41.634848","type":"change","z":"5a81f3be.561bdc","name":"Get","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":230,"wires":[[]]},{"id":"682ac304.0590d4","type":"switch","z":"5a81f3be.561bdc","name":"check topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"TIMESTAMP","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":200,"y":100,"wires":[[],["be4fd533.c9abb"]]},{"id":"c7ad4b3c.3a5368","type":"change","z":"5a81f3be.561bdc","name":"TimeStamp","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":180,"wires":[["cf554e88.9fff18"]]},{"id":"ece2d27e.1926e8","type":"subflow:5a81f3be.561bdc","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"","x":4520,"y":150,"wires":[["97bdf5b5.57e4c8"],[],[]]},{"id":"4511a9a0.0b2e38","type":"inject","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"Clock","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":4350,"y":150,"wires":[["611b65a9.71b0f4","ece2d27e.1926e8"]]},{"id":"97bdf5b5.57e4c8","type":"gate","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"","controlTopic":"control","defaultState":"closed","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":4690,"y":120,"wires":[["ac7fc38f.acb178"]]},{"id":"611b65a9.71b0f4","type":"exec","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","command":"uptime -p","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":4510,"y":100,"wires":[["2fa4bddf.cb243a"],[],[]]},{"id":"ac7fc38f.acb178","type":"function","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"","func":"msg.payload = flow.get(\"Uptime\") + \"Last alive \" + msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":4805,"y":120,"wires":[["15466412.7808ac","1644881a.fb9ab","8042da6d.3015d"]],"l":false},{"id":"1f6371fd.ba4d56","type":"function","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"set","func":"msg.payload = \"open\";\nmsg.topic = \"control\";\nreturn msg;","outputs":1,"noerr":0,"x":4690,"y":160,"wires":[["97bdf5b5.57e4c8"]]},{"id":"2fa4bddf.cb243a","type":"change","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"Uptime","rules":[{"t":"set","p":"Uptime","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":4700,"y":80,"wires":[[]]},{"id":"15466412.7808ac","type":"file","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"Alive","filename":"/home/me/.node-red/public/uptime.txt","appendNewline":true,"createDir":true,"overwriteFile":"true","encoding":"none","x":4900,"y":120,"wires":[[]]},{"id":"1644881a.fb9ab","type":"debug","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"Uptime","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":4850,"y":160,"wires":[]},{"id":"abd001b.e0ee4","type":"delay","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":4700,"y":200,"wires":[["1f6371fd.ba4d56"]]},{"id":"fadf9736.69d95","type":"subflow:5a81f3be.561bdc","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"","x":4520,"y":200,"wires":[[],["abd001b.e0ee4"],["11f8a8d.dd42957"]]},{"id":"11f8a8d.dd42957","type":"function","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"build msg","func":"msg.spath = \"/home/me/.node-red/public/\";\nmsg.dpath = \"/home/me/.node-red/public/history/\";\n\nmsg.payload = \"Powered on at \" + msg.payload + \".db\";\nnode.status(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":4700,"y":240,"wires":[["ad90ee9d.430698"]]},{"id":"6a30385a.7bb1f","type":"inject","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"Boot detector","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"","payload":"BOOT SIGNAL","payloadType":"str","x":4330,"y":200,"wires":[["fadf9736.69d95"]]},{"id":"ad90ee9d.430698","type":"fs-ops-move","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"","sourcePath":"spath","sourcePathType":"msg","sourceFilename":"uptime.txt","sourceFilenameType":"str","destPath":"dpath","destPathType":"msg","destFilename":"payload","destFilenameType":"msg","link":false,"x":4850,"y":240,"wires":[[]]},{"id":"8042da6d.3015d","type":"mqtt out","z":"26262ba1.62dcbc","g":"39fd3a77.cbb756","name":"","topic":"uptime/txt","qos":"","retain":"","broker":"378c0403.8cda04","x":4910,"y":80,"wires":[]},{"id":"378c0403.8cda04","type":"mqtt-broker","name":"TIMEPI MQTT","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"true","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
No foreign nodes, but a sub-flow I wrote included.
Basically it does this:
Boot: rename and move current file to (existing path)/history
and rename it to one of my weird filenames. (Not really that weird)
MQTT
listens in and a python
script shows me on the screen the usage for THIS time.
Ok. No problem/s yet.
But!
Today is 9 Feb. This is the only time I have turned on the machine. (First/only session for today)
Looking in the directory (history
) I see this:
How (semi rhetorical) were those files made?
I get that today when I turned it on, yesterday's "log" would be renamed to "Powerd on at...." with today's timestamp.
That explaines one of the two files.
06:48 is about the time I powered on the machine.
But it hasn't been turned off since then.
Note I did say ABOUT. 07:40 is also possible.
This is what a screen shot of the flow shows me which is why I changed the original claim of 06:xx to 07:xx
DIgging a bit deeper, these are the file contents:
me@me-desktop:~/.node-red/public/history$ cat Powered\ on\ at\ 2021-02-09\ 064813.db
up 1 hour, 3 minutes
Last alive 2021-02-08 22:55:30
me@me-desktop:~/.node-red/public/history$ cat Powered\ on\ at\ 2021-02-09\ 074737.db
up 58 minutes
Last alive 2021-02-09 07:47:11
me@me-desktop:~/.node-red/public/history$ cd ..
me@me-desktop:~/.node-red/public$ cat uptime.txt
up 3 hours, 3 minutes
Last alive 2021-02-09 09:52:35
me@me-desktop:~/.node-red/public$
So uptime.txt
us the current session.
Uptime 3:03. So that means it started .... 09:54 - 3:03 = 06:51
Which doesn't tie in with the flow's "powered on at" time.
It says 07:47
The uptime
does match. The 2:54
and 3:03` is only 9 minutes and that would have passed as I was looking at things and posting.
Low priority.
But it just seems confusing how the second file got created with todays' date if I haven't tured off the machine today since I turned it on.
Update:
The flow I posted is in a group. Shame on me for not reading all the stuff on groups.
But it would appear that the inject
node set to inject on boot is triggered every time I DEPLOY something on any flow on the machine.
Errrr....... Is that what should happen?