Sort Battery Discharge and Charge Values

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.

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

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 ```)

``` 
   code goes here 
```

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

See this post for more details - How to share code or flow json

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

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?

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.

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?

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?

Use a Range node for that.

Thanks, got that working now as well.

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.

Thanks, let me give that a try.

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

1 Like

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