Hello. I have the voltage measurements of 16 cells that make up a battery. I would like to ask a question to represent the difference between the cell with the highest voltage and the cell with the lowest voltage. Do you know how this can be done? Thank you.
Hi @s118 Are you using Flux or InfluxQL? Have you attempted any queries thus far and if so, can you share here (even if not working or giving incorrect results)?
Hi. Obviously I’m referring to Flux (we’re on an influxdb 2 forum).
If we were talking about a single table I would solve it this way: How to calculate difference between min and max with Flux?
The problem is that I want the difference between the maximum and minimum of 16 measurements.
That was my guess, I have been wrong to assume that before as InfluxQL is still being used by people who are on v2.
Since you have only 16 cells, I would think creating a tag called cell number
would be logical (rather than separate measurements for the 16 cells). Assuming the measurement
would be called voltage
, then I think something like this should work (did not have an opportunity to create fake data and test).
import "math"
data = from(bucket: "s118-bucket")
|> range(start: -1h) // Adjust the time range as needed
|> filter(fn: (r) => r["_measurement"] == "voltage")
maxVoltage = data
|> max(column: "_value")
|> findRecord(fn: (key) => true)
minVoltage = data
|> min(column: "_value")
|> findRecord(fn: (key) => true)
diff = math.abs(x: maxVoltage._value - minVoltage._value)
Tanks. This is the code:
import "math"
data = from(bucket: "energy")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "VOLTAGE_CELL")
maxVoltage = data
|> max(column: "_value")
|> findRecord(
fn: (key) => true,
idx: 0,
)
minVoltage = data
|> min(column: "_value")
|> findRecord(
fn: (key) => true,
idx: 0,
)
delta = math.abs(x: maxVoltage._value - minVoltage._value)
and this is the error obtained:
“error in query specification while starting program: this Flux script returns no streaming data. Consider adding a “yield” or invoking streaming functions directly, without performing an assignment”
Can you add this to the very end and see what happens?
|> yield(name: "voltage_difference")
Error:
expected stream[A] but found float (argument tables)
Hi @s118 Let’s break this down into chunks and try to hone in on the error.
Does this give you a single value?
data = from(bucket: "energy")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "VOLTAGE_CELL")
maxVoltage = data
|> max(column: "_value")
|> findRecord(
fn: (key) => true,
idx: 0,
)
|> yield(name: "max_value")
Other error:
error @9:6-9:13: expected stream[A] but found { B with _value: A, _time: time, _stop: time, _start: time, _measurement: string, _field: string, } (record) (argument tables)
any help to solve my query?