Skip to content

Instantly share code, notes, and snippets.

@alsargent
Last active December 20, 2022 09:46
Show Gist options
  • Save alsargent/f0688f996d95f6dda9847cb369badba6 to your computer and use it in GitHub Desktop.
Save alsargent/f0688f996d95f6dda9847cb369badba6 to your computer and use it in GitHub Desktop.
// Import Flux date package: https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/date/
import "date"
// Import Flux dictionary package: https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/dict/
import "dict"
bucket = "mybucket"
// Work with data collected in the past 5 minutes
start = -5m
stop = now()
measurement = "m0"
field = "f0"
// Create a dictionary of kwh measurements
kwh_by_day = [
date.Sunday: 0.05,
date.Monday: 0.03,
date.Tuesday: 0.02,
date.Wednesday: 0.01,
date.Thursday: 0.15,
date.Friday: 0.03,
date.Saturday: 0.07,
]
// Query from our bucket defined above
from(bucket)
// Query from our time range defined above
|> range(start, stop)
// Pull out our measurement and field defined above
|> filter(fn: (r) => r._measurement == measurement and r._field == field)
// For reach row r in the results, get today's day of week, and
// look up the kwh for today
// append a column called cost that multiples the kwh hours and daily cost
// map() documentation: https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/map/
|> map(fn: (r) => {
day = date.weekDay(t: r._time)
kwh = dict.get(dict: kwh_by_day, key: day, default: 0.0)
return {r with cost: kwh * r._value}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment