# Help ME with calculating the percentage

**URL:** <https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404>\
**Category:** General\
**Created:** [31 May 2024 09:20 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404 "2024-05-31T09:20:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrBatman](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@MrBatman](https://discourse.nodered.org/u/MrBatman)\
**Post date:** [31 May 2024 09:20 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/1 "2024-05-31T09:20:30Z")

</div>

I want to add two values received from OPC UA, calculate the percentage of one of these values in the sum, and display it on the screen. How can I do this?"

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [31 May 2024 09:55 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/2 "2024-05-31T09:55:50Z")

</div>

It would help if you shared an example of the data you are receiving and perhaps share what you've tried already?

Do you have Dashboard working for example? If so, which one?

---

<div class="post-metadata">

**Author:** ![MrBatman](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@MrBatman](https://discourse.nodered.org/u/MrBatman)\
**Post date:** [31 May 2024 10:42 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/3 "2024-05-31T10:42:49Z")

</div>

![opc tag](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca525a956af84d15f4f29be4a1b3b86bf9e739d6.jpeg)  
 ![DASH BOARD](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/5/a512ac3c6c7c8c4b736700b8c55df0c730bf65eb.jpeg)

I want to calcculate percentage fo Kenet 1 Çıkış OK and Kenet 1 Çıkış 1 NOK value

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 May 2024 11:05 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/4 "2024-05-31T11:05:58Z")

</div>

Due to the fact items arriving from seprate are always separate messages, you need to combine the items of interest into one message so that you can access them both at the same time.

Luckily this is pretty simple - see [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

Once you have both values in a single message, your calculation is pretty simple.

Use debug nodes to understand what is happening in your flows

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [31 May 2024 12:25 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/5 "2024-05-31T12:25:52Z")

</div>

between the input link node and the ui display node, add either a function node or a change node.

For a function node, something like this should work (untested):

```auto
msg.percent = 100 * msg.NOK / msg.OK
return msg

```

For a change node, you can use an Expression to do a similar calculation.

---

<div class="post-metadata">

**Author:** ![MrBatman](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@MrBatman](https://discourse.nodered.org/u/MrBatman)\
**Post date:** [31 May 2024 13:08 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/6 "2024-05-31T13:08:23Z")

</div>

it didnt work . ı saw only NOK value.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [31 May 2024 15:30 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/7 "2024-05-31T15:30:55Z")

</div>

I think we need to see a few of the messages that contain Kenet 1 Çıkış data.

Can you put some debug nodes in your flow and post the output here?  
An image might be OK but the actual data is better, copied from the debug pane and posted between lines with backticks like this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/0/504f6fdca167e0c0f17155182363622a0a5060a6.png)

---

<div class="post-metadata">

**Author:** ![MrBatman](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@MrBatman](https://discourse.nodered.org/u/MrBatman)\
**Post date:** [31 May 2024 20:18 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/8 "2024-05-31T20:18:45Z")

</div>

![DEBUG](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8e4177af5305f4a6069c208262a35fb5ae495537.png)

Thats my data

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 May 2024 20:27 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/9 "2024-05-31T20:27:49Z")

</div>

> [@MrBatman](#):
>
> ![DEBUG](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8e4177af5305f4a6069c208262a35fb5ae495537.png)

As I said before...

> [@Steve-Mcl](#):
>
> Once you have both values in a single message, your calculation is pretty simple.

> [@Steve-Mcl](#):
>
> Luckily this is pretty simple - see [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [31 May 2024 20:42 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/10 "2024-05-31T20:42:24Z")

</div>

A pretty minimal response, yet enough to confirm that the values are in two separate messages and as @Steve-Mcl says, you need to get them into a single message.

It looks like msg.topic is a JavaScript object rather than a string. I wonder if that will hamper the join node.

---

<div class="post-metadata">

**Author:** ![MrBatman](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@MrBatman](https://discourse.nodered.org/u/MrBatman)\
**Post date:** [31 May 2024 21:02 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/11 "2024-05-31T21:02:53Z")

</div>

These values coming from opc ua and data types are Uınt16 .

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 May 2024 21:09 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/12 "2024-05-31T21:09:26Z")

</div>

> [@MrBatman](#):
>
> These values coming from opc ua

Yes, that is clear from your screenshots.

You STILL need to get both `payload`s into a single `msg` to you can compare the two.

this is fairly basic but important knowledge in order to be successful in Node-RED.

It may be worth you spending a little time learning the basics?

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<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:** [29 August 2024 21:10 UTC](https://discourse.nodered.org/t/help-me-with-calculating-the-percentage/88404/13 "2024-08-29T21:10:27Z")

</div>

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