keptenkurk / BS440

Python code to talk to Medisana BS440 bluetooth enabled bathroom scale
MIT License
50 stars 34 forks source link

Division by zero in BS440csv.py #102

Open aeff81 opened 2 years ago

aeff81 commented 2 years ago

There's a potential "division by 0" in BS440csv.py if size = 0 (Guest-User for example). Due to my lack of Python skills, I solved it the ugly way. But I would suggest that somebody, who knows what he's doing, adds a check if (size == 0) { size = 1 } Here's my ugly patch, just to show the location of the Problem:

diff --git a/plugins/BS440csv.py b/plugins/BS440csv.py
index 0131124..a2a62e6 100644
--- a/plugins/BS440csv.py
+++ b/plugins/BS440csv.py
@@ -32,6 +32,7 @@ class Plugin:
                calculateddata = []
                datetimedata = []
                size = persondata[0]['size'] / 100.00
+               size = 1
                for i, e in list(enumerate(weightdata)):
                        bmiDict = {}
                        bmiDict['bmi'] = round(weightdata[i]['weight'] / (size * size), 1)
Boldfor commented 1 year ago

This is not only relevant for „guest users“, and also not limited to the .csv-plugin (am using the mqtt-plugin): I have the BS 444, and sometimes the „user detection“ fails (or does not even start), which results in „only weight being sent“ (with everything else being blank). Also in these (multiple) cases they script stops with „division by 0“.

Traceback (most recent call last):
  File "/home/pi/BS440-HA-AD/BS440.py", line 332, in <module>
    appendBmi(persondata[0]['size'], weightdata)
  File "/home/pi/BS440-HA-AD/BS440.py", line 122, in appendBmi
    element['bmi'] = round(element['weight'] / (size * size), 1)
ZeroDivisionError: float division by zero
Boldfor commented 1 year ago

@aeff81, not sure whether I know what I'm doing ;) but I added a PR for this bug (https://github.com/creatvty/BS440-HA-AD/pull/2) which fixed it for me.

@creatvty, please have a look.

Boldfor commented 1 year ago

@aeff81, is merged and should be fixed. Maybe you can check and close the issue.