flixlix / energy-flow-card-plus

An upgraded Energy Distribution Card for Home Assistant, with added features like Individual Devices and refined UI enhancements, while maintaining the Energy Dashboard's original design.
155 stars 8 forks source link

[BUG] Wrong values when total increasing sensor unavailable #128

Open mhspelt opened 4 months ago

mhspelt commented 4 months ago

Describe the bug I have two PV inverters that report the total yield in their lifetime, but one goes offline when the voltage is too low. So at midnight, this sensor is always unavailable.

image

When I add this to the energy flow card, it assumes that the missing value is 0: image

Expected behavior When the value of a total increasing sensor is missing, use the last known value instead of zero.

Alternative solution I could try to define a helper that will keep the last known value but that will only work from this moment onwards and for my several years of historical data.

Please make sure all of this applies and check the checkboxes, by writing "X" inside of them.

limburg11 commented 4 months ago

Same here, "day" view shows total energy from beginning to that day and not delta energy of that day only. My PV generator is also unavailable during night. In the native HA energy dashboard, it works correctly.

udivankin commented 2 months ago

Anything that can be done on the current project side? I've spent some time researching and it seems that no good solution exists, event defining template-sensor that preserves previous value doesn't work in all cases. https://community.home-assistant.io/t/sensor-display-last-known-value-instead-of-unavailable/200081/30 https://community.home-assistant.io/t/preserve-last-value-of-sensor-before-becoming-unavailable/469875/8

Screenshot 2024-08-24 at 15 43 50
mhspelt commented 2 months ago

Anything that can be done on the current project side? I've spent some time researching and it seems that no good solution exists, event defining template-sensor that preserves previous value doesn't work in all cases. https://community.home-assistant.io/t/sensor-display-last-known-value-instead-of-unavailable/200081/30 https://community.home-assistant.io/t/preserve-last-value-of-sensor-before-becoming-unavailable/469875/8

Screenshot 2024-08-24 at 15 43 50

@flixlix @udivankin I think I may have found a cause, but I don't really have time (or skills, at the moment, to even know how to setup a component in a way I can change the code) to test if there is a viable solution.

But looking at src/energy/index.ts line 160, the comment there states

// if the start of the first value is after the requested period, we have the first data point, and should add a zero point

I think that if we check if the sensor is total_increasing, and if so, instead of a zero copy the first retrieved data point to the start, the statistics should get calculated correctly.

So something like

if (stat.length && new Date(stat[0].start) > startMinHour) {
  if (stat.sensor_type == 'total_increasing') { // <-- add this if check
      // Modified code to add a data point with correct statistics
      stat.unshift({
        ...stat[0],
        start: startMinHour.toISOString(),
        end: startMinHour.toISOString(),
        sum: stat[0].sum, // <-- use first available data point instead of 0
        state: stat[0].state,  // <-- use first available data point instead of 0
      });
  } else {     
      // The original code of line 161 - 169 to add a zero dat point
      stat.unshift({
        ...stat[0],
        start: startMinHour.toISOString(),
        end: startMinHour.toISOString(),
        sum: 0,
        state: 0,
      });
    }
}

I hope this helps you to fix this more quickly; or at the very least as a reminder for myself if I ever have time to try and code something myself.

faspie commented 1 month ago

I can confirm this bug. Any success available? How can I define the helper as a workaround?