-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use the same battery calculation as ZHA in Tuya TS0203 door sensors #7681
Conversation
It looks like this change causes some bounciness in battery readings. I'm not sure why, but one of my sensors (
Other sensors seem to report them at the same time. Should we remove the battery percent reporting from these devices in the configure function? Or, simply say "this device reports inaccurate battery info, it's not our problem" in the docs? I suppose we could start with just ignoring the models that I have on hand similar to the exposes function. |
I propose to ignore the battery % by adding a fromZigbee converter which only processes the voltage. |
I started down the path of adding a new converter, but I don't think it makes for a clear design. The problem is that we still need a way to define what battery curve to use - and there's no way to easily pass additional parameters into the converter - except for meta, which we already have: batteryUsingOnlyVoltage: {
cluster: 'genPowerCfg',
type: ['attributeReport', 'readResponse'],
convert: (model, msg, publish, options, meta) => {
const payload: KeyValueAny = {};
if (msg.data.hasOwnProperty('batteryVoltage') && (msg.data['batteryVoltage'] < 255)) {
// Deprecated: voltage is = mV now but should be V
payload.voltage = msg.data['batteryVoltage'] * 100;
if (model.meta && model.meta.battery && model.meta.battery.voltageToPercentage) {
payload.battery = batteryVoltageToPercentage(payload.voltage, model.meta.battery.voltageToPercentage);
}
}
return payload;
},
} satisfies Fz.Converter, I think adding a |
I ended up doing the converter method, because |
src/devices/tuya.ts
Outdated
@@ -816,7 +816,7 @@ const definitions: Definition[] = [ | |||
model: 'TS0203', | |||
vendor: 'Tuya', | |||
description: 'Door sensor', | |||
fromZigbee: [fz.ias_contact_alarm_1, fz.battery, fz.ignore_basic_report, fz.ias_contact_alarm_1_report], | |||
fromZigbee: [fz.ias_contact_alarm_1, fz.batteryIgnoringPercentage, fz.ignore_basic_report, fz.ias_contact_alarm_1_report], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ends up doing this for all models, not just the ones I've tested myself. Is that OK?
src/converters/fromZigbee.ts
Outdated
@@ -352,6 +396,7 @@ const converters1 = { | |||
convert: (model, msg, publish, options, meta) => { | |||
const payload: KeyValueAny = {}; | |||
if (msg.data.hasOwnProperty('batteryPercentageRemaining') && (msg.data['batteryPercentageRemaining'] < 255)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking now, maybe we should always ignore the battery % reporting when voltageToPercentage
is set since those will always conflict with each other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That vastly simplifies this PR, and avoids the need to have a new converter. I've force-pushed here as resolving merge conflicts wasn't worth the effort.
4614fd1
to
35af9c7
Compare
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
… into use-zha-linear-battery
model.meta?.battery?.voltageToPercentage != null && | ||
model.meta?.battery?.voltageToPercentage == null && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always enjoy when writing a test uncovers a bug :D.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 😄
Thanks! |
…oenkk#7681) * (fix) Ignore reported percentage when using voltage to calculate * fix style * Use optional chaining for optional properties Co-authored-by: Koen Kanters <koenkanters94@gmail.com> * Add tests for battery percentage conversion * Fix inverted null check --------- Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
This PR improves battery readings for TS0203 sensors, or at least matches ZHA which will allow for greater collaboration and hopefully fewer support threads.
I recently purchased a load tester for batteries, and have been using that to check devices which seemed to have unreliable battery reporting.
TS0203 door sensors:
I haven't truly run one of these batteries out yet though, so I don't know for sure what the lower voltage end is.
This has previously been reported at Koenkk/zigbee2mqtt#17337.
Here's the corresponding code in ZHA, which doesn't have any device specific overrides so applies to these sensors:
https://github.com/zigpy/zha-device-handlers/blob/c6ed94a52a469e72b32ece2a92d528060c7fd034/zhaquirks/__init__.py#L195-L228
I traced the voltage numbers to the PR, which doesn't describe why they were changed:
zigpy/zha-device-handlers#208
Likewise, the issue and commits in zigbee2mqtt don't describe how it's voltage readings were found:
Koenkk/zigbee2mqtt#4466
There's also this discussion with no resolution: Koenkk/zigbee2mqtt#17337
With all this in mind, I think:
So, here's a PR that does that!
There is a greater risk of devices going from 2500 mV to zero with no warning. Today, the issue is that users may replace perfectly fine batteries prematurely. I'm hoping to track this myself and see if there's a voltage drop I can rely on to tweak this in the future, but that may take many months or even years!