Skip to content

Clamp negative untracked energy to zero in detail graph#51623

Draft
loganrosen wants to merge 2 commits into
home-assistant:devfrom
loganrosen:fix-negative-untracked-energy
Draft

Clamp negative untracked energy to zero in detail graph#51623
loganrosen wants to merge 2 commits into
home-assistant:devfrom
loganrosen:fix-negative-untracked-energy

Conversation

@loganrosen
Copy link
Copy Markdown
Contributor

@loganrosen loganrosen commented Apr 19, 2026

Proposed change

Clamp negative "untracked" energy values to zero in the devices detail graph card.

The problem

When a grid meter has lower resolution than individual device sensors (e.g., an integer-resolution smart meter reporting 0 kWh while fractional device sensors report 0.2+ kWh), the untracked calculation produces negative values: untracked = 0 - 0.2 = -0.2 kWh. These appear as confusing negative bars in the detail graph.

This affects many users — core#125362 has 35 unique commenters and 10 upvotes, with the majority finding negative untracked energy confusing and misleading.

Prior discussion

This behavior was discussed in #23893: during the ECharts migration, @MindFreeze initially clamped negatives to zero, then reverted after @karwosts (who implemented the original untracked feature in #21632) argued that negative values serve as a diagnostic signal for misconfigured sensors.

However, I believe clamping is the right choice:

  1. Consistency: The pie chart, sankey, and power-sankey cards already clamp negative untracked values (with > 0 or > 1 guards). Only the detail graph card shows negatives, creating an inconsistent user experience.

  2. The diagnostic argument is undermined in practice. Most users seeing negative values have integer-resolution meters, not misconfigured sensors. A silent negative bar with no explanation doesn't help anyone diagnose anything — a dedicated warning or notification would be far more effective.

  3. User impact. The 35+ users on core#125362 overwhelmingly find negative untracked values confusing rather than useful.

What this PR does

  • Extracts the per-timestamp untracked computation into a pure computeUntrackedConsumption() function in energy-chart-options.ts
  • Clamps each timestamp's untracked value to Math.max(0, ...)
  • Adds 8 test cases covering positive passthrough, negative clamping, zero values, mixed timestamps, missing data, extra device timestamps, empty inputs, and input immutability

The other cards use scalar (not per-timestamp) untracked values and already have their own guards, so they are not changed here.

Type of change

  • Bugfix (non-breaking change which fixes an issue)

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.

To help with the load of incoming pull requests:

Extract computeUntrackedConsumption() as a pure function in
energy-chart-options.ts and use it in the devices detail graph card.

Negative untracked values arise from meter resolution mismatches
(e.g., integer grid meter reports 0 kWh while fractional device
sensors report 0.2+ kWh). The other energy cards (pie, sankey,
power-sankey) already guard against this with scalar checks; only
the detail graph card was missing the clamp.

Fixes home-assistant/core#125362

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@MindFreeze MindFreeze added the Needs UX Items requiring a review from the Home Assistant design team label Apr 20, 2026
@MindFreeze
Copy link
Copy Markdown
Member

I am not sure about this one. Both sides make sense.
The compromise I can offer is to only round it to 0 if the untracked is between 0 and -1 kWh, but leave it as is if it is < -1 . This should handle small measurement mismatches but still highlight larger inconsistencies. Not sure it's worth the complexity though.

@mbb365 mbb365 self-assigned this Apr 20, 2026
@mbb365
Copy link
Copy Markdown
Member

mbb365 commented Apr 20, 2026

Hi @loganrosen - Thanks for this. Now for my UX two cents.

For consistency clamping to zero makes sense. But NOT fully silently (as you say.. "a dedicated warning or notification would be far more effective..." When there is a mismatch we should surface a small indicator below the graph, or a warning (maybe not a warning icon or something alarming, but something to indicate something to look at) on the untracked item. That keeps the value without needing a negative bar.

Let me know where you think works best, (some screenshots if possible!) and we'll give some design direction.

@Leatherface75
Copy link
Copy Markdown

Leatherface75 commented Apr 20, 2026

I am not sure about this one. Both sides make sense. The compromise I can offer is to only round it to 0 if the untracked is between 0 and -1 kWh, but leave it as is if it is < -1 . This should handle small measurement mismatches but still highlight larger inconsistencies. Not sure it's worth the complexity though.

My problem is that my electricity company not gives any data for the last day so it's always wrong there.
This is corrected when it gets values the next day.
Because of that it gives me negative values with same amount as all tracked devices so an option to ignore this is needed. Here it's within 1 kWh but sometimes it's more than that.

image

@MindFreeze
Copy link
Copy Markdown
Member

My problem is that my electricity company not gives any data for the last day so it's always wrong there. This is corrected when it gets values the next day.

I don't think the chart should be hiding such edge cases where the data is plain wrong. What if your main grid sensor broke? We should not hide that. So even if we change this from negative bars to any kind of warning, you'll still see something wrong because your data for the day is just wrong.
There is a common case where a small discrepancy might be normal due to sensor differences but any big discrepancy indicates a problem.

@Leatherface75
Copy link
Copy Markdown

Leatherface75 commented Apr 21, 2026

My problem is that my electricity company not gives any data for the last day so it's always wrong there. This is corrected when it gets values the next day.

I don't think the chart should be hiding such edge cases where the data is plain wrong. What if your main grid sensor broke? We should not hide that. So even if we change this from negative bars to any kind of warning, you'll still see something wrong because your data for the day is just wrong. There is a common case where a small discrepancy might be normal due to sensor differences but any big discrepancy indicates a problem.

I have no gridsensor i am getting data from Tibber addon.
It's broke because i have no data for the last day and that's why it shows negative values.
The only way to fix that is to hide this.
So if my total amount for my tracked devices is 1 kWh it will show -1 kWh untracked.
If my devices have 2 kWh in total it will show -2 kWh untracked etc.
When Tibber addon gets data from electricity company the next day that will be corrected for that day but it will always be wrong for the last day i have no griddata for.
And showinig those negative values is just wrong and annoying.
I understand that my case is not the problem for everyone that's why i say an option for this is better so you can make the choice yourself.

@karwosts
Copy link
Copy Markdown
Member

And showinig those negative values is just wrong and annoying.
I understand that my case is not the problem for everyone that's why i say an option for this is better so you can make the choice yourself.

You kind of have the option, you can go to untracked energy in the legend and turn it off, if your setup doesn't provide you enough high-fidelity data to make use of it. Then you won't see it anymore.

@Leatherface75
Copy link
Copy Markdown

Leatherface75 commented Apr 21, 2026

And showinig those negative values is just wrong and annoying.
I understand that my case is not the problem for everyone that's why i say an option for this is better so you can make the choice yourself.

You kind of have the option, you can go to untracked energy in the legend and turn it off, if your setup doesn't provide you enough high-fidelity data to make use of it. Then you won't see it anymore.

I don't want to turn it off i just don't want it to show negative values for the last day.
And i also have to turn it off everytime i show that data.
So that doesn't work good either.
How hard can it be just implement an option to not display negative values.
An option not that behaviour as default.
And if i understand this pull request correctly it will fix my problem if my power consumption is lower than 1kWh.
That 1kWh value could also be an option with own values so that works for me too.

@MindFreeze
Copy link
Copy Markdown
Member

And i also have to turn it off everytime i show that data

This is incorrect. Legend selection is remembered on the device. So you only have to do it once per device.

How hard can it be just implement an option...

HA already has an incredible number of options and we get many requests for new options every day? It is unsustainable to add an option for every little thing.
You already have options. You can hide Untracked with the legend, You can probably hide it with card_mod somehow too. You can create your own energy dashboard without this card. You can use templating to create a smart grid sensor that uses device total and only corrects with tibber data once that comes in. There are probably other options to choose from as well.

@Leatherface75
Copy link
Copy Markdown

Leatherface75 commented Apr 22, 2026

And i also have to turn it off everytime i show that data

This is incorrect. Legend selection is remembered on the device. So you only have to do it once per device.

How hard can it be just implement an option...

HA already has an incredible number of options and we get many requests for new options every day? It is unsustainable to add an option for every little thing. You already have options. You can hide Untracked with the legend, You can probably hide it with card_mod somehow too. You can create your own energy dashboard without this card. You can use templating to create a smart grid sensor that uses device total and only corrects with tibber data once that comes in. There are probably other options to choose from as well.

I don't want to hide it i just want to get rid of negative untracked consumption for the last day.
All other days it shows it correctly.
And if your solution is cardmod, creating a smart grid sensor, whatever then create that because i have no idea how to fix that. I can add a sensor that shows total of all tracked devices but then i don't have data for untracked consumption at all.

@Leatherface75
Copy link
Copy Markdown

Leatherface75 commented Apr 22, 2026

And i also have to turn it off everytime i show that data

This is incorrect. Legend selection is remembered on the device. So you only have to do it once per device.

For me it shows everytime it doesn't even work to turn it off i have to change date and then back again.
And even if it works i have to turn it on and off the whole time and that's annoying.
And if i understand this pull request correctly this will fix my problem most of time atleast.
It's not often my tracked devices is over 1 kWh. But there is probably others with same problem and higher consumption.

When tracked devices report more energy than total grid consumption
(common with integer-resolution meters), untracked energy is clamped
to zero. This adds:

- Info banner below chart explaining the adjustment
- Tooltip info line showing the actual excess per bar
- rawNegatives tracking in computeUntrackedConsumption

Note: tooltip text is hardcoded in English since formatTooltip()
lacks access to hass.localize() and the entire energy-chart-options
file has zero localization calls. This can be addressed separately.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@wendevlin wendevlin requested a review from MindFreeze May 4, 2026 12:32
@laupalombi laupalombi removed the Needs UX Items requiring a review from the Home Assistant design team label May 18, 2026
@MindFreeze
Copy link
Copy Markdown
Member

I prefer to just have the negative bars. They indicate more clearly and take less space

image

@timmo001
Copy link
Copy Markdown
Member

Marking as draft, also has conflicts to resolve

@timmo001 timmo001 marked this pull request as draft May 19, 2026 08:42
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.

Negative untracked electricity usage

7 participants