Calculate percentage between time

Hi Everyone,

I am new to this forum.
I struggle with calculating percentage between two times.

I have the time in two objects. The 'operating' time and the 'downtime'.
I want to calculate the 'uptime' (by operating time minus downtime)
What am I doing wrong?

And in the end I want to calculate uptime percentage in relation to operating time.

Who wants to help me with this?

First thing you should do is go lookup how javascript works with date/time. A good starting point would be the Date Object https://www.w3schools.com/jsref/jsref_obj_date.asp

Take a look thru there and then if you run into a problem, show what you have tried.

Hi zenofmud,

Thanks for your reply!

I've already read a lot about the getDate function.
I did follow your link, but still I don't find the solution.
I tried a lot. I don't know where to look anymore. :worried:

I got the time from a timer function.
Do you know if the output of the timer function can be used for calculations?

I will share the flow below:

[{"id":"fd7c4176.ab8a7","type":"comment","z":"da622957.7a8bf8","name":" Availability = Downtime / Total Time ","info":"","x":220,"y":60,"wires":[]},{"id":"7b812286.ed5d4c","type":"inject","z":"da622957.7a8bf8","name":"on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"on","payload":"","payloadType":"date","x":130,"y":420,"wires":[["d44f5e55.24191"]]},{"id":"ff30dd5c.c16b18","type":"inject","z":"da622957.7a8bf8","name":"off","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"off","payload":"","payloadType":"date","x":130,"y":460,"wires":[["d44f5e55.24191"]]},{"id":"6c168a15.61460c","type":"inject","z":"da622957.7a8bf8","name":"reset","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"reset","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"reset","payload":"","payloadType":"date","x":130,"y":500,"wires":[["d44f5e55.24191"]]},{"id":"957a824.8d04d","type":"debug","z":"da622957.7a8bf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":500,"wires":[]},{"id":"d44f5e55.24191","type":"dsm","z":"da622957.7a8bf8","name":"operating time","sm_config":"{\n    \"currentState\": \"stopped1\",\n    \"states\": {\n        \"stopped1\": {\n            \"on\": \"started1\"\n        },\n        \"started1\": {\n            \"inc\": \"counting1\",\n            \"off\": \"stopped1\"\n        },\n        \"counting1\": {\n            \"inc\": \"counting1\",\n            \"off\": \"stopped1\"\n        }\n    },\n    \"data\": {\n        \"prev_time\": null,\n        \"time\": 0,\n        \"seconds\": 0,\n        \"interval\": 1,\n        \"interval_output\": true,\n        \"hms_format\": true\n    },\n    \"methods\": {\n        \"init\": [\n            \"sm.calc_time = function() {\",\n            \"   var now = Date.now();\",\n            \"   sm.data.time += now - sm.data.prev_time;\",\n            \"   sm.data.prev_time = now;\",\n            \"   sm.data.seconds = Math.round(sm.data.time / 1000);\",\n            \"};\",\n            \"sm.sec2hhmmss = function(sec) {\",\n                \"var t = {};\",\n                \"t.h = pad(Math.floor(sec / 3600));\",\n                \"sec %= 3600;\",\n                \"t.m = pad(Math.floor(sec / 60));\",\n                \"t.s = pad(sec % 60);\",\n                \"return t.h+':'+t.m+':'+t.s;\",\n            \"};\"\n        ],\n        \"on\": [\n            \"if (sm.currentState === 'started1') {\",\n            \"   sm.data.prev_time = Date.now();\",\n            \"   resume('inc', msg);\",\n            \"}\",\n            \"output = false;\"\n        ],\n        \"inc\": [\n            \"timeout.interval = setTimeout(function() {\",\n            \"   sm.calc_time();\",\n            \"   msg.data = sm.data;\",\n            \"   if (sm.data.interval_output) {\",\n            \"       msg.payload = sm.data.hms_format ? sm.sec2hhmmss(sm.data.seconds): sm.data.seconds;\",\n            \"       node.send(msg);\",\n            \"   }\",\n            \"   resume('inc', msg);\",\n            \"}, sm.data.interval*1000);\",\n            \"output = false;\"\n        ],\n        \"off\": [\n            \"clearTimeout(timeout.interval);\",\n            \"sm.calc_time();\",\n            \"msg.payload = sm.data.hms_format ? sm.sec2hhmmss(sm.data.seconds): sm.data.seconds;\"\n        ],\n        \"reset\": [\n            \"sm.data.time = 0;\",\n            \"sm.data.seconds = 0;\"\n        ],\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.currentState === 'counting1' ? 'green' : 'grey';\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"'time ' + (sm.data.hms_format ? sm.sec2hhmmss(sm.data.seconds): sm.data.seconds);\"\n            }\n        }\n    }\n}\n","x":410,"y":460,"wires":[["d8845bab.6c9818","7a0a2523.84d74c"]]},{"id":"7afc18a9.0367a","type":"comment","z":"da622957.7a8bf8","name":"Total Time","info":"","x":160,"y":300,"wires":[]},{"id":"bb327f27.b0865","type":"link in","z":"da622957.7a8bf8","name":"","links":["752be2a7.4a0f3c","d5dd8f7e.bc482"],"x":275,"y":360,"wires":[["d44f5e55.24191"]]},{"id":"225cc97e.a3dcf6","type":"link in","z":"da622957.7a8bf8","name":"","links":["16e15d3a.8a3bcb","3129dd13.ffce6a"],"x":255,"y":440,"wires":[["d44f5e55.24191"]]},{"id":"fb79f2f6.370cb8","type":"link in","z":"da622957.7a8bf8","name":"","links":["ac2d0db8.12613","73808281.ba64fc"],"x":265,"y":400,"wires":[["d44f5e55.24191"]]},{"id":"d8845bab.6c9818","type":"influxdb out","z":"da622957.7a8bf8","influxdb":"52a06381.0494c4","name":"Database: raspioee (TotalTime)","measurement":"Total_Time","precision":"s","retentionPolicy":"","x":650,"y":380,"wires":[]},{"id":"50688912.3695a8","type":"comment","z":"da622957.7a8bf8","name":"Downtime","info":"","x":170,"y":720,"wires":[]},{"id":"1fec94bf.748fc3","type":"interval-length","z":"da622957.7a8bf8","format":"human","bytopic":true,"minimum":"","maximum":"","window":"","timeout":false,"msgTimeout":"10","minimumunit":"msecs","maximumunit":"msecs","windowunit":"msecs","msgTimeoutUnit":"secs","reset":true,"startup":true,"msgField":"interval","timestampField":"timestamp","repeatTimeout":false,"name":"","x":160,"y":880,"wires":[["8ee8d34a.cdcea","91003569.b226c8","2a0abc2c.b9258c"],["fc462763.3da72","5b0c8405.ef408c"]]},{"id":"57a73e2f.bc883","type":"change","z":"da622957.7a8bf8","name":"topic = \"sensorWELW\"","rules":[{"t":"set","p":"topic","pt":"msg","to":"sensorWELW","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":180,"y":840,"wires":[["1fec94bf.748fc3"]]},{"id":"fc462763.3da72","type":"debug","z":"da622957.7a8bf8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":880,"wires":[]},{"id":"8ee8d34a.cdcea","type":"debug","z":"da622957.7a8bf8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":840,"wires":[]},{"id":"c32fd39c.b56148","type":"influxdb out","z":"da622957.7a8bf8","influxdb":"52a06381.0494c4","name":"Database: raspioee (AlarmTest)","measurement":"AlarmTest","precision":"s","retentionPolicy":"","x":650,"y":760,"wires":[]},{"id":"91003569.b226c8","type":"function","z":"da622957.7a8bf8","name":"","func":"var interval =msg.interval\n\n    \nif (interval>10.00)  ;\n{\n    msg.payload=\n    {\n        numValue: interval,\n\n    }}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":760,"wires":[["c32fd39c.b56148"]]},{"id":"f1936937.055bb8","type":"inject","z":"da622957.7a8bf8","name":"SensorWELW","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":800,"wires":[["57a73e2f.bc883"]]},{"id":"ee424fba.c01d7","type":"inject","z":"da622957.7a8bf8","name":"on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"on","payload":"","payloadType":"date","x":210,"y":1240,"wires":[["51cebe96.f19048"]]},{"id":"73c2677b.ca95e","type":"inject","z":"da622957.7a8bf8","name":"off","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"off","payload":"","payloadType":"date","x":210,"y":1280,"wires":[["51cebe96.f19048"]]},{"id":"234e49b4.8d06c6","type":"inject","z":"da622957.7a8bf8","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"reset","payload":"","payloadType":"date","x":210,"y":1320,"wires":[["51cebe96.f19048"]]},{"id":"fd07da2d.a08d4","type":"debug","z":"da622957.7a8bf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":1280,"wires":[]},{"id":"51cebe96.f19048","type":"dsm","z":"da622957.7a8bf8","name":"Total Downtime Counter","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        \"prev_time\": null,\n        \"time\": 0,\n        \"seconds\": 0,\n        \"interval\": 1,\n        \"interval_output\": true,\n        \"hms_format\": true\n    },\n    \"methods\": {\n        \"init\": [\n            \"sm.calc_time = function() {\",\n            \"   var now = Date.now();\",\n            \"   sm.data.time += now - sm.data.prev_time;\",\n            \"   sm.data.prev_time = now;\",\n            \"   sm.data.seconds = Math.round(sm.data.time / 1000);\",\n            \"};\",\n            \"sm.sec2hhmmss = function(sec) {\",\n                \"var t = {};\",\n                \"t.h = pad(Math.floor(sec / 3600));\",\n                \"sec %= 3600;\",\n                \"t.m = pad(Math.floor(sec / 60));\",\n                \"t.s = pad(sec % 60);\",\n                \"return t.h+':'+t.m+':'+t.s;\",\n            \"};\"\n        ],\n        \"on\": [\n            \"if (sm.currentState === 'started') {\",\n            \"   sm.data.prev_time = Date.now();\",\n            \"   resume('inc', msg);\",\n            \"}\",\n            \"output = false;\"\n        ],\n        \"inc\": [\n            \"timeout.interval = setTimeout(function() {\",\n            \"   sm.calc_time();\",\n            \"   msg.data = sm.data;\",\n            \"   if (sm.data.interval_output) {\",\n            \"       msg.payload = sm.data.hms_format ? sm.sec2hhmmss(sm.data.seconds): sm.data.seconds;\",\n            \"       node.send(msg);\",\n            \"   }\",\n            \"   resume('inc', msg);\",\n            \"}, sm.data.interval*1000);\",\n            \"output = false;\"\n        ],\n        \"off\": [\n            \"clearTimeout(timeout.interval);\",\n            \"sm.calc_time();\",\n            \"msg.payload = sm.data.hms_format ? sm.sec2hhmmss(sm.data.seconds): sm.data.seconds;\"\n        ],\n        \"reset\": [\n            \"sm.data.time = 0;\",\n            \"sm.data.seconds = 0;\"\n        ],\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.currentState === 'counting' ? 'green' : 'grey';\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"'time ' + (sm.data.hms_format ? sm.sec2hhmmss(sm.data.seconds): sm.data.seconds);\"\n            }\n        }\n    }\n}\n","x":500,"y":1280,"wires":[["59f9dddb.7b8c6c","309c1637.cf2722"]]},{"id":"5b0c8405.ef408c","type":"change","z":"da622957.7a8bf8","name":"Set \"on\"","rules":[{"t":"set","p":"topic","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":180,"y":920,"wires":[["665501a.711ea","6de35ad1.e4817c","f3ce9d78.89bc9"]]},{"id":"59f9dddb.7b8c6c","type":"influxdb out","z":"da622957.7a8bf8","influxdb":"52a06381.0494c4","name":"Database: raspioee (Downtime_Counter)","measurement":"Total_Downtime_Counter","precision":"s","retentionPolicy":"","x":680,"y":1220,"wires":[]},{"id":"96f7d727.4d0b18","type":"debug","z":"da622957.7a8bf8","name":"","active":false,"console":"false","complete":"false","x":690,"y":980,"wires":[]},{"id":"2a0abc2c.b9258c","type":"trigger","z":"da622957.7a8bf8","name":"","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"0","extend":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":500,"y":920,"wires":[["96f7d727.4d0b18","8c07b35c.856718"]]},{"id":"665501a.711ea","type":"change","z":"da622957.7a8bf8","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":980,"wires":[["2a0abc2c.b9258c"]]},{"id":"7c9866ff.d51318","type":"link in","z":"da622957.7a8bf8","name":"Downtime_Off","links":["8a4b50a5.b90008"],"x":335,"y":1220,"wires":[["51cebe96.f19048"]]},{"id":"8a4b50a5.b90008","type":"link out","z":"da622957.7a8bf8","name":"Downtime_Off","links":["220b40cc.82d848","7c9866ff.d51318","9e736227.96beb"],"x":795,"y":940,"wires":[]},{"id":"8c07b35c.856718","type":"change","z":"da622957.7a8bf8","name":"Set \"off\"","rules":[{"t":"set","p":"topic","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":940,"wires":[["8a4b50a5.b90008"]]},{"id":"6de35ad1.e4817c","type":"link out","z":"da622957.7a8bf8","name":"Downtime_On","links":["b578119d.c7a978","87b115c3.d1f6b8","a5ca323b.38f5a8","e8d4357a.d92ab8"],"x":295,"y":1000,"wires":[]},{"id":"b578119d.c7a978","type":"link in","z":"da622957.7a8bf8","name":"","links":["6de35ad1.e4817c"],"x":335,"y":1180,"wires":[["51cebe96.f19048"]]},{"id":"dec927f2.f487a8","type":"comment","z":"da622957.7a8bf8","name":"Total Downtime Counter","info":"","x":210,"y":1120,"wires":[]},{"id":"ebb77ba4.5a5778","type":"inject","z":"da622957.7a8bf8","name":"on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"on","payload":"","payloadType":"date","x":210,"y":1540,"wires":[["96f5c632.cbdc28"]]},{"id":"25b8d7b7.2893e8","type":"inject","z":"da622957.7a8bf8","name":"off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"0.6","topic":"off","payload":"","payloadType":"date","x":210,"y":1580,"wires":[["96f5c632.cbdc28"]]},{"id":"c03b6ab0.eb4c08","type":"inject","z":"da622957.7a8bf8","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"reset","payload":"","payloadType":"date","x":210,"y":1620,"wires":[["96f5c632.cbdc28"]]},{"id":"f5b21cb6.4b917","type":"debug","z":"da622957.7a8bf8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":1580,"wires":[]},{"id":"9e736227.96beb","type":"link in","z":"da622957.7a8bf8","name":"","links":["8a4b50a5.b90008"],"x":335,"y":1520,"wires":[["96f5c632.cbdc28"]]},{"id":"19b5c9f2.5c111e","type":"influxdb out","z":"da622957.7a8bf8","influxdb":"52a06381.0494c4","name":"Database: raspioee (Downtime_Last_Stop)","measurement":"Downtime_Last_Stop","precision":"s","retentionPolicy":"","x":690,"y":1520,"wires":[]},{"id":"87b115c3.d1f6b8","type":"link in","z":"da622957.7a8bf8","name":"","links":["6de35ad1.e4817c"],"x":335,"y":1480,"wires":[["96f5c632.cbdc28"]]},{"id":"891b9861.5b7a7","type":"comment","z":"da622957.7a8bf8","name":"Downtime since last Stop","info":"","x":210,"y":1440,"wires":[]},{"id":"3d77ad67.8fb9c2","type":"link in","z":"da622957.7a8bf8","name":"","links":["e2b78d97.5eab","d75aea2f.98a96"],"x":315,"y":920,"wires":[["2a0abc2c.b9258c"]]},{"id":"85706acb.20b2b8","type":"change","z":"da622957.7a8bf8","name":"Reset","rules":[{"t":"set","p":"topic","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":1680,"wires":[["96f5c632.cbdc28"]]},{"id":"e8d4357a.d92ab8","type":"link in","z":"da622957.7a8bf8","name":"","links":["6de35ad1.e4817c"],"x":215,"y":1680,"wires":[["85706acb.20b2b8"]]},{"id":"159b756d.ab0d1b","type":"link in","z":"da622957.7a8bf8","name":"","links":["16e15d3a.8a3bcb","e2b78d97.5eab","3129dd13.ffce6a","d75aea2f.98a96"],"x":305,"y":1340,"wires":[["51cebe96.f19048"]]},{"id":"f3ce9d78.89bc9","type":"debug","z":"da622957.7a8bf8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":460,"y":1060,"wires":[]},{"id":"96f5c632.cbdc28","type":"dsm","z":"da622957.7a8bf8","name":"Downtime since last Stop","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        \"prev_time\": null,\n        \"time\": 0,\n        \"seconds\": 0,\n        \"interval\": 1,\n        \"interval_output\": true,\n        \"hms_format\": true\n    },\n    \"methods\": {\n        \"init\": [\n            \"sm.calc_time = function() {\",\n            \"   var now = Date.now();\",\n            \"   sm.data.time += now - sm.data.prev_time;\",\n            \"   sm.data.prev_time = now;\",\n            \"   sm.data.seconds = Math.round(sm.data.time / 1000);\",\n            \"};\",\n            \"sm.sec2hhmmss = function(sec) {\",\n                \"var t = {};\",\n                \"t.h = pad(Math.floor(sec / 3600));\",\n                \"sec %= 3600;\",\n                \"t.m = pad(Math.floor(sec / 60));\",\n                \"t.s = pad(sec % 60);\",\n                \"return t.h+':'+t.m+':'+t.s;\",\n            \"};\"\n        ],\n        \"on\": [\n            \"if (sm.currentState === 'started') {\",\n            \"   sm.data.prev_time = Date.now();\",\n            \"   resume('inc', msg);\",\n            \"}\",\n            \"output = false;\"\n        ],\n        \"inc\": [\n            \"timeout.interval = setTimeout(function() {\",\n            \"   sm.calc_time();\",\n            \"   msg.data = sm.data;\",\n            \"   if (sm.data.interval_output) {\",\n            \"       msg.payload = sm.data.hms_format ? sm.sec2hhmmss(sm.data.seconds): sm.data.seconds;\",\n            \"       node.send(msg);\",\n            \"   }\",\n            \"   resume('inc', msg);\",\n            \"}, sm.data.interval*1000);\",\n            \"output = false;\"\n        ],\n        \"off\": [\n            \"clearTimeout(timeout.interval);\",\n            \"sm.calc_time();\",\n            \"msg.payload = sm.data.hms_format ? sm.sec2hhmmss(sm.data.seconds): sm.data.seconds;\"\n        ],\n        \"reset\": [\n            \"sm.data.time = 0;\",\n            \"sm.data.seconds = 0;\"\n        ],\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.currentState === 'counting' ? 'green' : 'grey';\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"'time ' + (sm.data.hms_format ? sm.sec2hhmmss(sm.data.seconds): sm.data.seconds);\"\n            }\n        }\n    }\n}\n","x":500,"y":1580,"wires":[["f5b21cb6.4b917","19b5c9f2.5c111e"]]},{"id":"d7688d61.80761","type":"comment","z":"da622957.7a8bf8","name":"Uptime Calculation","info":"","x":370,"y":1840,"wires":[]},{"id":"f7fd7be7.52d78","type":"link out","z":"da622957.7a8bf8","name":"TotalTime","links":["6570b222.6a6c8c","9908bf92.d675f8"],"x":835,"y":560,"wires":[]},{"id":"6fd77f56.c27a78","type":"link in","z":"da622957.7a8bf8","name":"","links":["6dacabd9.11c40c"],"x":115,"y":2020,"wires":[["5519afa5.e7b61"]]},{"id":"6dacabd9.11c40c","type":"link out","z":"da622957.7a8bf8","name":"Downtime","links":["6fd77f56.c27a78"],"x":875,"y":1320,"wires":[]},{"id":"89dfb542.536f98","type":"debug","z":"da622957.7a8bf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":490,"y":1900,"wires":[]},{"id":"9908bf92.d675f8","type":"link in","z":"da622957.7a8bf8","name":"","links":["f7fd7be7.52d78","5a5a99f4.61a018"],"x":115,"y":1940,"wires":[["5519afa5.e7b61"]]},{"id":"39023bea.9c339c","type":"comment","z":"da622957.7a8bf8","name":"Total Time","info":"","x":150,"y":1900,"wires":[]},{"id":"d7c50fb5.8e092","type":"comment","z":"da622957.7a8bf8","name":"Downtime","info":"","x":140,"y":1980,"wires":[]},{"id":"5519afa5.e7b61","type":"join","z":"da622957.7a8bf8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":330,"y":1960,"wires":[["89dfb542.536f98","49aab2a4.da7c34"]]},{"id":"311581f8.cee7d6","type":"debug","z":"da622957.7a8bf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":1960,"wires":[]},{"id":"77df5c05.ec9fa4","type":"link in","z":"da622957.7a8bf8","name":"","links":["16e15d3a.8a3bcb","e2b78d97.5eab","3129dd13.ffce6a","d75aea2f.98a96"],"x":215,"y":1740,"wires":[["96f5c632.cbdc28"]]},{"id":"1431d456.9585fc","type":"link in","z":"da622957.7a8bf8","name":"","links":["d24305d.49752f8"],"x":195,"y":760,"wires":[["57a73e2f.bc883"]]},{"id":"8f8e4f91.db64d","type":"change","z":"da622957.7a8bf8","name":"","rules":[{"t":"change","p":"topic","pt":"msg","from":"counting","fromt":"str","to":"stop","tot":"str"},{"t":"change","p":"topic","pt":"msg","from":"stopped","fromt":"str","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":1440,"wires":[[]]},{"id":"7a0a2523.84d74c","type":"function","z":"da622957.7a8bf8","name":"operating","func":"msg.topic=\"operating\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":500,"wires":[["957a824.8d04d","f7fd7be7.52d78"]]},{"id":"309c1637.cf2722","type":"function","z":"da622957.7a8bf8","name":"downtime","func":"msg.topic=\"downtime\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":720,"y":1280,"wires":[["fd07da2d.a08d4","6dacabd9.11c40c"]]},{"id":"49aab2a4.da7c34","type":"function","z":"da622957.7a8bf8","name":"","func":"var msg1 = { payload:\"operating\" };\nvar msg2 = { payload:\"downtime\" };\nmsg.payload = msg1 - msg2\n\n\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":1960,"wires":[["311581f8.cee7d6"]]},{"id":"52a06381.0494c4","type":"influxdb","z":"","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"raspioee","name":"","usetls":false,"tls":""}]

You can use the javascript split() function to split the strings at the colons to give you the three values (hours, mins, seconds) in an array. Then you can convert that to seconds for each one and then divide them and multiply by 100 to give the percentage.

Or do it in a change node with JSONata expression

[{"id":"75500b3f.812ff4","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round(\t   (100 / ($toMillis(\"1970-01-01T\" & payload.operating & \"Z\")  /\t           $toMillis(\t           \"1970-01-01T\" &\t       payload.downtime & \"Z\"\t       )) ), 2)\t   \t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":780,"wires":[["ca18810d.3ed83"]]}]

Should give you down time percentage.

Or, building on that, to get the uptime percentage

[{"id":"9ad32d5f.b88f5","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":4720,"wires":[]},{"id":"a79cb83d.9c08e8","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"operating\": \"01:00:00\", \"downtime\": \"00:01:12\"}","payloadType":"json","x":90,"y":4720,"wires":[["790df0f9.031f18"]]},{"id":"790df0f9.031f18","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"100 *\t   (1 - ($toMillis(\"1970-01-01T\" & payload.downtime & \"Z\")  /\t           $toMillis(\t           \"1970-01-01T\" &\t       payload.operating & \"Z\"\t       )) )\t   \t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":4720,"wires":[["9ad32d5f.b88f5"]]}]

Hi Colin,

That's a great idea!
Do you have any idea how I can do that?

I said how to do it in a function node, or you can use JSONata in a change node as suggested by @E1cid

Thank you Colin. I don't know how to do this in de function node with the split() function. I'm a beginner in JSON programming.
And JSONata is also difficult for me.

I saw when I view the "complete msg object" the is a 'seconds' value in it.
I would bring this 'seconds' value from the "operating time" and "downtime" to a "join node" so I could calculate with this values.

Can anyone explain me how I can set the seconds value to a var in a function node?
I used the command: var runtime = { payload: 'seconds' };
But it doesn't work properly...
I want to use the value after seconds:

Time: 00:02:09
Seconds value = 129
seconds

feeding in msg.payload "00:45:03"

msg.seconds = msg.payload.split(":")[2]; // a string "03"
msg.seconds = Number(msg.payload.split(":")[2]); // a number 3

if you are talking about the msg.data.second from your debug
then

let runtime = msg.data.seconds;

You don't need to use a Join node as they are already in the same message.
Please spend a couple of hours watching the excellent node-red Essentials. That will save a lot of time in the long run.

When you have done that if you can't work it out then ask again explaining what you are trying to achieve with the various values in the message.

Thanks Colin!

I spend hours watching node-red Essentials.

I made the runtime msg.runtime
And the downtime msg.downtime
In de debug menu I see the values in seconds from msg.runtime and msg.downtime

I made a function node where I want to calculate the uptime by: runtime minus downtime .

Like the example: Percent calculation

But it doesn't work properly :frowning:
I have no idea why. It's driving me crazy! :triumph:

What am I doing wrong? :thinking:

runtime

Not a particularly good programmer but it appears to me there is a basic misunderstanding of percentage. Percentage literally means compared to 100 and what I see in your posting is a difference or subtraction of values. You will need to use something like X/100 = runtime/downtime to get a percentage. How you write the code can be in several different ways but you aren't completing the operation to get to percent.

Show us what is going into the node, what is coming out, and tell us exactly what you expect out for the values you are feeding in.

First I want to calculate the uptime.
After that works, I would make a calculation for uptime in %.

The uptime calculation in seconds didn't work.
The output I got is: NaN

Inputs:
msg.runtime = 2718
msg.downtime = 9
The msg.uptime should be: 2709
But the only output I get is: NaN

The outcome I want to achieve is uptime in percentage.
But I can't even finish a simple math problem of runtime minus downtime...

msg.downtime msg.runtime msg.uptime

@shel
That's right. I know that I must calculate ((uptime/runtime)*100) to get a percentage of the uptime.
But first but I can't even finish a simple math problem of (runtime) - (downtime)...

Are your messages numbers? It looks like they are but:
Try
var runtime = Number(msg.runtime);
var downtime = Number(msg.downtime);
see if you still get NaN,(Not A Number)

Other than that I'll turn it back over to the real wizard here (@Colin)

@shel

I tried it, but the outcome of msg.uptime is still NaN

Number