Skip to content

fix(sensor): floor e_consumption_today to 1 dp to prevent TOTAL_INCREASING dips#144

Merged
dewet22-claude merged 1 commit into
mainfrom
fix/consumption-today-floor-precision
Jun 8, 2026
Merged

fix(sensor): floor e_consumption_today to 1 dp to prevent TOTAL_INCREASING dips#144
dewet22-claude merged 1 commit into
mainfrom
fix/consumption-today-floor-precision

Conversation

@dewet22

@dewet22 dewet22 commented Jun 8, 2026

Copy link
Copy Markdown
Owner

What

Floors e_consumption_today to one decimal place in the value_fn and adds suggested_display_precision=1 to match peer daily-energy sensors. Keeps (restores) SensorStateClass.TOTAL_INCREASING.

Why

e_consumption_today is a computed field — givenergy-modbus derives it as PV gen + grid-in - grid-out - AC-charge from several registers polled at slightly different times. When one component updates before the others, the result can transiently dip by a few Wh (e.g. 4.7 → 4.6 kWh), tripping HA's strictly-increasing guard (#142).

Flooring to 1 dp makes sub-0.1 kWh fluctuations invisible. Keeping TOTAL_INCREASING is important: switching to TOTAL (as in the now-reverted #143) would silently corrupt the accumulated sum on the midnight reset to zero, because TOTAL requires an explicit last_reset attribute to distinguish a reset from a decrease.

Display precision of 1 dp matches e_grid_in_day, e_grid_out_day, and e_pv_day.

Fixes #142

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Updated energy consumption sensor to consistently display values with 1 decimal place precision
    • Improved handling of missing data in energy consumption reporting
    • Refined sensor behavior classification for more accurate consumption tracking

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@dewet22, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 32 minutes and 58 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a19ca0ec-86fe-46e9-8a60-c31147ab1a8e

📥 Commits

Reviewing files that changed from the base of the PR and between f834e56 and 854249c.

📒 Files selected for processing (1)
  • custom_components/givenergy_local/sensor.py
📝 Walkthrough

Walkthrough

Updated the e_consumption_today inverter sensor to quantize values to 1 decimal place and use the TOTAL_INCREASING state class. Added math import and modified the sensor's value function to return None when missing and floor values to prevent floating-point precision artifacts causing false state decreases.

Changes

Energy consumption sensor fix

Layer / File(s) Summary
Fix e_consumption_today quantization and state class
custom_components/givenergy_local/sensor.py
Added math import and updated the e_consumption_today sensor to quantize values to 1 decimal place using floor(v * 10) / 10, return None when the attribute is missing, switch state_class to TOTAL_INCREASING, and set suggested_display_precision=1 to align display with the quantized values.

🎯 2 (Simple) | ⏱️ ~8 minutes

A sensor's state now flows without a hitch,
Quantized to one decimal—no floating-point glitch!
math.floor brings precision so true,
The inverter's consumption now sums just right too. 🐰⚡

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main fix: flooring e_consumption_today to 1 decimal place to prevent TOTAL_INCREASING state violations.
Linked Issues check ✅ Passed The PR directly addresses issue #142 by implementing flooring logic to prevent state decreases that violate TOTAL_INCREASING semantics.
Out of Scope Changes check ✅ Passed All changes are focused on resolving the e_consumption_today sensor flooring and precision alignment, with no unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/consumption-today-floor-precision

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.

@dewet22-codex

Copy link
Copy Markdown
Collaborator

I’ve started reviewing this PR.

Comment thread custom_components/givenergy_local/sensor.py Outdated

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the "e_consumption_today" sensor to use "SensorStateClass.TOTAL_INCREASING" and floors its value to one decimal place to prevent transient dips from triggering Home Assistant's strictly-increasing guard. The reviewer pointed out a potential floating-point precision issue where "math.floor(v * 10) / 10" can result in an incorrect lower value (e.g., 5.7 becoming 5.6), and suggested rounding the value to a high precision before flooring. Additionally, the reviewer recommended avoiding defensive "getattr" checks for attributes guaranteed to exist.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread custom_components/givenergy_local/sensor.py Outdated
@dewet22 dewet22 force-pushed the fix/consumption-today-floor-precision branch from f834e56 to a05a4e9 Compare June 8, 2026 18:52
@dewet22-codex

Copy link
Copy Markdown
Collaborator

I’ve started re-reviewing this PR.

Comment thread custom_components/givenergy_local/sensor.py Outdated
@dewet22 dewet22 force-pushed the fix/consumption-today-floor-precision branch from a05a4e9 to 919a862 Compare June 8, 2026 19:01
@dewet22-codex

Copy link
Copy Markdown
Collaborator

I’ve started re-reviewing this PR.

Comment thread custom_components/givenergy_local/sensor.py
@dewet22 dewet22 force-pushed the fix/consumption-today-floor-precision branch from 919a862 to a0bd6a6 Compare June 8, 2026 19:06
@dewet22-codex

Copy link
Copy Markdown
Collaborator

I’ve started re-reviewing this PR.

Comment thread custom_components/givenergy_local/sensor.py
…, and reset-threshold

The sensor is computed from several registers (PV gen + grid-in - grid-out -
AC-charge) polled at slightly different times. When one component updates
before the others the result can transiently dip by a few Wh, tripping
HA's strictly-increasing guard (#142).

Adds a `monotonic: bool` flag to `GivEnergyInverterSensorDescription` and
tracks `_monotonic_max` and `_monotonic_date` on the entity. When set,
`native_value` applies three layers of logic:

1. Day boundary: when HA's local date changes, reset both fields so the
   midnight drop to zero passes through as a real decrease and
   TOTAL_INCREASING starts a new recorder cycle.

2. Large-drop reset (clock-drift guard): if the value drops by more than
   _MONOTONIC_RESET_THRESHOLD (0.5 kWh) on the same HA date, treat it as a
   genuine source reset. This handles inverter clocks that lag HA's midnight
   by one scan interval — the actual counter reset arrives on a subsequent
   read after the day boundary has already been committed.

3. Intra-day clamp: otherwise, return max(new, session_max) to hide the
   transient few-Wh dips that triggered the original warning.

`GivEnergyInverterSensor` also inherits `RestoreEntity` and seeds
`_monotonic_max` from the last persisted state in `async_added_to_hass`
(only when the persisted state is from today) so a transient-dip first
reading after an HA restart does not undercut the recorder's prior value.

Keeping `TOTAL_INCREASING` is important: switching to `TOTAL` (as in the
reverted #143) silently corrupts the accumulated `sum` on the midnight reset
to zero, because `TOTAL` requires an explicit `last_reset` attribute to
distinguish a reset from a decrease.

Fixes #142

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@dewet22 dewet22 force-pushed the fix/consumption-today-floor-precision branch from a0bd6a6 to 854249c Compare June 8, 2026 19:13
@dewet22-codex

Copy link
Copy Markdown
Collaborator

I’ve started re-reviewing this PR.

@dewet22-codex dewet22-codex left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The delayed inverter-clock reset concern is addressed by the large-drop guard, and I have no further concerns with this revision.

@dewet22-claude dewet22-claude merged commit f9b089f into main Jun 8, 2026
12 checks passed
@dewet22-claude dewet22-claude deleted the fix/consumption-today-floor-precision branch June 8, 2026 19:16
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.

Has state class total_increasing, but its state is not strictly increasing

3 participants