I am graphing this time series. The upward and downward spikes represent the start and stop of a machine. Is there any way to compute the run time from this series? I’ve looked at the stateduration function but that seems to assume a constant threshold. In this case the time starts when the value goes above +1 and stops when the value goes below -1.
Using a fixed threshold over-estimates burn time because it counts a portion of the time when the burner has switched off but the unit is still cooling back down.
So what I’m after is a way to measure the time between when the value ticks up sharply and when it ticks down again rather than time above a fixed threshold. This probably doesnt exist but I thought I would ask.
I think you would have to define a custom rate-of-change (i.e. derivative) threshold and use a map function. Something like this (this has not been tested) to get the point in time where you have spiked up. Then use that value in another function to calculate the time between the two events.
import "math"
// Define your threshold value
threshold = 5.0
// Query your time series data
from(bucket: "your-bucket")
|> range(start: -1h) // Adjust the time range as needed
|> filter(fn: (r) => r["_measurement"] == "your-measurement" and r["_field"] == "your-field")
|> derivative(unit: 1s, nonNegative: true, columns: ["_value"]) // Calculate the derivative
|> map(fn: (r) => ({
r with
exceedsThreshold: math.abs(r._value) > threshold,
}))
|> filter(fn: (r) => r.exceedsThreshold == true)
|> yield(name: "exceeds_threshold")
Thanks for all the suggestions but since the data was already being processed by Node-Red before it arrived at grafana I’ve made a simple state machine node that gives me a nice clean signal without too much effort