The error you're seeing:
invalid: runtime error @12:6-12:57: filter: type conflict: time != int
is happening because the math.abs() function is being applied to a time value, which doesn't make sense. Specifically, this line:
|> filter(fn: (r) => math.abs(x: r.current_soc) > 0.1)
This looks okay at first glance, but later in your map function, you are using current_soc as both:
- A float (
soc_change = r.current_soc)
- And also trying to subtract time values from it
But more importantly, the actual issue is that inside the map function, you're assigning:
current_soc = r._value
Then:
soc_change = r.current_soc
previous_soc = current_soc - soc_change
However, r.current_soc is actually coming from:
|> difference(nonNegative: false, keepFirst: true, columns: ["current_soc"])
And the result of difference() on a time-series column will return a duration if the _time field is used for ordering — and this can accidentally propagate durations into numeric fields depending on how the data flows.
✅ Root Cause:
You are applying difference() to the current_soc column before duplicating _value, and the difference() function uses timestamps to compute differences — so it may be outputting a duration instead of a number.
✅ Fix: Restructure Your Pipe to Ensure Numeric Flow
Here's a corrected version of your Flux script with explanations:
import "math"
import "date"
from(bucket: "${BUCKET}")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] =~ /${DEVICE:regex}/)
|> filter(fn: (r) => r["_field"] == "BMS_FastUpdateSOC")
|> aggregateWindow(every: v.windowPeriod, fn: mean)
|> sort(columns: ["_time"])
|> duplicate(column: "_value", as: "current_soc") // preserve original value
// Compute time delta between points
|> elapsed(unit: 1ns, columnName: "delta_time")
// Now calculate SoC difference (must be done after elapsed() to avoid confusion)
|> difference(columns: ["current_soc"], nonNegative: false, keepFirst: true)
// Filter out small changes
|> filter(fn: (r) => math.abs(x: r.current_soc) > 0.1)
|> map(fn: (r) => ({
_time: r._time,
delta_time: r.delta_time,
soc_final: math.round(x: r._value * 10.0) / 10.0,
soc_change: math.round(x: r.current_soc * 10.0) / 10.0,
time_final: r._time,
time_initial: r._time - r.delta_time,
// Optional: Anomaly flag
is_anomaly:
if math.abs(x: r.current_soc) > 3.0 then 1.0 else 0.0
}))
|> sort(columns: ["_time"])
|> group()
|> yield(name: "soc_changes")