TL;DR InfluxDB Tech Tips How to Monitor States with InfluxDB
By
Anais Dotis-Georgiou /
Product, Use Cases, Developer
Aug 07, 2020
Navigate to:
In this post, we learn about monitoring states with InfluxDB. This TL;DR assumes that you already know how to create a check. If you are new to checks, please read this “TL;DR – Using Tasks and Checks for Monitoring with InfluxDB”.
Q: What ways can I monitor states with InfluxDB? A: There are several ways to monitor states with InfluxDB. Some Flux functions for monitoring states include:
- stateDuration(): allows you to determine how long a state persists.
- stateCount(): allows you to count the number of consecutive states.
- monitor.stateChanges(): allows you to detect changes in the
_level
column from one level to another specific level. - monitor.stateChangesOnly(): allows you to detect all changes in the
_level
column from any level to any other level.
Q: Can you provide an example of how I might use monitor.stateChanges()? A: Let’s use monitor.stateChanges() to calculate the average charging cycle of a solar battery like in the previous TL;DR. Before using monitor.StateChages, you typically create a threshold check first. In this example we create custom status with conditional logic within a task:
from(bucket: "solar")
|> range(start: -task.every)
|> filter(fn: (r) => r["_measurement"] == "battery")
|> filter(fn: (r) => r["_field"] == "kWh")
|> derivative(unit: 3s, nonNegative: false, columns: ["_value"], timeColumn: "_time")
|> map(fn: (r) => ({
r with
_level:
if r._value > 0.0 then "CH"
else "DH"
}))
Now you want to determine when a charging cycle occurs. For example, you want to monitor when the _level
changes from CH
to DH
.
You can do so with:
|> monitor.stateChanges(
fromLevel: "CH",
toLevel: "DH"
)
This function will return a table with all of the times that the _level
first changed to DH
. Now apply the elapsed() function and mean() function to find the average charging cycle time.
Q: What if I’ve used a task to assign custom statuses, and I want to apply the monitor package functions to other columns other than the _level
column?
A: Unfortunately, this feature doesn’t exist. However, you can use the rename() function to rename your column to _level
.
Note: If this approach is cumbersome, and this type of feature enhancement is important to you, please comment on this issue #3075.