# Sum of maxima (or absolute difference for incremental measurement)

Hello there,
A measurement represents the increasing amount of energy “consumed” by an electric car:

``````from(bucket: "home_assistant")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["entity_id"] == "evcc_charged_energy_self_kwh")
|> filter(fn: (r) => r["_field"] == "value")
|> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
|> yield(name: "last")
``````

The measurement of consumed energy amount increases over time but is reset to 0 from time to time (on system restart).

How do I best calculate the total amount of energy consumed between two datetimes (e.g. yesterday or full last year)?

Thanks!

Writing my question here helped me to understand the question better myself. This does the job perfectly:

``````from(bucket: "home_assistant_awf1877")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["entity_id"] == "evcc_charged_energy_self_kwh")
|> filter(fn: (r) => r["_field"] == "value")
|> difference(nonNegative: true)
|> sum()
``````
1 Like