Hi,
How do you do in a query to loop through a date range a significant number of times?
For now, I relied on an answer from “grant1” on the topic “difference between 2 values in a query”.
It works for my project except if I want to do it over 365 days (it becomes impossible to write the query).
It may be necessary to combine the aggregateWindow function with the difference function but I can’t do it
So here’s what I have write for only 2 days:
import “math”
day001period1_start = time(v: “2023-02-25T00:00:00Z”)
day001period1_stop = time(v: “2023-02-25T06:00:00Z”)
day001period2_start = time(v: “2023-02-25T06:00:00Z”)
day001period2_stop = time(v: “2023-02-25T23:59:59Z”)
day002period1_start = time(v: uint(v: day001period1_start) + uint(v:1d))
day002period1_stop = time(v: uint(v: day001period1_stop) + uint(v:1d))
day002period2_start = time(v: uint(v: day001period2_start) + uint(v:1d))
day002period2_stop = time(v: uint(v: day001period2_stop) + uint(v:1d))
day001period1_first = from(bucket: “Energie”)
|> range(start: day001period1_start, stop: day001period1_stop)
|> filter(fn: (r) => r[“_measurement”] == “TeleInfoPzem”)
|> filter(fn: (r) => r[“Equipement mesuré”] == “PzemPac”)
|> filter(fn: (r) => r[“field"] == "Puissance_totale_consommee(KWh)”)
|> first()
day001period1_last = from(bucket: “Energie”)
|> range(start: day001period1_start, stop: day001period1_stop)
|> filter(fn: (r) => r[“_measurement”] == “TeleInfoPzem”)
|> filter(fn: (r) => r[“Equipement mesuré”] == “PzemPac”)
|> filter(fn: (r) => r[“field"] == "Puissance_totale_consommee(KWh)”)
|> last()
day001period2_first = from(bucket: “Energie”)
|> range(start: day001period2_start, stop: day001period2_stop)
|> filter(fn: (r) => r[“_measurement”] == “TeleInfoPzem”)
|> filter(fn: (r) => r[“Equipement mesuré”] == “PzemPac”)
|> filter(fn: (r) => r[“field"] == "Puissance_totale_consommee(KWh)”)
|> first()
day001period2_last = from(bucket: “Energie”)
|> range(start: day001period2_start, stop: day001period2_stop)
|> filter(fn: (r) => r[“_measurement”] == “TeleInfoPzem”)
|> filter(fn: (r) => r[“Equipement mesuré”] == “PzemPac”)
|> filter(fn: (r) => r[“field"] == "Puissance_totale_consommee(KWh)”)
|> last()
day002period1_first = from(bucket: “Energie”)
|> range(start: day002period1_start, stop: day002period1_stop)
|> filter(fn: (r) => r[“_measurement”] == “TeleInfoPzem”)
|> filter(fn: (r) => r[“Equipement mesuré”] == “PzemPac”)
|> filter(fn: (r) => r[“field"] == "Puissance_totale_consommee(KWh)”)
|> first()
day002period1_last = from(bucket: “Energie”)
|> range(start: day002period1_start, stop: day002period1_stop)
|> filter(fn: (r) => r[“_measurement”] == “TeleInfoPzem”)
|> filter(fn: (r) => r[“Equipement mesuré”] == “PzemPac”)
|> filter(fn: (r) => r[“field"] == "Puissance_totale_consommee(KWh)”)
|> last()
day002period2_first = from(bucket: “Energie”)
|> range(start: day002period2_start, stop: day002period2_stop)
|> filter(fn: (r) => r[“_measurement”] == “TeleInfoPzem”)
|> filter(fn: (r) => r[“Equipement mesuré”] == “PzemPac”)
|> filter(fn: (r) => r[“field"] == "Puissance_totale_consommee(KWh)”)
|> first()
day002period2_last = from(bucket: “Energie”)
|> range(start: day002period2_start, stop: day002period2_stop)
|> filter(fn: (r) => r[“_measurement”] == “TeleInfoPzem”)
|> filter(fn: (r) => r[“Equipement mesuré”] == “PzemPac”)
|> filter(fn: (r) => r[“field"] == "Puissance_totale_consommee(KWh)”)
|> last()
union(tables: [day001period1_first, day001period1_last, day001period2_first, day001period2_last, day002period1_first, day002period1_last, day002period2_first, day002period2_last])
|> difference()
|> map(fn: (r) => ({r with _value: math.abs(x: r._value)}))
I specify that I don’t master the programming this is the reason why I try to manage with bits of code that I adapt to my situation, except that there I am blocked.
Thanks for your help if there is a solution.