'Issue with min - max - mean task

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1