In November I wrote about InfluxDB 2.0 and what changes from 1.x. Since then I set up a 2.0 instance on a small 64-bit server, and the Pis send their data there with Telegraf. The old 1.8 instance still runs in parallel. Most of my time went into learning Flux. Nothing here is new for people who use Flux every day, but I could not find these patterns in one place when I started.
All examples assume a bucket sensors with a measurement climate, a tag room, and fields temperature and humidity.
The basic shape
from(bucket: "sensors")
|> range(start: -6h)
|> filter(fn: (r) => r._measurement == "climate")
|> filter(fn: (r) => r._field == "temperature")
|> aggregateWindow(every: 5m, fn: mean, createEmpty: false)
Three things helped me read Flux:
- Each row has
_time,_value,_field,_measurementand the tags as columns. One field per row. This is the big difference from InfluxQL, where one row could have many fields. - The result is a stream of tables. By default there is one table per series, so one per room and field here.
range()is required, and should come right afterfrom(). Filters on_measurement,_fieldand tags right after it are pushed down to the storage engine. If you put amap()before them, the query reads much more data.
In Grafana
In a Grafana panel, do not hard-code the time range. Grafana provides variables:
from(bucket: "sensors")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "climate" and r._field == "temperature")
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
v.windowPeriod gets bigger when you zoom out, so a 30-day panel does not try to draw every point. Without aggregateWindow my first 30-day panels were very slow.
For the legend, Grafana takes the name from the group key. If the names look like temperature {room="kitchen"} and you want only kitchen, I found it easiest to drop columns before the panel gets the data:
|> keep(columns: ["_time", "_value", "room"])
Several fields in one row: pivot
For a table panel or for math between two fields you want them side by side:
from(bucket: "sensors")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "climate")
|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
Now each row has temperature and humidity columns. This only lines up if both fields have exactly the same timestamp, which is true when one Telegraf line writes both. If they come from different sensors, use aggregateWindow on both first, so the timestamps match.
Calculations: map
map() makes a new row from each row. The with syntax keeps the other columns:
|> map(fn: (r) => ({ r with _value: r._value * 1.8 + 32.0 }))
Types are strict. r._value * 1.8 + 32 fails if _value is a float, because 32 is an integer. Write 32.0. This error took me a while the first time, because the message was not clear to me.
After a pivot, map() can use both fields. Dew point with the Magnus formula:
import "math"
from(bucket: "sensors")
|> range(start: -6h)
|> filter(fn: (r) => r._measurement == "climate")
|> aggregateWindow(every: 5m, fn: mean, createEmpty: false)
|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => {
a = 17.62
b = 243.12
g = math.log(x: r.humidity / 100.0) + a * r.temperature / (b + r.temperature)
return { r with _value: b * g / (a - g) }
})
|> keep(columns: ["_time", "_value", "room"])
In InfluxQL I did this kind of thing in Grafana or in a Python script. Here it stays in the query.
Two measurements: join
The outdoor temperature comes from a different measurement, weather. To show indoor minus outdoor:
indoor = from(bucket: "sensors")
|> range(start: -24h)
|> filter(fn: (r) => r._measurement == "climate" and r._field == "temperature" and r.room == "living")
|> aggregateWindow(every: 15m, fn: mean, createEmpty: false)
|> keep(columns: ["_time", "_value"])
outdoor = from(bucket: "sensors")
|> range(start: -24h)
|> filter(fn: (r) => r._measurement == "weather" and r._field == "temperature")
|> aggregateWindow(every: 15m, fn: mean, createEmpty: false)
|> keep(columns: ["_time", "_value"])
join(tables: {in: indoor, out: outdoor}, on: ["_time"])
|> map(fn: (r) => ({ _time: r._time, _value: r._value_in - r._value_out }))
join() renames columns that exist in both inputs with the table name as suffix, so _value becomes _value_in and _value_out. The keep() before the join matters. Without it, the two sides had different columns and group keys, and I got empty or confusing results. I lost an evening on this. I also read that join() can be slow and uses a lot of memory on big ranges. For 24 hours of 15-minute data it is fine.
Downsampling with a task
Continuous queries from 1.x are now tasks. I keep raw data for 30 days in sensors and 15-minute averages forever in sensors_long:
option task = {name: "downsample-climate", every: 1h}
from(bucket: "sensors")
|> range(start: -task.every)
|> filter(fn: (r) => r._measurement == "climate")
|> aggregateWindow(every: 15m, fn: mean, createEmpty: false)
|> to(bucket: "sensors_long", org: "home")
Create the bucket first (influx bucket create -n sensors_long -o home -r 0, where 0 means no expiry). Then create the task in the UI under Tasks, or with influx task create -f downsample.flux.
Two pitfalls:
- If the task fails, nothing tells you. Check the run log in the UI after the first few runs.
- A task only processes data from when it runs. It does not fill in old data. For the history I ran the same query once by hand with a longer
range().
aggregateWindow sets _time to the end of each window by default. That is fine, but it surprised me when I compared with my old continuous query results, which used the start. There is a timeSrc parameter to change this.
What I still do in InfluxQL
The 1.8 instance has dashboards I have not converted, and for “last value per room” panels InfluxQL is shorter. Flux has last(), and it works, but I have not found a reason to rewrite panels that work. New panels I write in Flux.