n0liver / HAAC

Homeassistant APSystems API client
1 stars 0 forks source link

Incorrect statistics in HA when the APSystems webservice is unavailable #2

Open Id2ndR opened 7 months ago

Id2ndR commented 7 months ago

It seems that when the APSystems webservice is unavailable or came back online, this integration start with value "0" for the sensors instead of null.

At this time HA considers 0 as new index value of the counter, as a counter that restart at 0 each month for example. But the value that follow after this is the (total increasing) sum from APSystems which is then consider as the instantaneous production by HA, and is added like that in the long term statistics.

As I use this HAAC extension since the beginning of my solar production, I can workaround this issue by setting the long term statistics directly to the value from APSystems:

-- Count incorrect statistics
select count(*) from statistics
  where metadata_id=(select id from statistics_meta where statistic_id="sensor.energy_generated_during_lifetime")
  and sum > state;
select count(*) from statistics_short_term
  where metadata_id=(select id from statistics_meta where statistic_id="sensor.energy_generated_during_lifetime")
  and sum > state;
-- Workaround if HAAC has been used since the begining
update statistics_short_term set sum=state
  where metadata_id=(select id from statistics_meta where statistic_id="sensor.energy_generated_during_lifetime")
  and sum > state;
update statistics set sum=state
  where metadata_id=(select id from statistics_meta where statistic_id="sensor.energy_generated_during_lifetime")
  and sum > state;
n0liver commented 7 months ago

Thank you for sharing the queries -- I've heard about these issues before with other energy-related plugins, sad to see my plugin isn't be safe from it either.

Since you're talking long-term statistics, can I assume that you use the energy_generated_during_lifetime sensor for your energy dashboard? If so, I would like to suggest trying the energy_generated_today sensor instead, which resets anyway at midnight. If your stats became messed up at 2 december I see the same 0-value in my log, but looking at the energy dashboard the daily value matches what I see in the APS EMA app.

Ideally I would like to fix the false 0-values, but that'll require a lot of effort I'm afraid.

Id2ndR commented 7 months ago

Thank you for your suggestion that may effectively mitigate this issue. I'll change my setup. Your guess was right about the 2 December. The APS API seems to be unavailable about every two month.