Hello! Thanks for you help already.
Certainly my way of thinking might be biased / blocked by my previous attempts of solving it. So I am also happy for any other ideas.
-
What Grafana version and what operating system are you using?
- Grafana v11.2.1 (086b47f098) within Home Assistant. I have InfluxQL with Influx v1 as data source.
-
What are you trying to achieve?
- I want to set a Gauge to a (percentage) value (0…100% or 0 to 1). The normal query is working ok. The signal is binary (0/1) and by calculating the MEAN of it I basically have the usage percentage that I want. However I try to make the gauge work as well if there are no values within the user-selected timewindow.
-
How are you trying to achieve it?
Therefore I added the following queries / expressions. The base query is forwardfilled however if there are NaN values at the beginning of the timewindow it would naturally induce an error in the final percentage outtput. To tackle this I introduced the other queries. Please bear with me - it sounds complicated because I probably overcomplicated it, but the actual task is pretty straightforward.
Base query: “raw”
SELECT last(“value”) FROM “autogen”.“Wohnzimmer_Heiz_Status_Stellgroesse_alle_3” WHERE $timeFilter GROUP BY time($__interval) fill(previous)
Other Queries:
“prev”: This gets the last value BEFORE the user-timewindow (idea is to use this to fill all the NaNs that still exist within the base query)
SELECT last(“value”) FROM “Wohnzimmer_Heiz_Status_Stellgroesse_alle_3” WHERE time < $__from * 1000000
→ is a series that holds one value 0 / 1
“prevval”: is a Reduce expression to to be able to reduce the prev to a single constant (to be used like a factor for multiplication) → is a number 0/1
“nans_filled_prev”: Using the is_null math expression to find any NaNs from base query and then multiply those by prevval
expression: is_null($raw)*$prevval
Then I have two REDUCE (Sum) expressions where I basically sum up the two time series: “raw” and “nans_filled_prev”. sum_raw and sum_prev
Also there is an expression that counts the datapoints: As you remember I basically try to calculate a mean, I therefore also make an expression that counts the datapoints: Reduce (Count) on the nans_filled_prev. Name: count_dp
So finally I calculate my heating percentage as:
“final_percentage”:
($sum_raw + $sum_prev) / $count_dp * 100.0
-
What happened?
- This works well if there are ANY datapoints within the timewindow. If there are “No data” / empty array it obviously shows No Data (fair enough). But instead of showing No data I want to set the Gauge to the value of prevval (which is the physically / mathematically correct thing to do). So it should be really easy… : If final_percentage has any result / is not NaN then use it. Otherwise use prevval
-
What did you expect to happen?
- I tried math expressions, I tried The legacy expressions. I tried overrides, I tried transformations. I can’t get it done.
Is it really such a difficult thing I am trying to achieve here? Please help me. Maybe I took a wrong turn early and this can all be fixed by adjusting my queries. Or can be fixed with a simple transformation? I don’t know. So I try to solve it now on a level where I have two values and grafana should just pick the first of them that is not NaN. maybe instead this problem can be fixed already on a time series level beforehand. e.g. use the base query and perform another fill operation on it where prevval is used… But I fail with the execution on either of them.
Dropped approaches :
(outside of the box) set KNX system up so it sends regular telegrams instead of “on change” -telegrams. Is tricky for this particular actor and would create a lot of unnessary data basically.
25 posts - 4 participants