Calculate percentage between time

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

Either msg.downtime or msg.runtime is missing
we need to see better debugs and the section of flow this comes from.

Feed an inject into the functon set with msg downtime and msg,uptime.

Until @Colin can weigh in, are you sure the numbers or payload (msg.runtime) and (msg.downtime) are what you think they are? You can stick
node.status({fill:"blue",shape:"dot",text: msg.runtime});
in your node and see underneath the node what the value is
[EDIT]
I think @E1cid may have the right idea though, unless you are doing some tricks upstream of the node I'll bet you are missing one of the values, you realize that the values aren't persistent so unless the message has both values in it you will need to save them somewhere then read them back or ensure they are both present in the msg.

@E1cid
I double checken what the incoming values are.
They are real values:

And now with node.status:

But to the previous point, are msg.runtime and msg.downtime in the same message? In other words does the value runtime come from one node and the value downtime come from another? It looks that way in your flow. If that is the case then you will be missing one of the numbers when you do your math.
try this status and see if both numbers appear at the same time
node.status({fill:"blue",shape:"dot",text: msg.runtime + " " + msg.downtime});

They are in seperate message streams . Set the debug msg.uptime to 'complete message object' and you will see they are not in same debug message. you will need to join the messages or store them.

Example of storing to context, first run will have default 0 values.

[{"id":"de919c9d.bae398","type":"function","z":"8d22ae29.7df6d","name":"","func":"var runtime = msg.runtime || (flow.get('runtime')  || 0);\nvar downtime = msg.downtime || (flow.get('downtime') || 0);\nflow.set(['runtime','downtime'], [runtime, downtime]);\n\nmsg.uptime = runtime - downtime;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":820,"wires":[["ca18810d.3ed83"]]}]

You have now given us the vital information that they are not in the same message (I think you gave the impression earlier that they were in the same message).
Rather than using context I prefer to join them using the technique shown in this example from the cookbook. Move the values or structures into msg.payload in each path and make sure they have different topics. Import the example from the cookbook and play with it and make sure you understand how it works. Then you should be able to incorporate it in your flow.