Skip to content

fix: add missing state_class and device_class to sensor entities#311

Merged
tomquist merged 3 commits into
tomquist:developfrom
michikrug:fix-sensor-state-device-classes
Jun 4, 2026
Merged

fix: add missing state_class and device_class to sensor entities#311
tomquist merged 3 commits into
tomquist:developfrom
michikrug:fix-sensor-state-device-classes

Conversation

@michikrug
Copy link
Copy Markdown
Contributor

@michikrug michikrug commented Jun 4, 2026

Several sensor entities were missing state_class, preventing Home Assistant from recording long-term statistics and excluding them from the Energy Dashboard.

Affected sensors across b2500Base, b2500V1, and venus devices:

  • Battery percentage / SoCdevice_class: battery sensors were missing state_class: measurement, so they did not appear as selectable options in HA's Energy Dashboard battery configuration.
  • Voltage sensors — cell voltages (min, max, diff, avg, individual cells) and solar input voltages had device_class: voltage but no state_class.
  • Temperature sensorstemperature_min/temperature_max and BMS cell temperatures had device_class: temperature but no state_class.
  • BMS physical measurements (venus) — voltage, current, temperature, charge voltage, and MOSFET temperature in the BMS field loop were missing state_class: measurement.
  • Off-grid power (venus) — had device_class: apparent_power but was missing state_class: measurement.

The Jupiter device already had these correctly set and served as the reference. All added values are state_class: measurement (instantaneous, non-cumulative readings). No logic changes — discovery payload fields only.

Tested: manually published a modified MQTT discovery payload with state_class: measurement via MQTT Explorer and confirmed the entity appeared in HA Developer Tools with the correct attribute and became selectable in the Energy Dashboard.

AI Transparency Note: Change was prepared and implemented with Claude Code

Summary by CodeRabbit

  • Improvements
    • Home Assistant sensors for battery level, capacity, SoC, and off‑grid power now use measurement state classification for more accurate telemetry and history.
    • Per‑cell voltages, per‑cell temperatures, and aggregate cell voltage metrics (min/max/avg/diff) now expose measurement classification and improved metadata for clearer display and recording.

Battery percentage, voltage, and temperature sensors were missing
state_class: measurement, preventing Home Assistant from recording
long-term statistics and excluding them from the Energy Dashboard.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 4, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 38ade9f4-79b6-4c6a-a0ae-9de2a31f935e

📥 Commits

Reviewing files that changed from the base of the PR and between e411523 and c2ec0d2.

📒 Files selected for processing (1)
  • src/device/venus.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/device/venus.ts

Walkthrough

This PR adds state_class: 'measurement' metadata to Home Assistant sensor configurations in B2500 Base, B2500 V1, and Venus device modules; Venus BMS field metadata also includes stateClass for conditional advertising.

Changes

B2500 Base, B2500 V1, and Venus Home Assistant Sensor Definitions

Layer / File(s) Summary
B2500 Base device sensor definitions
src/device/b2500Base.ts
Battery Percentage, Battery Capacity, Temperature Min/Max, Host Battery SoC, Extra Battery SoC (1 and 2), and cell-voltage aggregate (min, max, diff, avg) and per-cell sensors now include state_class: 'measurement'.
B2500 V1 device extra battery sensor
src/device/b2500V1.ts
Extra battery input voltage sensor component now includes state_class: 'measurement'.
Venus device runtime sensors and BMS metadata
src/device/venus.ts
Runtime sensors (batteryCapacity, batterySoc, offGridPower) and BMS per-cell voltage/temperature sensors now include state_class: 'measurement'. bmsFields mapping was extended with camel-cased stateClass: 'measurement' for multiple BMS properties and advertising sets state_class from info.stateClass when present.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • tomquist/hm2mqtt#309: Both PRs modify the bmsFields mapping and BMS sensor advertising logic in src/device/venus.ts.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and accurately summarizes the main changes: adding missing state_class and device_class fields to sensor entities for Home Assistant discovery across multiple device files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@tomquist tomquist changed the base branch from main to develop June 4, 2026 16:20
Copy link
Copy Markdown
Owner

@tomquist tomquist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@tomquist tomquist merged commit c1a4577 into tomquist:develop Jun 4, 2026
13 checks passed
tomquist pushed a commit that referenced this pull request Jun 4, 2026
Completes the state_class additions from #311 for the remaining devices:

- Jupiter: BMS capacity (energy_storage) and charge voltage (voltage)
  were missing state_class: measurement.
- B2500 V2: solar input voltage/current, output voltage/current and
  host/extra battery voltage/current sensors were missing
  state_class: measurement.

Without state_class, Home Assistant does not record long-term statistics
for these sensors and the State of Charge sensors cannot be added to the
Energy Dashboard battery configuration introduced in HA 2026.6.

fixes #310
tomquist pushed a commit that referenced this pull request Jun 4, 2026
Completes the state_class additions from #311 for the remaining devices:

- Jupiter: BMS capacity (energy_storage) and charge voltage (voltage)
  were missing state_class: measurement.
- B2500 V2: solar input voltage/current, output voltage/current and
  host/extra battery voltage/current sensors were missing
  state_class: measurement.

Without state_class, Home Assistant does not record long-term statistics
for these sensors and the State of Charge sensors cannot be added to the
Energy Dashboard battery configuration introduced in HA 2026.6.

fixes #310
tomquist added a commit that referenced this pull request Jun 4, 2026
Completes the state_class additions from #311 for the remaining devices:

- Jupiter: BMS capacity (energy_storage) and charge voltage (voltage)
were missing state_class: measurement.
- B2500 V2: solar input voltage/current, output voltage/current and
host/extra battery voltage/current sensors were missing state_class:
measurement.

Without state_class, Home Assistant does not record long-term statistics
for these sensors and the State of Charge sensors cannot be added to the
Energy Dashboard battery configuration introduced in HA 2026.6.

fixes #310

Co-authored-by: Claude <noreply@anthropic.com>
@michikrug michikrug deleted the fix-sensor-state-device-classes branch June 4, 2026 17:01
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.

2 participants