Hi,
I work for a company that produces woodworking machines. Every machine has a gateway that sends data to our cloud e.g. status of the machine.
We would like to start using influxdb to implement a notification service.
Every time a machine change status we would like to send a notification (email/slack …).
I have read the following articles
A useful feature should be
monitor.stateChangesOnly
but this works only with the four std _level ( crit
, warn
, info
, or ok
), we have 8 custom states so I can’t map this to the standard level 
In an article I read that:
. If you need to define more than 4 custom levels for your check, we recommend making multiple checks and implementing helpful check naming, custom tags, and messages
where can I find an example?
Thanks in advance and I apologize for the length of the message,
Benjamin
Hello @bjbezzi,
Welcome!
That’s such a cool use case.
Give me a day to test some stuff and get back to you. Thank you for your patience. If I don’t get back to you end of day tomorrow ping me.
1 Like
Hello @bjbezzi,
I wonder if this would work for you?
import "influxdata/influxdb/monitor"
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)
|> limit(n: 10)
|> map(
fn: (r) => ({r with
_level_tag: if r._value >= 95.0000001 and r._value <= 100.0 then
"1"
else if r._value >= 85.0000001 and r._value <= 95.0 then
"2"
else if r._value >= 70.0000001 and r._value <= 85.0 then
"3"
else if r._value >= 65.0000001 and r._value <= 70.0 then
"4"
else if r._value >= 60.0000001 and r._value <= 65.0 then
"5"
else if r._value >= 55.0000001 and r._value <= 60.0 then
"6"
else if r._value >= 50.0000001 and r._value <= 55.0 then
"7"
else
"8",
}),
)
|> map(
fn: (r) => ({r with
_level: if r._level_tag == "1" then
"info"
else if r._level_tag == "2" then
"ok"
else if r._level_tag == "3" then
"warn"
else if r._level_tag == "4" then
"crit"
else if r._level_tag == "5" then
"info"
else if r._level_tag == "6" then
"ok"
else if r._level_tag == "7" then
"warn"
else
"crit",
}),
)
|> yield(name: "before")
|> monitor.stateChangesOnly()
|> yield(name: "after")
It works based off of the assumption that your thresholds or states are defined in an incremental way like this so you don’t accidentally get two “info” levels from different _level_tags sequentially.
Hello @Anaisdg your solution works perfectly.
With this method I can only use the map convention for sending messages, e.g. to slack? I iterates on results and for each send a message. It is correct?
Thanks again and sorry for the trivial questions, but it’s the first time I use InfluxDB,
Benjamin