Skip to content

fix(sensor): return None for 0 mV battery readings on mains-powered nodes - #417

Open
calin2k wants to merge 1 commit into
meshcore-dev:mainfrom
calin2k:fix/zero-mv-battery-unsupported
Open

calin2k wants to merge 1 commit into
meshcore-dev:mainfrom
calin2k:fix/zero-mv-battery-unsupported

Conversation

@calin2k

@calin2k calin2k commented Nov 8, 2026

Copy link
Copy Markdown

Summary

In MeshCore firmware (ESP32Board.h), boards without a battery ADC pin (such as mains/USB-powered gateways like the Elecrow ThinkNode G3) return 0 from getBattMilliVolts():

uint16_t getBattMilliVolts() override {
#ifdef PIN_VBAT_READ
  ...
#else
  return 0; // not supported
#endif
}

Previously in meshcore-ha:

  • calculate_battery_percentage(0) clamped (0 - 3000) / 1200 * 100 to 0.0%
  • MeshCoreSensor (battery_voltage) divided 0 / 1000.0 to report 0.0 V
  • MeshCoreRepeaterSensor (bat) fell through if key == "bat" and ... value > 0 to return value (0) when bat == 0

As a result, mains-powered companion radios and repeaters reported a false 0% / 0.0 V depleted battery state in Home Assistant instead of unknown (None).

Changes

  • Return None from calculate_battery_percentage() when voltage_mv is None or <= 0.
  • Return None in MeshCoreSensor (battery_voltage) and MeshCoreRepeaterSensor (bat) when the reported millivolt reading is <= 0.
  • Add unit tests in tests/test_battery_zero_mv.py covering 0 mV, None, and positive battery readings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant