question

Felix avatar image
Felix asked

only positive numbers in node-red from battery monitor

Hi,

I get the current from battery monitor but the numbers are always positive, even when the battery discharges. How can I get the sign to distinguish beween charging and discharging

Node-RED
1 comment
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

It looks like the value is represented as a signed 16 bit number.
I would use a function, containing:

if (msg.payload > 2**8) {
    msg.payload -=  2**16
}
return msg;

It checks if the number is larger than 2^8. If so, it subtracts 2^16 from the value.

0 Likes 0 ·
1 Answer
Sebastian avatar image
Sebastian answered ·

I had a similar problem. But my Batrium was sending 65536 - current if the current is negative.

So i used this:

msg.payload = msg.payload[0];

if(msg.payload > 20000){

msg.payload = msg.payload - 65536;

}

return msg;

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.