Task difference that doesn't write data in new bucket

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 :slight_smile:

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:

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:

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:

  • range(start: -2h)

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. :smiley:

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!

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 :innocent: )

1 Like