Lack of data using the difference function

With node-red I’ve created a program that imitates an electricity meter index.
The data rises, then falls again when the index is reset.

With influxdb, I want to use the “difference” function to show and calculate the difference each time the index is different.

But the problem I’m having is with the first data after the reset, which doesn’t seem to be counted.

Since the index data has rotated without passing through 0, 9 - 60 being a negative number and the parameter forbids negative numbers, it sets nothing/0.
But there’s an error. 9 still has to be counted. Plus the difference between the counter’s high threshold.

from(bucket: "Test")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "IndexVirtuel")
  |> filter(fn: (r) => r["_field"] == "Index")
  |> difference(nonNegative: true, columns: ["_value"])

For example, if my high threshold is 65, and the next value is 9, then the consumption is 9+(65-60) = 14.

I need a little help on how to use the “difference” function, which must take into account more parameters than just the difference.

Today, the only solution I’ve found is to use Node-red to calculate the difference each time the index falls.
The script also checks that the new value is not lower than the previous one. If it is, it makes the counter’s max value - the previous value + the new value.

I know that this is an extremely rare case, but it’s still a fundamental principle for not forgetting values.

Thanks for your help

@Yohan The behavior you’re seeing is because of the nonNegative parameter. With this parameter set to true, difference() replaces negative difference values with null. However, if you set the initialZero parameter to true, it will return the difference between 0 and the value (which is what I’m guessing you want). This behavior is described in the documentation.

I think this will give you what you want:

from(bucket: "Test")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "IndexVirtuel")
  |> filter(fn: (r) => r["_field"] == "Index")
  |> difference(nonNegative: true, initialZero: true, columns: ["_value"])

Hi @scott , thank you for your interest in my problem and your quick response.

What you gave me works compared to 0.

But there may still be the difference between the max value and the previous value.

If the counter has a max value of 100, the last value sent was 95 and the new one is 5. Thanks to what you’ve given me, it does count the 5 starting from 0. But I think it’s missing the 5 between 100 and 95.

I don’t know if there’s a solution for this (like a parameter that lets you set the high value).

There currently isn’t a way to do this with Flux. This requires some custom aggregate functionality that currently isn’t available. difference() is a row aggregator that performs an aggregate operation between values in subsequent rows. There isn’t a way to define this type of custom function in raw Flux (you’d have to do it in Go as a “primitive” function).

There is proposed functionality that would allow for this, but it’s not in development: EPIC: scan function · Issue #4671 · influxdata/flux · GitHub