Search code examples
influxdb

InfluxDB integral gives high value after missing data


I am storing Amps, Volts and Watts in influxdb in a measurement/table called "Power". The frequency of update is approx every second. I can use the integral function to get power usage (in Amp Hour or Watt Hour) on an hourly basis. This is working very nicely, so I can get a graph of power used each hour over a 24 hour period. My SQL is below.

The issue is if there is a gap in the data then I get a huge spike in the result when it returns. eg if data was missing from 3pm to 5.45 pm, then the 5 pm result shows a huge spike. Reason I can see is there is close to 3 hours gap, so it just calculates the area under the graph and lumps it into the 5 PM value. Can I avoid that?

SELECT INTEGRAL(Watts) FROM Power WHERE time > now() - 24h GROUP BY time(1h)


Solution

  • I had a similar issue with Influx. It turns out that integral() doesn't support fill() as noted by Yuri Lachin in the comments.

    Since you're grouping by hours anyway, then the average value of the power (watts) for the hour happens to be equal to the energy consumption for the hour (watt-hours), so you can use the mean() value here and you should get the correct result.

    The query I'm using is:

    SELECT mean("load_power") AS "load"
    FROM "power_readings"
    WHERE $timeFilter
    GROUP BY time(1h) fill(0)
    

    For daily numbers I can go back to using integral() because I rarely have gaps of data that span multiple days, so there's no filler needed.

    Since you can use the fill() function in this query, you can decide what makes the most sense of the various options (see https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/#group-by-time-intervals-and-fill)