custom-components / sensor.owlintuition

A set of sensors to integrate the OWL Intuition devices network
MIT License
11 stars 9 forks source link

Calculated Grid Energy incorrect around midnight as values reset at different times #20

Closed shortbloke closed 3 years ago

shortbloke commented 3 years ago

Using the template sensor can still have issues. The issue seen here is caused when solar generated reset to zero at 00:00:06 and electricity reset at 00:02:05. This gap causes the amount total for the day to jump by the amount of solar that had been generated, leading to HA believing we'd suddenly used ~9.4kwh image

So we need to handle elec and solar which resetting at different times. I'm struggling to think how to handle this in a template sensor, especially when solar doesn't send packets often (or at all) when it's dark, so I don't think I can even rely on the last update time.

Is there a way the reset can be synchronised? Or do you have any other ideas how this problem might be approached, either in the component directly or via logic I can attempt to implement with sensors in HA.

glpatcern commented 3 years ago

Interesting challenge, let me think about it. It's surprising though that solar reset right after midnight despite not having sent data, whereas electricity was off by 2 minutes, i.e. there were at least 2 packets sent past midnight with previous-day total values (did I get it right?).

shortbloke commented 3 years ago

I didn't have debug logging enabled (I do now, along with an extra log message to output the XML payload) so I can't confirm exactly what was received. Looking just at what I have shown in HA I can see the following changes, but not certain HA UI doesn't show a data point if the value is unchanged (I'm aware each sensor has a last changed and last updated property. So there could be other packets being processed, but if the value was unchanged then just not shown. Debug logs will help clarify what's going on. But for now looking at the markers on the graph in Home Assistant I see:

- Solar
  - Generating
    - 21:16:63 - 17 W
    - 21:19:06 - 0 W
    - 08:08:07 - 252W
  - Generated
    - 20:56:06 - 9.4 kWh
    - 00:00:06 - 0 kWh
    - 08:08:18 - 0.1 kWh
 - Electricity
   - Using
     - 24:59:42 - 991 W
     - 00:04:17 - 1024 W
   - Used
     - 29:59:53 - 50.6 kWh
     - 00:02:05 - 0 kWh
shortbloke commented 3 years ago

Thinking about this more... I wonder if changing the template sensor so that it only updates the grid usage when the electricity using and used changes, which I think can be done using a trigger (not tested it yet). This might fix the issue if the grid template sensor was being updated when the solar reset occurred.

shortbloke commented 3 years ago

I've updated my PR with an updated sensor template which I think protects against the problem above. Though needs some time (multiple days to allow resets to occur) to test more fully.

shortbloke commented 3 years ago

Ok Looks to be working well now with my latest update to the PR #19. So closing this issue.

glpatcern commented 3 years ago

Excellent, will merge the PR then, thanks!