How to get total usage over time

Hey all

I have a power meter (openenergymonitor) that I’ve made and that I feed the data into influxdb. Currently it only injects 4 samples per second.
Overall, I get good results. Graphs look pretty good. However, I would like to get “Active usage over time” with Grafana.
Example: Run a heater for 20 mins and it uses 200W. It’s off for 10 mins, and then runs again for 10 mins, and then off. So for the duration of an hour, it was on for 30 mins. Calculation for Watt-Hours is Watts * Time. So in this case, it would of been 100Wh of usage.
This seems simple, and easy to do. But when there are multiple devices running at various different times, and on different power legs (I have 2 hot lines, each 120V), it can get pretty nasty on the results.
I have tried various different queries to get the data, but it doesn’t always work. Some times I can get it to return correct results when $timefilter is 1h, but when I go to 3h, it no longer works.

Here is my current query:
select SUM( “M1”) from (SELECT mean(“watt1”) AS M1 FROM “powermon” WHERE $timeFilter group by time(1h) )

In the text panel graph, it’ll show me 2kWh. However the average in the line graph will be 797Wh. So something isn’t right. At least for 1 hour, the average would work as being valid. For 2 or more hours, it would be the sum of the number of hours that is averaged (mean(?)) group by 1h.

Thanks

Just a visual example, here’s at 1h timeFilter

And here’s at 3h

And at 24h

No hints, suggestions??