I have a simple tables with Time and Voltage, like this
Time Voltage
2023-04-12 00:00:00 244
2023-04-12 00:05:00 244
2023-04-12 00:10:00 244
2023-04-12 00:15:00 243
2023-04-12 00:20:00 243
2023-04-12 00:25:00 241
2023-04-12 00:30:00 249
2023-04-12 00:35:00 244
2023-04-12 00:40:00 244
2023-04-12 00:45:00 244
2023-04-12 00:50:00 244
I simply want to find the min, max and latest from the Voltage.
data = from(bucket: “home”)
|> range(start: v.timeRangeStart, stop:v.timeRangeStop)
|> drop(columns: [“_start”, “_stop”, “_measurement”, “location”, “region”])
|> filter(fn: (r) => r._field == “Voltage”)
max = data
|> aggregateWindow(every: 1d, fn: max)
min = data
|> aggregateWindow(every: 1d, fn: min)
last = data
|> aggregateWindow(every: 1d, fn: last)
|> yield()
This produces the last value only. I don’t know what yield does but it does not work without it and the error code suggests to use it.
How do I get the code to return the 3 values?
Put yield after every declaration.
You are getting last value because you have defined the yield after last variable declaration.
Try to comment last yield and don’t put any other yield in code you will see the error as put the yield function for output.
I have progressed this a bit further without using the yield at all.
Updated code
data = from(bucket: “home”)
|> range(start: v.timeRangeStart, stop:v.timeRangeStop)
|> drop(columns: [“_start”, “_stop”, “_measurement”, “location”, “region”])
|> filter(fn: (r) => r._field == “Voltage”)
|> pivot(rowKey: [“_time”], columnKey: [“_field”], valueColumn: “_value”)
max = data
|> duplicate(column: “Voltage”, as: “Max”)
|> aggregateWindow(column: “Max”, every: 1y, fn: max)
min = data
|> duplicate(column: “Voltage”, as: “Min”)
|> aggregateWindow(column: “Min”, every: 1y, fn: min)
last = data
|> duplicate(column: “Voltage”, as: “Last”)
|> aggregateWindow(column: “Last”, every: 1y, fn: last)
union(tables: [max, min, last])
|> group(columns: [“_time”])
|> drop(columns: [“Voltage”])
This this output
I am now trying to work out how to use sort and fill to get all the values in 1 row and then use tail(n: 1).
The desc: true/false does not appear to work with null values.
Any ideas?