Improve performance and save disk space

Hi,

I have a running chain: data-source, influxdb and grafana.

An application requests data from the data-source and write these data into influxdb.

Now I have the idea to make some optimization steps.
One point is to write only changed values into the database:

Like in this pattern

public class X
{
    private int x = 1;

    public void setX(int proposedValueForX)
    {
       if(proposedValueForX != x)
       {
           writeToInfluxDB();
           x = proposedValueForX;
       }
    }
}

source

On the grafana site, I use this query:

from(bucket: "grafanaBucket")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) =>
    r._measurement == "DEVICE1" and
    r.COMMON == "VALUE1"
    )
  |> set(key: "_field", value: "")
  |> set(key: "COMMON", value: "DEVICE 1")      
  |> aggregateWindow(every: v.windowPeriod, fn: last)

But with this query, I get the value of the last marked time.
On a graph, I need to show the last changed value periodically independent of the last inserted time/value pair.

Is there a known approach for that?

Thx in advance