0

I have a bucket (homeassistant) that collects info from a temperature / humidity sensor.

Of course, I want to downsample his data to min/max/mean of previous day.

Inspired by this post, I created a new bucket downsample and two tasks for the moment:

option task = {name: "BALCONE_MAX", cron: "0 0 * * *"}
option v = {timeRangeStart: -1d, timeRangeStop: now()}

from(bucket: "homeassistant")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r.entity_id == "0x00158d00067beedf_temperature")
    |> filter(fn: (r) => r["_field"] == "value")
    |> aggregateWindow(every: 24h, fn: max, createEmpty: false)
    |> yield(name: "max")
    |> to(bucket: "downsample", org: "sineverba")

And

option task = {name: "BALCONE_MIN", cron: "0 0 * * *"}
option v = {timeRangeStart: -1d, timeRangeStop: now()}

from(bucket: "homeassistant")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r.entity_id == "0x00158d00067beedf_temperature")
    |> filter(fn: (r) => r["_field"] == "value")
    |> aggregateWindow(every: 24h, fn: min, createEmpty: false)
    |> yield(name: "min")
    |> to(bucket: "downsample", org: "sineverba")

They run both at 00:00 and save min and max in same bucket.

But today I checked and... I have only a single point (only the min value, in reality), not the max.

Is it possible to save both value inside same bucket?

sineverba
  • 5,059
  • 7
  • 39
  • 84

2 Answers2

0

You can save both values to the same bucket, in one task. The key is to have different fields for min and max values, otherwise the value gets overwritten by the latter to.

option task = {name: "BALCONE_MAX", cron: "0 0 * * *"}
option v = {timeRangeStart: -1d, timeRangeStop: now()}

data = from(bucket: "homeassistant")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r.entity_id == "0x00158d00067beedf_temperature")
  |> filter(fn: (r) => r["_field"] == "value")
min = data
  |> aggregateWindow(every: 24h, fn: min, createEmpty: false)
  |> set(key: "_field", value: "value_min")
  |> to(bucket: "downsample", org: "sineverba")
  |> yield(name: "min")
max = data
  |> aggregateWindow(every: 24h, fn: max, createEmpty: false)
  |> set(key: "_field", value: "value_max")
  |> to(bucket: "downsample", org: "sineverba")
  |> yield(name: "max")
alespour
  • 397
  • 1
  • 5
0

I enountered a similar problem where I wanted to compute several min and max values, but for multiple fields at the same time to reduce the number of queries.

I found it helpful to use the map function to append _min or _max to the field names. For example, for the field temp_hotwater, I get temp_hotwater_min and temp_hotwater_max and also temp_outside_min and temp_outside_max for temp_outside - all with one query.

data = from(bucket: "house")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r["_measurement"] == "heatpump")
    |> filter(fn: (r) => r["_field"] == "temp_hotwater" or r["_field"] == "temp_outside")

data
    |> aggregateWindow(every: 1d, fn: max, createEmpty: false)
    |> map(fn: (r) => ({r with _field: r._field + "_max"}))
    |> to(bucket: "house_daily", org: "home")
    |> yield(name: "max")

data
    |> aggregateWindow(every: 1d, fn: min, createEmpty: false)
    |> map(fn: (r) => ({r with _field: r._field + "_min"}))
    |> to(bucket: "house_daily", org: "home")
    |> yield(name: "min")