eschava / node-red-contrib-xiaomi-ble

Xiaomi Bluetooth4 (BLE) sensors
MIT License
19 stars 15 forks source link

LYWSD03MMC always shows 109% on Battery value #24

Open Ocean-85 opened 3 years ago

Ocean-85 commented 3 years ago

Hi,

my tempsensors always show 109% of battery which in my eys can't be correct value here, or? msg : Object object payload: object battery: 109 temperature: 20.9 humidity: 51 address: "a4:c1:xx:xx:xx:xx" _msgid: "ef91c9b2.cf4118"

This is now shown for some month so I even don't see any change in that value. Can you check if your using the correct hex for the battery value here?

Kind regards

Ocean

chaosblog commented 3 years ago

For me, it's also 109% - I'm trying to debug it, but I'm not completely sure about the code. msg.battery = data.toString().charCodeAt(0); Line 24, xiaomi-ble.js This part gets the battery state - but data.toString() doesn't contain any battery information for me. It contains a domain name.

const prep = typeof data === typeof 's' ? data : JSON.stringify(data.toString('hex')).replace(/\"/gi, ''); Line 58, xiaomi-ble.js gets the data which contains temperature, humidity and battery voltage in it. Temperatue are the first two hex numbers in little endian, followed by one hex numer big endian humidity. the other two hex numbers are the voltage in mV little endian. Example: prep contains b8083f030c

So the doesn't report a percentage, it reports the voltage. A percentage could be calculated, based on the coin-cell discharge curve. There is already some code at zigbee-herdsman-converters which could be used for the conversion from voltage to percent. Also it could be possible to return two battery fields, on with the percentage, one with the voltage.

I could prepare a pull request for all of that, I changed the code for me already and tested it. But I only have LYWSD03MMC, so I can't check if my changes would work with other devices too.