Flux: Query for last value

Hello,
I would like to display the current value for the current day in this chart:


The values for the current day are stored in a separate measurement, “Wind-currentDay.” However, I only need the last and current value for display purposes.

The query without the daily value looks like this:

from(bucket: “iobroker_all”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “Wind-PerDay”)
|> filter(fn: (r) => r[“_field”] == “value”)

How do I adjust the query?

For the last value, call this Query A:

from(bucket: "iobroker_all")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "Wind-PerDay")
|> filter(fn: (r) => r["_field"] == "value")
|> aggregateWindow(every: 1d, fn: last, timeSrc:"_start")  // displays the last value each day from the Wind-PerDay measurement

For the current value, call this Query B:

from(bucket: "iobroker_all")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "Wind-currentDay")
|> filter(fn: (r) => r["_field"] == "value")
|> aggregateWindow(every: 1d, fn: last, timeSrc:"_start")  // displays the last 'current' value each day from the Wind-currentDay measurement

Create both Query A and B in Grafana so they are both displayed.

with a small change:

from(bucket: "iobroker_all")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "Wind-ProTag")
|> filter(fn: (r) => r["_field"] == "value")
|> aggregateWindow(every: 1d, fn: last, timeSrc:"_start")
|> yield(name: "protag")
from(bucket: "iobroker_all")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "Wind-aktuellerStand")
|> filter(fn: (r) => r["_field"] == "value")
|> aggregateWindow(every: 1d, fn: last, timeSrc:"_start")
|> yield(name: "akttag")

In both cases, you created one single query (“A”). Try doing it as two separate queries (“A” and “B”) as I had explained.

I cannot tell from your response above. Are you satisified with the results?

Hello,
I’m still testing, but your tip has already helped me a lot.

Background to the problem:
I want to display the maximum values of the wind measurements (past days and current day) in Grafana (bar chart).
For this purpose, I have two measurements with data.
The first measurement contains the maximum values of the respective day (1 value per day) for each day (time: 11:58 PM).
In addition, I record the values of the current day (multiple values) in a second measurement. Here, a new value is always written whenever the value is higher than the last recorded value. At the end of the day, the last recorded value, which must be the highest of the current day, is transferred to the first measurement.
I then delete the second measurement (daily measurement) using “influx delete.” This way, current values can be saved for the next day.

In the latest version, I first removed this line from the first query:
|> aggregateWindow(every: 1d, fn: last, timeSrc:“_start”)
Reason: In the bar chart, the times were displayed as 2025-07-15 02:00:00, for example, but after removing them, they were displayed as 2025-07-15 23:58:00.

If I do the same for the second query, it looks as if all the values from the second query are displayed, not just the last and largest value.

I’ll continue testing and get back to you in a few days. I hope I’ve made myself clear and will now hand this over to the German-English translator.

Thanks in advance.

My test isn’t complete yet.
This is the current status:

I also tried changing the color of the bar on the right for the current daily balance (Edit - Add fields override), but the colors are then changed for everything, even though I only select the relevant field.

A fundamental mistake I made was not paying attention to the time setting in influx (UTC).
I’m still having problems with that (see x-axis).

Thanks for the update, but have you tried creating 2 queries (A and B) instead of just one like you have done (A)? Not sure it will make any difference, but an easy thing to try.

That’s a good idea.
With just the first query, everything actually looks normal:

Here alone with only the second query, also ok.:

Both queries again with problem on the x-axis:

Have I missed something?

In all 3 cases above, you created one single query (A). What changes (if anything) if you create separate queries (A and B)?

I’m sorry, but I don’t understand what you mean.

To create a second (or third, fourth, etc.) query, click this button in the lower left while in Edit mode:
image

This is what it looks like so far:

Here with only the first query:

Here with the first query and Add query for the second query:

I can’t see any difference.

Everything seems to be OK in the data for the first query:

The data for the second query also contains time information from previous days, but without values. These rows should not be present:

I think that’s the reason for the label on the x-axis.

For the second query, what happens if you change this line to include ‘createEmpty: false’?
|> aggregateWindow(every: 1d, fn: last, createEmpty: false)

That worked! Why did I miss that? It was already included in the first query.
This solves the problem, and I thank you very much for your help.
If you have time at some point: Is it possible to set a different color for the bar (the second query)? I had no success with “Add fields override.” If I set it to the second query, the color changes for all bars.
But that would only be for visual purposes and not really that important.

Thank you again!

I do not see a legend on your graph. Can you display the legend and then click on the series that you want to change the color for?

I thought it would be okay now, but the crossed-out bar shouldn’t appear. Only the last value (19.4) would be correct here. Only this bar should actually have a different color.

This is the current data from iobroker. I don’t know why the value 4.7 is displayed again in Grafana.

Can you create 2 queries (A and B) and see if that gives you 2 different legends?


two values are displayed, regardless of whether “Values=Max” or “Values=Last”