Flux StateDuration not working as expected

Since moving from TickScripts in V1 to Flux v2.6 im struggling with some of the functions.
The below i want to add the critical duration to my alert notification but it just fails.
Have i missed something silly?
FYI - The below is my version of an SNMP bandwidth check and alert.

import “influxdata/influxdb/monitor”
import “influxdata/influxdb/v1”

data =
from(bucket: “telegraf”)
|> range(start: -5m)
|> filter(fn: (r) => r[“_measurement”] == “cr01ma4” or r[“_measurement”] == “xs01ma04”)
|> filter(fn: (r) => r[“ifName”] == “Ethernet1/47”)
|> filter(fn: (r) => r[“_field”] == “ifHCInOctets”)
|> derivative(unit: 1s, nonNegative: true)
|> map(fn: (r) => ({r with _value: float(v: r._value) * 8.0 / 1000000.0}))

option task = {name: “WDWM MA4 ↔ TN19”, every: 5m, offset: 0s}

check = {
_check_id: “0b2c2a6ddfab4000”,
_check_name: "“WDWM MA4 ↔ TN19”,
_type: “threshold”,
tags: {},
}
crit = (r) => r[“ifHCInOctets”] < 50.0 or r[“ifHCInOctets”] > 9000.0
messageFn = (r) =>
“Check: ${r._check_name} is: ${r._level} Outside of threshold 50 Mbps - 9000 Mbps. State duration: ${string(v: r._value)} minutes.”

data
|> v1"fieldsAsCols"
|> monitor[“check”](data: check, messageFn: messageFn, crit: crit)
|> monitor[“stateDuration”](unit: 1m)

Hello @Si-Richards,
It’s worth mentiononing that using the monitor.check function is used by the UI by default but if you don’t care about the metadata that the monitor package produces. I fist suggest simplifying your alert script. You don’t need to use those functions you can just filter with conditionals.

and use the to() function to write any data that you care about. retaining.

Heres an example of an alert task without using the monitor package:

I like this general approach because I find the monitor functions and separating out the data into a new bucket to be kinda black boxy.

But on to your actual question.
first I’d calculate the state duration.
Then I’d filter for values above your critical duration.
Next I’d call the slack.message() function.

Thats outlined in the link more or less above.

Let me know what you think about this approach :slight_smile: