Divide field value by n



Published: 2021-10-30 12:51:13 +0000
Categories: Flux,

Language

Flux

Description

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

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}))

Keywords

flux, conversion, map, influxdb,

Latest Posts


Copyright © 2021 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk and http://phecoopwm6x7azx26ctuqcp6673bbqkrqfeoiz2wwk36sady5tqbdpqd.onion