I’m wondering if this is a timezone issue. The calculation itself is ok, depends on timestamps (since it is in fact a weighted average depending on the time-difference of the stored values). As last value the current item’s state is included with the current timestamp. If this value has another timezone than the stores valued and that is not properly considered, negative averages could be calculated (because now-first<0).