Documentation

stateDuration() function

stateDuration() returns the cumulative duration of a given state.

The state is defined by the fn predicate function. For each consecutive record that evaluates to true, the state duration is incremented by the duration of time between records using the specified unit. When a record evaluates to false, the value is set to -1 and the state duration is reset. If the record generates an error during evaluation, the point is discarded, and does not affect the state duration.

The state duration is added as an additional column to each record. The duration is represented as an integer in the units specified.

As the first point in the given state has no previous point, its state duration will be 0.

stateDuration(fn: (r) => r._measurement == "state", column: "stateDuration", unit: 1s)

If the expression generates an error during evaluation, the point is discarded, and does not affect the state duration.

Parameters

Make sure fn parameter names match each specified parameter. To learn why, see Match parameter names.

fn

(Required) A single argument function that evaluates true or false to identify the state of the record. Records are passed to the function. Those that evaluate to true increment the state duration. Those that evaluate to false reset the state duration.

column

Name of the column added to each record that contains the state duration. Default is stateDuration.

unit

Unit of time to increment state duration with. For example: 1s, 1m, 1h, etc. Default is one second (1s).

tables

Input data. Default is piped-forward data (<-).

Examples

The following example uses data provided by the sampledata package to show how stateDuration() transforms data.

import "sampledata"

sampledata.int()
    |> stateDuration(fn: (r) => r._value > 10)

View input and output


Was this page helpful?

Thank you for your feedback!


Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.