Gettting a count of state changes in a table of boooleans


I have a table of booleans with a state change of an input/output on a plc. Its sent on state change only but there is multiple outputs being sent at once. So we end up with a single output that does repeat the boolean value but the state isnt changed. I am struggling with a query to give me the count “only” of the times the state went from true to false, false to true. If you look at the attached snippet image the value should 2 where is went from false true, then went from true to false. I then know that 1 full unit passed by as it went from false to true (it was seeen) then true to false (it was no longer seen). No idea how to query that!

easy peasy:

2 functions that could help you acomplish the desired task:

stateCount() function | Flux 0.x Documentation (

If you need to detect positive or negative transition, you may use this other one:

monitor.stateChanges() function | Flux 0.x Documentation (

for this one you may need to rename your _value coulmn to_level, create a new one or duplicate _value as_level.

and I think does not work with bool so convert to string i.e.:

  |> map(fn: (r) => ({r with _level: string(v: r._value)}))
  |> monitor.stateChanges(fromLevel: "true", toLevel: "false")

Also, assuming that your most right column is _time every entry seems to have the same timestamp? which shouldn’t be possible because each _value should have an unique timestamp on a time series? (Or maybe I am just looking at _start or _last column?) in any case you may consider using an aggregate window using fn: last with your minimum time interval, like 1s or whatever works better for you.


Thanks for your reply. Ill try the monitor.statechange. i did read that and thought it wouldn’t work as would give me true and false as it just outputs what unique state change there are not a count of state changes.


I used this query like you suggested and it does seem to be working. I will do a manual check against the query but it seems ok! thank you!

import “array”
import “influxdata/influxdb/monitor”

from(bucket: “metrics”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“topic”] == “xxxx/InlineCount/”)
|> filter(fn: (r) => r[“_field”] == “InLine2Counts”)
|> map(fn: (r) => ({r with _level: string(v: r._value)}))
|> monitor.stateChanges(fromLevel: “false”, toLevel: “true”)