Divide field value by n (Flux)
This snippet details how to divide a field value by a fixed value in Flux - for example diving by 100.00 to convert pounds to pence
Details
- Language: Flux
Snippet
map(fn: (r) => ({ r with _value: float(v: r._value) / <number> } ))
Usage Example
// Convert pence to pounds
from(bucket: "Systemstats")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "power_watts")
|> filter(fn: (r) => r["_field"] == "today_cost")
|> filter(fn: (r) => r["host"] == "power-meter")
|> map(fn: (r) => ({r with _value: float(v: r._value) / 100.00}))
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "max")
// Convert watts to Kilowatts
from(bucket: "Systemstats")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "power_watts")
|> filter(fn: (r) => r["_field"] == "watts_today")
|> filter(fn: (r) => r["host"] == "power-meter")
|> last()
|> map(fn: (r) => ({r with _value: float(v: r._value) / 1000.00}))
// Convert feet to miles
from(bucket: "traveltracker")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "mileage")
|> filter(fn: (r) => r["_field"] == "distance_today")
|> last()
|> map(fn: (r) => ({r with _value: float(v: r._value) / 5280.00}))