Difference between two hours min values

I’ve been playing with grafana for a few days and I am facing a dilemma :smiley:
How could I make the difference between the minimum of the next hour and the minimum of the previous one?

I’ve a sensor counter (Energy), and till now i’m calculating kwh with this query:

SELECT (last(value)-first(value)) FROM Usage WHERE $timeFilter GROUP BY time(1h)

Generally it seems to be fine but sometimes there are cases like these:

As you can see, there is a difference, lost, between 19:41:38 (last value) and 20:00:34 (first value next hour).
So, I suppose, the best calculation should be between the first value of the hour and the first value of the next hour.

Is it possible? How?

Thanks a lot!

Use the InfluxDB subqueries. Inner query will calculate hourly mins and outer query will calculate their diffs.

But I would use more generic approach: calculate derivation (because you have counter) and then do a hourly sums (again task for subquery). Derivation of counter values is frequently asked question (especially for power measurements) also on this forum - just use search functionality for more discussions about this topic.

Hi @jangaraj,
thank you for your reply.
I’ve searched around in the forum but i’ve not found any example that’s right for me.
Could you provide me an example of query/subquery for my purpose?
I would appreciate it very much.
Thanks in advance!

I think i’ve found the right way…

select difference(last) from (
select last(value) from Usage group by time(1h) tz('Europe/Rome'))

gives me reasonable results.

Thank you again for the tip :slight_smile: