# Calculate percentage between time

**URL:** <https://discourse.nodered.org/t/calculate-percentage-between-time/34448>\
**Category:** General\
**Created:** [18 October 2020 11:50 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448 "2020-10-18T11:50:44Z")\
**Posts on this page:** 14\
**Page:** 2

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 October 2020 18:31 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/21 "2020-10-18T18:31:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shel](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shel](https://discourse.nodered.org/u/shel)\
**Post date:** [18 October 2020 18:31 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/22 "2020-10-18T18:31:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MrWolff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrwolff/32/30557_2.png) [@MrWolff](https://discourse.nodered.org/u/MrWolff)\
**Post date:** [18 October 2020 18:40 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/23 "2020-10-18T18:40:02Z")

</div>

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

 ![runtime - downtime = uptime](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6c8da9a7adb68681f18427948c6472d4a9a45ec1.png)

---

<div class="post-metadata">

**Author:** ![MrWolff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrwolff/32/30557_2.png) [@MrWolff](https://discourse.nodered.org/u/MrWolff)\
**Post date:** [18 October 2020 18:42 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/24 "2020-10-18T18:42:30Z")

</div>

> [@shel](#):
>
> node.status({fill:"blue",shape:"dot",text: msg.runtime});

And now with node.status:

 ![With run status](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/0536c23abda86c65137cc8ea15ffc310f51f3bb6.png)

---

<div class="post-metadata">

**Author:** ![shel](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shel](https://discourse.nodered.org/u/shel)\
**Post date:** [18 October 2020 18:48 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/25 "2020-10-18T18:48:40Z")

</div>

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});

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 October 2020 18:49 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/26 "2020-10-18T18:49:13Z")

</div>

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.

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 October 2020 19:28 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/27 "2020-10-18T19:28:35Z")

</div>

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](https://cookbook.nodered.org/basic/join-streams). 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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 October 2020 20:35 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/28 "2020-10-18T20:35:09Z")

</div>

I am interested in where the messages come from, are the values received from the device in separate messages or are you splitting them in node red? If you are splitting them then don't do that.  
If they arrive in separate messages where are they coming from and how do you get them?

---

<div class="post-metadata">

**Author:** ![MrWolff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrwolff/32/30557_2.png) [@MrWolff](https://discourse.nodered.org/u/MrWolff)\
**Post date:** [19 October 2020 08:39 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/29 "2020-10-19T08:39:29Z")

</div>

Hi Colin,

The messages msg.runtime and msg.downtime come from different timers via different messages.  
Since i discovered i could give the 'seconds value' of the timer to a self chosen msg.x name I tried to solve it like this way.

Before I also tried to do it like the example described in the cookbook.  
I have defined the timers by different topic names.  
How can I define the different topics to variabeles in the function node?

Is it like?: var runtime = msg.topic.runtime

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 October 2020 09:26 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/30 "2020-10-19T09:26:32Z")

</div>

Did you import the Joining Streams example and play with it so that you understand what it does?

**[Edit]** If so then where in the message does the example need the values it is joining, and what message property does it use to know which message is which?

---

<div class="post-metadata">

**Author:** ![MrWolff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrwolff/32/30557_2.png) [@MrWolff](https://discourse.nodered.org/u/MrWolff)\
**Post date:** [20 October 2020 16:38 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/31 "2020-10-20T16:38:24Z")

</div>

I imported the flow as shown in the cookbook. And played with it to onderstaand what is going on.

The disadvantage of a join node is that it only gives a msg.payload when all messages have arrived. I mean: first both msg.topic messages must have arrived. Only then does it issue a msg.payload

I also tried the flow from @E1cid  
That works great!  
The only one issue. And that is when the msg.runtime gives the second value. And the msg.downtime counter is still at zero (0). Then the calculation doesnt go well.  
But when the msg.downtime counter counter once, everything works fine.

 ![Availability % calculation](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b006e9abf016cc17ff0100f7dc5b6c0adcd2932a.png)

Thanks guys!  
I am grateful to you for your help.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [20 October 2020 16:44 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/32 "2020-10-20T16:44:20Z")

</div>

If you store runtime and downtime to context earlier in your flow , then they will see present later and the default 0 will not be called.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 October 2020 16:44 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/33 "2020-10-20T16:44:24Z")

</div>

> [@wolffgeert](#):
>
> first both msg.topic messages must have arrived. Only then does it issue a msg.payload

That is only true if 'After number of message parts' is set to 2. If you set that to 1 and select 'And every subsequent message', then you will get a message after the first message and every subsequent message. The topic of each message sent tells you which input message has triggered this output.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 December 2020 16:44 UTC](https://discourse.nodered.org/t/calculate-percentage-between-time/34448/34 "2020-12-19T16:44:28Z")

</div>

Closed on 19 December 2020 16:44 UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.nodered.org/t/calculate-percentage-between-time/34448.md?page=1)
