Hello @SBNV,
Welcome!
Are you using OSS or Cloud?
Do you want to create an alert task through the UI or your own custom one?
You’re right you can use difference as you suggested. The following worked for me:
from(bucket: "noaa")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "average_temperature")
|> filter(fn: (r) => r["_field"] == "degrees")
|> filter(fn: (r) => r["location"] == "coyote_creek")
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> difference(nonNegative: false, columns: ["_value"])
A complete alert task would look something like:
option task = {
name: "water alert",
every: 10s,
offset: 2s,
}
import "slack"
alert = (level, type, eventValue) => {
slack.message(
url: "https://hooks.slack.com/services/xxx/xxx",
text: "An alert \"${string(v: type)}\" event has occurred! The number of field values= \"${string(v: eventValue)}\".",
color: "warning",
channel: "#Anais"
)
return level
}
from(bucket: "noaa")
|> range(start: -task.every)
|> filter(fn: (r) => r["_measurement"] == "average_temperature")
|> filter(fn: (r) => r["_field"] == "degrees")
|> filter(fn: (r) => r["location"] == "coyote_creek")
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> difference(nonNegative: false, columns: ["_value"])
// I include a limit here for testing only to make sure that I don't spam my slack and that I only have a couple of values that exceed my thresholds defined in the filter() function below.
|> limit(n: 10)
|> filter(fn: (r) => r._value < -3.0 or r._value > 3.0)
|> map(
fn: (r) => ({r with level: alert(level: 1, type: "warn", eventValue: r._value) }))
You could also take out the filter function, and perform conditional mapping and assign a level for okay and bad saturation levels. Then you could use the to() function to write that level data back into influxdb so you have more information about your system and the check you created. Let me know if you want an example of that as well.
You can test out this task by executing it in the data explorer as well. And also including multiple yield() functions in between lines. I think of yield() functions like print statements.