Create Alert from InfluxDB boolean value

Hello,
I am using Grafana v9.1.2 in a docker image, migrated from an older version I am trying to create an alert from an InfluxDB Query that returns a boolean value.
In the panel, it looks fine


But when trying to create an alert from it:

Consulting the log, I get:
msg="alert rule panic" error="interface conversion: interface {} is nil, not bool"

Is there any way to create an alert from a boolean?
May also be there is a problem with my Grafana.

1 Like

Would you be willing to update to Flux? Looks easy acc. to this. I can try later if time allows.

Unfortunately I am not the only user. Many are “less proficient” when it comes to these queries. We also have to deal with a lot of alerts that are configured for InfluxQL (as well as lots of dashboards). The most workable way I have found so far is to duplicate the data source and change the query language there. Then use that data source for new alerts. But I haven’t looked into this too much and haven’t got it to work yet.

I quickly set up some test data and confirmed that the |> toInt() Flux function worked perfectly on the Grafana alerting.

from(bucket: "RetroEncabulator")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "BooleanData")
  |> filter(fn: (r) => r["Location"] == "FrontDoor")
  |> filter(fn: (r) => r["_field"] == "switch_state")
  |> toInt()
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
  |> yield(name: "boolean_state")
1 Like

Is the problem with Alerts on booleans an issue which is still unresolved? Now Flux is no longer an option I would like to see if there is an option to use the boolean for alerts.

1 Like

I am currently looking for a solution for this. I want to alert based on a boolean field from an influx 1.8 measurement. It seems like it should be simple: if true then alert. Is there a reasonable way to do this?

Welcome @nathanpegram to the Grafana forum. I guess the creators of InfluxQL did not think that far ahead! I am not really good with InfluxQL, but maybe if you’re looking to alert on the presence of true values within a time window (e.g., “is this boolean field true at least once in the last 5 minutes?”), you can use COUNT() with a WHERE clause.

SELECT COUNT("your_boolean_field") AS boolean_as_int
FROM "your_measurement"
WHERE "your_boolean_field" = true
AND time >= now() - 5m
GROUP BY time(1m)
FILL(0)

@grant2, that sounded like a good idea. But having just tried it, I find that no data is returned when the where condition "your_boolean_field" = true is not met. I was expecting 0 counts to be returned. If I change the condition to look for false, I get counts as expected. It’s possible I could choose to ignore when this alarm is in error as it would probably come out of error if there actually were a true value, but that isn’t ideal.