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.