Skip to content

Respect gas entity display precision in energy dashboard - #53712

Open
martin-g-it wants to merge 24 commits into
home-assistant:devfrom
martin-g-it:patch-3
Open

Respect gas entity display precision in energy dashboard#53712
martin-g-it wants to merge 24 commits into
home-assistant:devfrom
martin-g-it:patch-3

Conversation

@martin-g-it

@martin-g-it martin-g-it commented Aug 19, 2026

Copy link
Copy Markdown

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 display 0.01 m³ instead of 0.009 m³
  • 0.015 m³ — the Energy dashboard could display 0.02 m³ instead of 0.015 m³

This change applies the configured gas entity's display precision to:

  • Energy distribution gas usage
  • Individual gas source rows
  • Gas total rows
  • Gas graph total chip
  • Gas graph total tooltip
  • Gas graph y-axis

The number of decimal places is not hardcoded. The Energy dashboard reads the display_precision from 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³.

image image

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • I have followed the perfect PR recommendations
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

To help with the load of incoming pull requests:

@home-assistant home-assistant 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.

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!

@home-assistant

Copy link
Copy Markdown

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant
home-assistant Bot marked this pull request as draft August 19, 2026 15:45
@github-actions github-actions Bot added the Needs Template PR description does not follow the pull request template label Aug 19, 2026
@github-actions

This comment has been minimized.

@martin-g-it
martin-g-it marked this pull request as ready for review August 19, 2026 16:51
@github-actions github-actions Bot removed the Needs Template PR description does not follow the pull request template label Aug 19, 2026
@karwosts

Copy link
Copy Markdown
Member

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.

@martin-g-it

Copy link
Copy Markdown
Author

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..

@martin-g-it
martin-g-it force-pushed the patch-3 branch 2 times, most recently from 8fede84 to 4334b24 Compare August 20, 2026 08:09
Comment thread src/panels/lovelace/cards/energy/hui-energy-distribution-card.ts Outdated
@home-assistant
home-assistant Bot marked this pull request as draft August 21, 2026 05:29
@martin-g-it
martin-g-it requested a review from MindFreeze August 21, 2026 06:04
@martin-g-it
martin-g-it marked this pull request as ready for review August 21, 2026 06:04
@MindFreeze
MindFreeze requested a balanced review from Copilot August 24, 2026 06:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_precision update changes hass.entities, but this card's shouldUpdate() filters out entity-only hass changes 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.

Comment thread src/panels/lovelace/cards/energy/hui-energy-gas-graph-card.ts
Comment thread src/panels/lovelace/cards/energy/hui-energy-distribution-card.ts
Comment on lines +344 to +349
const gasDisplayPrecisions = types.gas
?.map(
(source) =>
this.hass.entities[source.stat_energy_from]?.display_precision
)
.filter((precision): precision is number => precision !== undefined);
Comment thread src/panels/lovelace/cards/energy/hui-energy-distribution-card.ts
@home-assistant
home-assistant Bot marked this pull request as draft August 24, 2026 06:18
Comment on lines +166 to +174
?.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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@martin-g-it

Copy link
Copy Markdown
Author

@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 display_precision is that the Energy Dashboard may display the statistic in a different unit. For example, a source can be recorded/displayed in GJ while the Energy Dashboard shows the converted value in kWh. In that case, the configured precision for GJ does not really tell us what precision the user wants for the resulting kWh value.

Would it make sense instead to make display precision an optional setting in the Energy Dashboard configuration itself?

Something like:

  • Automatic — preserve the current behaviour
  • 0, 1, 2, ... decimal places — explicitly override the precision used for the final displayed value

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.
@MindFreeze

Copy link
Copy Markdown
Member

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.
For example, if you used 15000 KWh, you don't need to see 15 000.17234. If you have <10 KWh however, we can show 2 digits like 1.12

@martin-g-it

Copy link
Copy Markdown
Author

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. For example, if you used 15000 KWh, you don't need to see 15 000.17234. If you have <10 KWh however, we can show 2 digits like 1.12

and thats the exact issue; with usages like 0,016 the value is shown as 0,02..

As-is:
image

To-be:
image

@karwosts

Copy link
Copy Markdown
Member

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.

@martin-g-it

Copy link
Copy Markdown
Author

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.

@MindFreeze

Copy link
Copy Markdown
Member

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.

@maartenla

Copy link
Copy Markdown
Contributor

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.
It needs to make sense to the user why the precision is different. If we scale, we decide the precision. Otherwise we use the source precision.

Maybe also abstract it a bit higher so it also works for the water meter.

@martin-g-it

Copy link
Copy Markdown
Author

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.
Converted or automatically scaled values continue to use the existing formatter precision.
The energy distribution card, sources table, and gas graph now update when relevant gas entity precision or unit settings change.
The gas graph y-axis now also respects the configured display precision.

@martin-g-it
martin-g-it marked this pull request as ready for review August 26, 2026 19:01
@home-assistant
home-assistant Bot requested a review from MindFreeze August 26, 2026 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants