node-red-contrib-xiaomi-ble icon indicating copy to clipboard operation
node-red-contrib-xiaomi-ble copied to clipboard

LYWSD03MMC always shows 109% on Battery value

Open Ocean-85 opened this issue 4 years ago • 1 comments

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

Ocean-85 avatar Sep 20 '20 10:09 Ocean-85

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

  • b8 08 convertes to 2232 (be aware of little endian!) the display states 22.3°C - this fits the 2232
  • 3f convertes to 63, the display shows also 63%
  • -03 0c_ convertes to 3075 (also little endian!), which means 3075 mV or 3.075 V. This could be plausible because I'm using a fresh coin cell. A CR2032 coin cell has a nominal voltage of 3V.

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.

chaosblog avatar Nov 12 '20 20:11 chaosblog