0 instead of "No Data", "else" not working as expected

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.

Anyone an idea how I can get this to work? :slight_smile:

import "timezone"
import "date"

option location = timezone.location(name: "Europe/Vienna")
  from(bucket: "fronius_aktuell")
  |> range(start: today())
  |> filter(fn: (r) => r._measurement == "actual")
  |> filter(fn: (r) => r._field == "E_STORAGE_CHARGE")
  |> aggregateWindow(every: 5m, fn: last, createEmpty: true)
  |> map(fn: (r) => ({
      r with
      _value: if exists r._value then float(v: r._value) + 100.0  else 0.0

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 

heres another approach:

1 Like