Respect gas entity display precision in energy dashboard - #53712
Respect gas entity display precision in energy dashboard#53712martin-g-it wants to merge 24 commits into
Conversation
There was a problem hiding this comment.
Hi @martin-g-it
It seems you haven't yet signed a CLA. Please do so here.
Once you do that we will be able to review and accept this pull request.
Thanks!
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
This comment has been minimized.
This comment has been minimized.
|
I forget if the unit of display is always the same display unit as the entity registry for item 0 in the gas, the precision kind of has to match the unit that you're using. Also I'm not sure if item 0 should be special cased. |
@karwosts Thanks, good catch. I’ve removed the source-0 special case and now use the highest configured display precision across all gas sources for aggregate values. Individual source rows still use their own entity precision. Fully accounting for unit conversions performed by Core would add quite a bit of complexity, and would only really matter when multiple gas sources use different display units. This approach keeps the logic simple while handling the common case correctly.. |
8fede84 to
4334b24
Compare
There was a problem hiding this comment.
Pull request overview
Updates gas consumption formatting to respect entity display precision.
Changes:
- Applies entity precision to gas source and total rows.
- Formats distribution and graph totals using gas precision.
- Uses the highest precision for aggregated sources.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
hui-energy-sources-table-card.ts |
Formats gas rows and totals. |
hui-energy-gas-graph-card.ts |
Formats graph total chip and tooltip. |
hui-energy-distribution-card.ts |
Formats distribution gas usage. |
Suppressed comments (1)
src/panels/lovelace/cards/energy/hui-energy-gas-graph-card.ts:224
- This precision is refreshed only when the energy collection emits. An entity-registry
display_precisionupdate changeshass.entities, but this card'sshouldUpdate()filters out entity-onlyhasschanges and_getStatistics()is not rerun, leaving both the chip and tooltip at the old precision until a later statistics refresh. Please include the configured gas entities' registry entries in invalidation and derive/refresh the precision when they change.
const gasDisplayPrecisions = gasSources
?.map(
(source) =>
this.hass.entities[source.stat_energy_from]?.display_precision
)
.filter((precision): precision is number => precision !== undefined);
this._displayPrecision = gasDisplayPrecisions?.length
? Math.max(...gasDisplayPrecisions)
: undefined;
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| const gasDisplayPrecisions = types.gas | ||
| ?.map( | ||
| (source) => | ||
| this.hass.entities[source.stat_energy_from]?.display_precision | ||
| ) | ||
| .filter((precision): precision is number => precision !== undefined); |
| ?.map( | ||
| (source) => | ||
| this.hass.entities[source.stat_energy_from]?.display_precision | ||
| ) | ||
| .filter((precision): precision is number => precision !== undefined); | ||
|
|
||
| const gasDisplayPrecision = gasDisplayPrecisions?.length | ||
| ? Math.max(...gasDisplayPrecisions) | ||
| : undefined; |
There was a problem hiding this comment.
We should only take the precision of entities that match the final gasUnit. If the entity is converted, it's configured display_precision is irrelevant.
|
@MindFreeze Before I rework the implementation, I wanted to check whether a slightly different approach would make more sense. The main difficulty with using the entity's Would it make sense instead to make display precision an optional setting in the Energy Dashboard configuration itself? Something like:
That would make the precision apply to the value after any unit conversion, so it is unambiguous and does not require deriving a target-unit precision from the source entity's configuration. It would also avoid questions around which source's precision should win when multiple gas sources are aggregated. Would you consider that a better direction for this? |
The gas graph previously formatted the total gas consumption using the generic number formatter, which could ignore the display precision configured on the selected gas entity. Use the configured gas entity's display precision for both the visible total chip and the total consumption tooltip. This keeps the gas graph consistent with the entity configuration. For example, a gas entity configured with 3 decimal places will display 0.009 m³ instead of being rounded to 0.01 m³.
The energy distribution card previously used the display precision of the first configured gas source for the aggregated gas value. Use the highest configured display precision across all gas sources instead of special-casing the first source. This avoids making source 0 authoritative for an aggregate value and preserves the greatest configured precision when multiple gas sources are present.
The gas graph previously used the display precision of the first configured gas source for its aggregated total. Use the highest configured display precision across all gas sources instead of special-casing source 0. This keeps the graph total and tooltip consistent with the greatest configured precision when multiple gas sources are present.
Replace dp with display_precision when reading precision from hass.entities, matching the EntityRegistryDisplayEntry type used by the frontend.
Replace dp with display_precision when reading precision from hass.entities, matching the EntityRegistryDisplayEntry type used by the frontend.
Replace dp with display_precision when reading precision from hass.entities, matching the EntityRegistryDisplayEntry type used by the frontend.
Replace dp with display_precision when reading precision from hass.entities, matching the EntityRegistryDisplayEntry type used by the frontend.
Replace dp with display_precision when reading precision from hass.entities, matching the EntityRegistryDisplayEntry type used by the frontend.
Replace dp with display_precision when reading precision from hass.entities, matching the EntityRegistryDisplayEntry type used by the frontend.
|
I don't see the need for an option for this tbh. We should derive the precision from the value like the distribution card already does. Just use values that make sense and fit on the card. |
and thats the exact issue; with usages like 0,016 the value is shown as 0,02.. |
The difference here is something like a tenth of a penny of gas. Why do you feel like you need that level of precision. |
The current unit price is €40.97/GJ, so the rounding can have a meaningful impact. For example, rounding 0.005 GJ to 0.01 GJ represents a cost difference of approximately €0.20. |
|
Not a gas user myself but looking at the code, we just use the unit of the source entity unless they are mixed. Mixing multiple gas sources with different units seems like an edge case. So using the precision of the source entities that match the final unit should cover almost all cases. |
|
I agree. We can use the precision of the source if it's a unit we dont auto scale, so the final unit is the same as the source unit. Maybe also abstract it a bit higher so it also works for the water meter. |
Add logic to calculate gas display precision based on available sources.
Refresh the gas graph when a gas entity's display precision or unit of measurement changes, ensuring the configured precision is applied without waiting for new energy data.
Refactor gas source display precision check and update unit of measurement comparison.
|
Addressed the review feedback and made the remaining changes accordingly. Gas display precision is only applied when the source unit matches the displayed gas unit. |


Breaking change
No.
Proposed change
Respect the configured display precision of the gas consumption entity across the Energy dashboard.
Previously, several gas-related views used the generic number formatter, which could ignore the display precision configured on the selected gas entity.
For example, when the configured gas entity has a display precision of 3 and the calculated usage is:
0.009 m³— the Energy dashboard could display0.01 m³instead of0.009 m³0.015 m³— the Energy dashboard could display0.02 m³instead of0.015 m³This change applies the configured gas entity's display precision to:
The number of decimal places is not hardcoded. The Energy dashboard reads the
display_precisionfrom the entity configured as the gas consumption source and uses that precision when the source unit matches the displayed gas unit. When the value is converted or automatically scaled to a different unit, the existing formatter determines the appropriate precision.Screenshots
Before:
0.015 m³ was rounded to 0.02 m³ despite the configured gas entity having a display precision of 3.
After:
The Energy dashboard respects the configured display precision and displays 0.015 m³.
Type of change
Additional information
Checklist
If user exposed functionality or configuration variables are added/changed:
To help with the load of incoming pull requests: