Convert voltage to percent in popular battery

Easy way to conver estimate percentage of capacity from voltage of battery. You can select chemistry type: li-ion, lifepo4 you can select cells count: int

Returns desire topic and calculated percent

[{"id":"dbfc88c7ea986b06","type":"subflow","name":"lithium volt to percents","info":"","category":"","in":[{"x":120,"y":120,"wires":[{"id":"f4a7f93f55cf6b85"}]}],"out":[{"x":640,"y":120,"wires":[{"id":"f4a7f93f55cf6b85","port":0}]}],"env":[{"name":"cellsCount","type":"num","value":"4","ui":{"icon":"font-awesome/fa-battery-3","label":{"en-US":"Cells count"},"type":"input","opts":{"types":["num"]}}},{"name":"topic","type":"str","value":""},{"name":"batType","type":"str","value":"lifepo4","ui":{"label":{"en-US":"battery type"},"type":"select","opts":{"opts":[{"l":{"en-US":"lifepo4"},"v":"lifepo4"},{"l":{"en-US":"li-ion"},"v":"liion"}]}}}],"meta":{},"color":"#3FADB5","icon":"node-red-dashboard/ui_gauge.png","status":{"x":520,"y":240,"wires":[{"id":"f4a7f93f55cf6b85","port":1}]}},{"id":"f4a7f93f55cf6b85","type":"function","z":"dbfc88c7ea986b06","name":"","func":"function mapIt(x, in_min, in_max, out_min, out_max) {\n    x = parseFloat(x);\n    in_min = parseFloat(in_min);\n    in_max = parseFloat(in_max);\n    out_min = parseFloat(out_min);\n    out_max = parseFloat(out_max);\n  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;\n}\n\nfunction get_percents_from_volts( volts ){\n\n    percent = 0.00;\n    volts = parseFloat( volts ) / parseFloat(env.get(\"cellsCount\")); // 4 cells in the 12v pack\n    \n    if( env.get(\"batType\") == \"lifepo4\" ){\n        li_voltage_to_percentage = [\n            [10.00,999],\n            [3.65, 100],\n            [3.40, 90],\n            [3.35, 80],\n            [3.32, 70],\n            [3.30, 60],\n            [3.27, 50],\n            [3.20, 40],\n            [3.15, 30],\n            [3.10, 20],\n            [3.05, 10],\n            [3.00, 5],\n            [2.80, 0],\n            [0.00,-999]\n        ];\n    }else{\n        li_voltage_to_percentage = [\n            [10.00,999],\n            [4.20, 100],\n            [4.06, 90],\n            [3.98, 80],\n            [3.92, 70],\n            [3.87, 60],\n            [3.82, 50],\n            [3.79, 40],\n            [3.77, 30],\n            [3.74, 20],\n            [3.68, 10],\n            [3.45, 5],\n            [3.00, 0],\n            [0.00,-999]\n        ];\n    }\n    li_voltage_to_percentage_len = li_voltage_to_percentage.length;\n    mmax = li_voltage_to_percentage[0];\n    mmin = li_voltage_to_percentage[1];\n   \n    for(i=0,ic=li_voltage_to_percentage_len-2; i<ic; i++){\n        if( volts <= parseFloat(li_voltage_to_percentage[i][0]) &&\n            volts >= parseFloat(li_voltage_to_percentage[i+1][0]) ){\n            mmax = li_voltage_to_percentage[i];\n            mmin = li_voltage_to_percentage[i+1];\n            //min = i;\n            break;\n        }\n    }\n   \n    percent = mapIt( volts, mmin[0], mmax[0], mmin[1],mmax[1] );\n    \n    //return 'volts:'+volts+' range:'+min+' max:';//percent;\n    return percent;\n}\n\nmsg = {\n    topic: env.get(\"topic\"),\n    payload: get_percents_from_volts( msg.payload ),\n    cells: env.get(\"cellsCount\"),\n    type: env.get(\"batType\")\n};\n\nmfill = \"green\";\nif( msg.payload <= 10 || msg.payload >= 90 )\n    mfill = \"red\";\n\n\n\nreturn [\n    msg,\n    {\n        payload:{\n            fill: mfill,   \n            shape:\"ring\",\n            text: msg.payload.toFixed(2)+\"%\"\n        }\n    }];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":120,"wires":[[],[]]}]

Flow Info

Created 1 month, 3 weeks ago
Rating: 5 1

Owner

Actions

Rate:

Node Types

Core
  • function (x1)
Other
  • subflow (x1)

Tags

  • battery
  • otplc
  • percents
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option