Alerts "check" for aggregated data (count)

Hello all,

I want to count records for the last 7 days for each tag and compare them with the expected number of records. In case that they are too less records I want to make a “info” alert. The problem I’m facing is, that after I make the aggregation “count” the field “_time” disappear… with that I will get a failure message that “fieldsAsCols” need the _time field. I also tried multiple workarounds but I don’t found a working one.


import "influxdata/influxdb/monitor"
import "influxdata/influxdb/v1"

data = from(bucket: "HeatMetering")
    |> range(start: -7d)
    |> filter(fn: (r) => r["_measurement"] == "Consumer" or r["_measurement"] == "Creator" or r["_measurement"] == "Network")
    |> filter(fn: (r) => r["_field"] == "Energy_kWh")
    |> count()
    |> group(columns: ["_measurement"])

//   |> filter(fn: (r) => r._value < 665.0)
option task = {name: "M-Bus Check", every: 7d, offset: 0s}

check = {_check_id: "087e536c7a807000", _check_name: "M-Bus Check", _type: "threshold", tags: {}}
info = (r) => r["Energy_kWh"] < 665
messageFn = (r) => "${r._level}: one or more heat meters have communication problem"

data |> v1["fieldsAsCols"]() |> monitor["check"](data: check, messageFn: messageFn, info: info)

Hello, the solution I found was to make the alerting only over the function “task” and than use |> map instead of “fieldsAsCols”.

import "influxdata/influxdb/monitor"
import "influxdata/influxdb/v1"
import "http"
import "json"

option task = {name: "Check"}

data = from(bucket: "HeatMetering")
    |> range(start: -7d, stop: now())
    |> filter(fn: (r) => r["_measurement"] == "Consumer" or r["_measurement"] == "Creator" or r["_measurement"] == "Net")
    |> filter(fn: (r) => r["_field"] == "Energy_kWh")
    |> aggregateWindow(every: 1w, fn: count)
    |> sum()
    |> group()
    |> filter(fn: (r) => r["_value"] < 665)
    |> map(
        fn: (r) => {
            message = {text: "one or more heat meters have communication problem (Actual= ${r._value} | Nominal=671)"}
            _ = http.post(url: "****", data: json.encode(v: message))

            return r
        },
    )
    |> yield(name: "Test")