Performing cubic spline interpolation

I created a custom integration, which performs cubic spline interpolation between a set of data points. It takes a numeric entity as input (x value) and outputs the interpolated value (y value) based on a discrete set of (x, y) pairs. The integration works very similar to Home Assistant’s built-in Compensation integration, but instead of using polynomial regression/fitting (polyfit from numpy), this integration uses cubic spline interpolation (CubicSpline from scipy), which can be superior in some cases because

  • interpolation exactly matches the input data, while fitting generally does not
  • polynomial fitting with higher degree polynomials can sometimes suffer from large under- and overshoots leading to significant errors, while low degree polynomials often do not capture nonlinear behavior well
  • cubic splines generally extrapolate more predictably than high-degree polynomials

Common use cases include computing the state of charge (SOC) value of a battery based on the voltage or correcting/compensating measured values. You can find some example configurations in the repository.

I did not (yet) submit the integration to HACS, but you can test it as custom repository. The repository with more details can be found here:

I am happy for any questions or feedback. So if you have any suggestions, issues, or requests feel free to write them here or open an issue or PR in the repository.

Finally, I want to make clear that large parts of the integration have been written by and together with GitHub Copilot, but I went through the code myself and tested it. I do not extend any warranty though.

2 Likes

Oh wow, I’m just trying to find a solution for my problem here:

So can it be used to calculate the approximate time left on a battery?

Thanks for asking! As I understand, a simple template sensor would already be enough in your case and this integration is not really suitable for your use case. I answered in the other thread with how I would approach this. Hope this helps.