Sum of 2 values which one sometimes is null/empty


I have two metrics (power from meter and power from PV).
I want to add them, but one (from PV) when there is no sun is null.
So in this case I need sum power from meter and 0.
I’ve created below code, that works when both values are visible:

from(bucket: "autogen")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "power")
  |> filter(fn: (r) => r["category"] == "energy")
  |> filter(fn: (r) => r["item"] == "SDM_SystemPower" or r["item"] == "Inverter_AC_Power")
  |> drop(columns:["_start", "_stop", "_field", "_measurement", "category", "label", "type"])
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> pivot(rowKey:["_time"], columnKey: ["item"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with _value: r.SDM_SystemPower + r.Inverter_AC_Power }))
  |> yield()

And when there is only 1 value, I’ve got null/empty graph.
So how can I achieve this?


I believe you can use the fill() function:

|> fill(column: "power_from_pv", value:0)
1 Like

It is much better but I get those strange lines in the graph:

How to fix this?:slight_smile:

Maybe you need to add something like:

|> sort(columns: ["_time"])


Yes, that’s it!
Why Flux has to be soooo (over)complicated?!
I’m from Prometheus metrics world and it is much simpler.
Even old influxql was easier… :slight_smile:

I think it’s a matter of learning. Flux is new and is different because it supports data transformations and also integrations between multiple data sources.

Perhaps this overview would be helpful?

Will try :-).
Thanks @rickspencer3 !