Hi to all,
I have this task:
option task = {
name: "DOWNSAMPLE APRILIA UTILITIES",
cron: "0 * * * *",
}
option entity_id = "shellyem_c45bbe5fed52_channel_1_energy"
option prefix = "energy_consumption_"
from(bucket: "homeassistant-aprilia")
|> range(start: -2h, stop: -1h)
|> filter(fn: (r) => r["_measurement"] == "kWh")
|> filter(fn: (r) => r["_field"] == "value")
|> filter(fn: (r) => r["domain"] == "sensor")
|> filter(fn: (r) => r.entity_id == "shellyem_c45bbe5fed52_channel_1_energy")
|> aggregateWindow(every: 1h, fn: max, createEmpty: false)
|> difference()
|> yield(name: "difference")
|> to(bucket: "downsample-utility-aprilia", org: "sineverba")
I have data in starting bucket.
If I launch the script (without the to
section) I can see aggregated - difference data.
And… If I don’t omit the to section, launched from starting bucket it write data to the destination bucket!
But, placed in the task, it doesn’t write nothing to the bucket and bucket itself is empty.
I have no idea why, task result says “executed” with state success.
@Anaisdg a small up for me. Seems a bug wth difference
: see also other task of another user: Tasks works fine when started manually, but not automatically? - #10 by MarcoB
Looking for your help
I did change the function, with last
:
option task = {
name: "DOWNSAMPLE APRILIA UTILITIES",
cron: "0 * * * *",
}
option entity_id = "shellyem_c45bbe5fed52_channel_1_energy"
option prefix = "energy_consumption_"
from(bucket: "homeassistant-aprilia")
|> range(start: -2h, stop: -1h)
|> filter(fn: (r) => r["_measurement"] == "kWh")
|> filter(fn: (r) => r["_field"] == "value")
|> filter(fn: (r) => r.entity_id == entity_id)
|> aggregateWindow(every: 1h, fn: last, createEmpty: false)
|> difference()
|> to(bucket: "downsample-utility-aprilia", org: "sineverba")
But without luck…
@MarcoB @Juan_Alonso_Pla do you want to help me? My task seems really very similar to your, @MarcoB .
I have a incremental electricity usage:
And I want to sum the electricity usage from -2h to -1h, to get last hour. As homeassistant does:
MarcoB
March 28, 2022, 8:44pm
4
I think you have to use
|> range(start: -2h) instead of |> range(start: -2h, stop: -1h)
So if e.g. your task runs at 22:00:01 it uses difference between:
the last value from the period 20:00:01 - 21:00 (so the last value from the “previous” hour) and
the last value from the period between 21:00 and 22:00:01 (is the last value from the “current” hour that just ended)
So that is actually the usage between 21:00 and 22:00:01
This is then written with the timestamp of 22:00:01
I honestly see the same problem as in the other topic:
Hi,
I’m a newbie, moving from InfluxDb 1.8 to 2.x. So I have to migrate my continuous queries to tasks.
Now I have made up a Flux Task that nicely downsamples my data to hourly or daily level, and writes this to the appropriate bucket.
This works all fine when I start the task by pressing the “Run Task” button in the GUI but not when the task runs according to the schedule. The log show " success" at the scheduled times, so it is run at the appropriate times, but I see no data appearing in t…
You are querying an interval of 1h (range(start: -2h, stop: -1h)) and then aggregating in intervals of 1h (aggregateWindow(every: 1h, fn: last, createEmpty: false)). The aggregateWindow function will return only one record for the interval especified and therefore, the difference function will not return anything since it needs two subsequent records to compute a difference.
If you want to get the last hour, why don’t you use:
and then, if aggregate by every hour:
aggregateWindow(every: 1h, fn: last, createEmpty: false)
In this way, you will get the last value of both intervals (-2h to -1h, -1h to 0h). After doing this, if you apply difference, this will compute the difference between both intervals. This difference is the energy usage for the last hour, as you requested.
Let me know if that is what you expected.
Solved, with stop at now()
:
option task = {
name: "DOWNSAMPLE APRILIA UTILITIES",
cron: "0 * * * *",
}
option entity_id = "shellyem_c45bbe5fed52_channel_1_energy"
data = from(bucket: "homeassistant-aprilia")
|> range(start: -2h, stop: now())
|> filter(fn: (r) => r["_measurement"] == "kWh")
|> filter(fn: (r) => r["_field"] == "value")
|> filter(fn: (r) => r.entity_id == entity_id)
|> aggregateWindow(every: 1h, fn: last, createEmpty: false)
|> difference()
|> to(bucket: "downsample-utilities-aprilia", org: "sineverba")
Thank you guys!
MarcoB
March 29, 2022, 6:33am
7
Great!
Isn’t stop: (now) the same as leaving that out?
I think you are right. Simply, I prefer a notation where every variable is declared (spoiler: I’m a developer )
1 Like