# Sort Battery Discharge and Charge Values

**URL:** <https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806>\
**Category:** General\
**Tags:** function-node\
**Created:** [10 October 2022 13:27 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806 "2022-10-10T13:27:13Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![OnewheelerSA](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/onewheelersa/32/68946_2.png) [@OnewheelerSA](https://discourse.nodered.org/u/OnewheelerSA)\
**Post date:** [10 October 2022 13:27 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/1 "2022-10-10T13:27:13Z")

</div>

I am trying to figure out how to spilt my battery charge and discharge values.  
The output reads currently as a positive value (Charging) or a negative value (Discharging).  
I want to split these outputs so I can use them accordingly.  
I tried to use this post as a guide line but I couldn't figure it out.  
Data is being read from Modbus with the following function node.

```auto
[{"id":"8682e863.07c7f8","type":"function","z":"5d091907.72bc38","name":"Battery_Output_Power","func":"var a\na = (msg.payload.data / 1)\nif (a > 32767) {\n msg.payload = (0 - a + 65535) / 1;\n} else {\n msg.payload = (0 - a) / 1;\n}\nif (msg.topic === \"battery_output_power\") {\n return [msg, null];\n} else {\n return [null, msg];\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1840,"y":1780,"wires":[["61ef70d1.c9fd7","360ab456.cb9dcc","bcad804a679170a4","458eb3b539c97444"]]}]

```

---

<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:** [10 October 2022 14:43 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/2 "2022-10-10T14:43:14Z")

</div>

Welcome to the forum @OnewheelerSA

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<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:** [10 October 2022 14:49 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/3 "2022-10-10T14:49:43Z")

</div>

Also, feed the value you have into a debug node and show us what you get.

---

<div class="post-metadata">

**Author:** ![OnewheelerSA](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/onewheelersa/32/68946_2.png) [@OnewheelerSA](https://discourse.nodered.org/u/OnewheelerSA)\
**Post date:** [10 October 2022 15:27 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/4 "2022-10-10T15:27:36Z")

</div>

Hi Colin, thanks for the help with that. I could not figure that out.  
So the current function node works perfectly, but it only gives me one outcome, either a negative value or a positive value. The current value from the debug node it -783. So the battery is currently discharging.  
I am needing to add 2 sensors to my flow chart so I need to split the positive and negative values so I can create the 2 sensors. Hope that makes sense?

---

<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:** [10 October 2022 16:00 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/5 "2022-10-10T16:00:02Z")

</div>

You can use a Switch node to check for \>= 0 or \<0 and send it to two outputs, is that what you mean? Alternatively, in your function node, if you are detecting +ve and -ve then use two outputs on the function node and send it to the appropriate one there.

---

<div class="post-metadata">

**Author:** ![OnewheelerSA](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/onewheelersa/32/68946_2.png) [@OnewheelerSA](https://discourse.nodered.org/u/OnewheelerSA)\
**Post date:** [10 October 2022 18:26 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/6 "2022-10-10T18:26:41Z")

</div>

Great, that worked using the switch node. Appreciate the help.  
Now I am wanting to change the negative output into a positive? Are you able to assist with that?

---

<div class="post-metadata">

**Author:** ![OnewheelerSA](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/onewheelersa/32/68946_2.png) [@OnewheelerSA](https://discourse.nodered.org/u/OnewheelerSA)\
**Post date:** [10 October 2022 19:25 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/7 "2022-10-10T19:25:11Z")

</div>

The other issue I am getting now is that when the battery stops charging and the value changes to negative, the positive output value just remains as the last known value. Is there a way to make the value 0 if it's not getting an active value from the sensor?

---

<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:** [10 October 2022 19:26 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/8 "2022-10-10T19:26:44Z")

</div>

> [@OnewheelerSA](#):
>
> Now I am wanting to change the negative output into a positive

Use a Range node for that.

---

<div class="post-metadata">

**Author:** ![OnewheelerSA](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/onewheelersa/32/68946_2.png) [@OnewheelerSA](https://discourse.nodered.org/u/OnewheelerSA)\
**Post date:** [10 October 2022 19:53 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/9 "2022-10-10T19:53:18Z")

</div>

Thanks, got that working now as well.

---

<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:** [10 October 2022 19:54 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/10 "2022-10-10T19:54:40Z")

</div>

> [@OnewheelerSA](#):
>
> when the battery stops charging and the value changes to negative, the positive output value just remains as the last known value

Feed the negative output into a Change node that sets the payload to 0 and merge that in with the positive wire, so each time you get a negative value it sends 0 as the positive value. Do the same the other way round for the negative value.

---

<div class="post-metadata">

**Author:** ![OnewheelerSA](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/onewheelersa/32/68946_2.png) [@OnewheelerSA](https://discourse.nodered.org/u/OnewheelerSA)\
**Post date:** [10 October 2022 19:55 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/11 "2022-10-10T19:55:43Z")

</div>

Thanks, let me give that a try.

---

<div class="post-metadata">

**Author:** ![OnewheelerSA](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/onewheelersa/32/68946_2.png) [@OnewheelerSA](https://discourse.nodered.org/u/OnewheelerSA)\
**Post date:** [10 October 2022 20:17 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/12 "2022-10-10T20:17:12Z")

</div>

@Colin Thanks for the assistance. I think I got it all working perfectly. I will test it all tomorrow.

---

<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:** [9 December 2022 20:17 UTC](https://discourse.nodered.org/t/sort-battery-discharge-and-charge-values/68806/13 "2022-12-09T20:17:35Z")

</div>

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