Stopwatch event for a newbie

I'm new to node red and javascript, so forgive me if this has already been covered elsewhere.

Im trying to record the length of time a boiler is switched on.
I intend to store and or display the duration into a graph (with the intention of storing it into a database/sreadsheet etc. Perhaps even graph the times into a daily /weekly bar chart

I can get the boiler to go on and off, but I need a stopwatch type function/node to start a timer as the boiler comes on and stops when it goes off.

maybe like this thread ? Cumulative running time
or this node may help - https://flows.nodered.org/node/node-red-contrib-interval-length

Thanks dceejay

As I'm coming toNode Red & Javascript new, I'm struggling to get to terms with basics things such as the terminology and operation concepts of node red.

For instance I've just discovered today that an injected string input is case sensitive for the dsm node, whereas Pete Scargill's Bigtimer seems to be more relaxed about the case of text. Quite irritating until I delved inside the dsm node.

Thanks

Yes, it is generally safer with JavaScript to assume that something is case sensitive.

I just realised how ingrained case sensitivity is for me as a long time Linux user and how it might not be so for a windows user for example.

1 Like

Check this flow with a modified DSM node i did with the help of #cflurin. Best of all it's persistent across reboot, for that you have to modify your setting.js file to allow it.

[{"id":"f6bcc450.adfc18","type":"inject","z":"fffe22a.86fdbe","name":"inject me once","topic":"set","payload":"{\"triggerInput\":\"payload\",\"currentState\":\"stopped\",\"states\":{\"stopped\":{\"on\":\"started\"},\"started\":{\"inc\":\"counting\",\"off\":\"stopped\"},\"counting\":{\"inc\":\"counting\",\"off\":\"stopped\"}},\"data\":{\"prev_time\":null,\"time\":0,\"seconds\":0,\"interval\":5},\"methods\":{\"init\":[\"sm.calc_time = function () {\",\" var now = Date.now();\",\" sm.data.time += now - sm.data.prev_time;\",\" sm.data.prev_time = now;\",\" sm.data.seconds = Math.round(sm.data.time / 1000);\",\"};\",\"sm.sec2hhmmss = function(t) {\",\" var min = t/60;\",\" var t_h = pad(parseInt(min / 60));\",\" var t_m = pad(parseInt(min % 60));\",\" var t_s = pad(Math.round(t % 60));\",\" return t_h+':'+t_m+':'+t_s;\",\"};\"],\"on\":[\"if (sm.currentState === 'started') {\",\" sm.data.prev_time = Date.now();\",\" resume('inc', msg);\",\"}\",\"output = false;\"],\"inc\":[\"timeout.interval = setTimeout(function() {\",\" sm.calc_time();\",\" msg.data = sm.data;\",\" /* msg.payload = sm.sec2hhmmss(sm.data.seconds);\",\" node.send(msg); */\",\" resume('inc', msg);\",\"}, sm.data.interval*1000);\",\"output = false;\"],\"off\":[\"clearTimeout(timeout.interval);\",\"sm.calc_time();\",\"msg.payload = sm.sec2hhmmss(sm.data.seconds);\"],\"reset\":[\"sm.data.time = 0;\",\"sm.data.seconds = 0;\"],\"status\":{\"fill\":{\"get\":\"sm.currentState === 'counting' ? 'green' : 'grey';\"},\"shape\":\"dot\",\"text\":{\"get\":\"'time '+ sm.sec2hhmmss(sm.data.seconds);\"}}}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":380,"wires":[["b17dab3d.fe0408"]]},{"id":"3a5d5987.01a966","type":"inject","z":"fffe22a.86fdbe","name":"on","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":370,"y":420,"wires":[["b17dab3d.fe0408"]]},{"id":"15f725b3.17c50a","type":"inject","z":"fffe22a.86fdbe","name":"off","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":370,"y":460,"wires":[["b17dab3d.fe0408"]]},{"id":"b17dab3d.fe0408","type":"dsm","z":"fffe22a.86fdbe","name":"moteur","sm_config":"","x":560,"y":460,"wires":[["d3f9ceef.122a4"]]},{"id":"d3f9ceef.122a4","type":"debug","z":"fffe22a.86fdbe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":750,"y":460,"wires":[]},{"id":"5c5d6cf.7d3ef94","type":"inject","z":"fffe22a.86fdbe","name":"Reset","topic":"reset","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":370,"y":500,"wires":[["b17dab3d.fe0408"]]},{"id":"882b828f.104e8","type":"comment","z":"fffe22a.86fdbe","name":"time formated & persistent counter ","info":"","x":420,"y":340,"wires":[]}]

And with a Mysql node you insert start time and update with end time, and reupdate a second after with start minus stop time, that will give you one line in Mysql with start, stop, and duration for each cycle.