Time difference between _time and now()

In Grafana, I want to show how much time has passed since the last record.

In an SQL, it would do something between:

  • SELECT now() - _time…

How do I do something similar in Flux?

My query returning data:

from(bucket: "my-bucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_field"] == "Battery")
  |> filter(fn: (r) => r["device"] == "ZigBee")
  |> last()
  |> group()
  |> bottom(n:100, columns: ["_value"])

This code return last value;
But, I would also like to know how long time since the last data, to check if the device has run out of battery!

Thanks!

@Saimond_Schroder You can use the events.duration() function:

import "contrib/tomhollingworth/events"

from(bucket: "my-bucket")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r["_field"] == "Battery")
    |> filter(fn: (r) => r["device"] == "ZigBee")
    |> events.duration()
    |> last()

Thank you so much!

Adding this function I got the information I was looking for!