Until my inverter starts to charge the battery, the “E_STORAGE_CHARGE” field does not get new data.
In this state I get “No Data” when I want to query how much energy I pumped into the battery today.
I would like to get a “0” instead of No Data.
I have tried the following, and while the math works when there is a field (in this example I add +100 just for a test) , the “else 0.0” does not do anything when there is “No Data” - I still get “No data” then as result.
Hello @cholzer,
The issue is that if you aren’t returning data after the range, filter, or agg window functions then you have nothing to map to.
What you can do is create an array something like:
import "array"
import "experimental"
// Query data from the bucket
noResultData = from(bucket: "anais")
|> range(start: -1d)
|> map(fn: (r) => ({
r with
_value: float(v: r._value) + 100.0
})
)
// Fallback data
whenNo = array.from(rows: [{_measurement: "test", _time: 2020-01-01T00:00:00Z, _field: "test", _value: 0.0}])
// Attempt to merge the query result with fallback data, ensuring at least one record is present
dataExists = exists (noResultData |> findRecord(fn: (key) => true, idx: 0))._value
if dataExists
then noResultData |> yield(name: "data")
else whenNo