Monitor states

Flux helps you monitor states in your metrics and events:

If you’re just getting started with Flux queries, check out the following:

Find how long a state persists

  1. Use the stateDuration() function to calculate how long a column value has remained the same value (or state). Include the following information:

    • Column to search: any tag key, tag value, field key, field value, or measurement.
    • Value: the value (or state) to search for in the specified column.
    • State duration column: a new column to store the state duration─the length of time that the specified value persists.
    • Unit: the unit of time (1s (by default), 1m, 1h) used to increment the state duration.

      1. |> stateDuration(
      2. fn: (r) => r._column_to_search == "value_to_search_for",
      3. column: "state_duration",
      4. unit: 1s
      5. )
  2. Use stateDuration() to search each point for the specified value:

    • For the first point that evaluates true, the state duration is set to 0. For each consecutive point that evaluates true, the state duration increases by the time interval between each consecutive point (in specified units).
    • If the state is false, the state duration is reset to -1.

Example query with stateDuration()

The following query searches the doors bucket over the past 5 minutes to find how many seconds a door has been closed.

  1. from(bucket: "doors")
  2. |> range(start: -5m)
  3. |> stateDuration(
  4. fn: (r) =>
  5. r._value == "closed",
  6. column: "door_closed",
  7. unit: 1s)

In this example, door_closed is the State duration column. If you write data to the doors bucket every minute, the state duration increases by 60s for each consecutive point where _value is closed. If _value is not closed, the state duration is reset to 0.

Query results

Results for the example query above may look like this (for simplicity, we’ve omitted the measurement, tag, and field columns):

  1. _time _value door_closed
  2. 2019-10-26T17:39:16Z closed 0
  3. 2019-10-26T17:40:16Z closed 60
  4. 2019-10-26T17:41:16Z closed 120
  5. 2019-10-26T17:42:16Z open -1
  6. 2019-10-26T17:43:16Z closed 0
  7. 2019-10-26T17:44:27Z closed 60

Count the number of consecutive states

  1. Use the stateCount() function and include the following information:

    • Column to search: any tag key, tag value, field key, field value, or measurement.
    • Value: to search for in the specified column.
    • State count column: a new column to store the state count─the number of consecutive records in which the specified value exists.

      1. |> stateCount(
      2. fn: (r) => r._column_to_search == "value_to_search_for",
      3. column: "state_count"
      4. )
  2. Use stateCount() to search each point for the specified value:

    • For the first point that evaluates true, the state count is set to 1. For each consecutive point that evaluates true, the state count increases by 1.
    • If the state is false, the state count is reset to -1.

Example query with stateCount()

The following query searches the doors bucket over the past 5 minutes and calculates how many points have closed as their _value.

  1. from(bucket: "doors")
  2. |> range(start: -5m)
  3. |> stateDuration(
  4. fn: (r) => r._value == "closed",
  5. column: "door_closed"
  6. )

This example stores the state count in the door_closed column. If you write data to the doors bucket every minute, the state count increases by 1 for each consecutive point where _value is closed. If _value is not closed, the state count is reset to -1.

Query results

Results for the example query above may look like this (for simplicity, we’ve omitted the measurement, tag, and field columns):

  1. _time _value door_closed
  2. 2019-10-26T17:39:16Z closed 1
  3. 2019-10-26T17:40:16Z closed 2
  4. 2019-10-26T17:41:16Z closed 3
  5. 2019-10-26T17:42:16Z open -1
  6. 2019-10-26T17:43:16Z closed 1
  7. 2019-10-26T17:44:27Z closed 2

Example query to count machine state

The following query checks the machine state every minute (idle, assigned, or busy). InfluxDB searches the servers bucket over the past hour and counts records with a machine state of idle, assigned or busy.

  1. from(bucket: "servers")
  2. |> range(start: -1h)
  3. |> filter(fn: (r) =>
  4. r.machine_state == "idle" or
  5. r.machine_state == "assigned" or
  6. r.machine_state == "busy"
  7. )
  8. |> stateCount(fn: (r) => r.machine_state == "busy", column: "_count")
  9. |> stateCount(fn: (r) => r.machine_state == "assigned", column: "_count")
  10. |> stateCount(fn: (r) => r.machine_state == "idle", column: "_count")

Related articles

states monitor flux