How to use current time in a calculation

  • What Grafana version and what operating system are you using?
    8.5.4 RPI4

  • What are you trying to achieve?
    I’m trying to perform a live calculation based on time and distance. The calculation requires a target time and needs to know how much time is remaining until the target time (i.e. Target time - current time = time to target). How can I get the current time into a Grafana calculation? I can set the target time via a dashboard variable (open to suggestions if there’s a better way).

Calculation: Distance/((TimeRemainingMins/60)+(TimeRemainingSec/60^2)) = Speed Required to reach target at target time. I want to panel to show this required speed in real time.

What’s the best way to go about this? I tried the clock widget for the countdown timer but I can’t reference its value at the same time as a Prometheus query. I figure I can probably feed the data into Prometheus somehow but then it’s likely to be delayed a few seconds and I want it as close to perfectly accurate as possible.

Appreciate any pointers thanks.

Is anyone able to provide some advice on this one?

Hi @veridian5

I think what you want is possible so long as you are using Grafana OSS (not Cloud) and have enabled / allowed unsanitized HTML scripts. Once that is done, you should be able to write some Javascript code in an HTML panel to use the value you want and then calculate the Speed Required to Reach Target.

I am Jar Jar Binks when it comes to writing Javascript, but there are many Jedi’s floating around different forums that could help you out (if you need help).

Here was my post on something sorta related…Semi-flexible countdown - #4 by grant2

Thanks @grant2 for the response. I did see the previous post about Tryit Editor v3.7 and tried to incorporate that. Really I only need the number of seconds so I’ve modified it.

I also managed to get the clock plugin to display a countdown based on a dashboard variable, which then allows me to calculate the seconds remaining.

The problem with both solutions is I’m stuck getting the outputs of the panels into a final calculation.

I have two panels Distance (Prometheus source) and SecondsRemaining (from clock plugin / Javascript).

To calculate the speed I need to execute the following formula:

How do I pull in the current values of the two panels listed above in order to make a new panel that performs the calculation for the speed required? Thats the part I’m stuck on.

@veridian5 I have to admit, this might be a tough one to implement in Grafana, in that you need the up-to-the-second value from Prometheus AND the up-to-the-second value from the Clock plugin panel countdown value. Maybe someone out there can dream up a solution.

What is the source of the Distance metric that you are storing in Prometheus? If it’s some sort of controller that could have its output sent to another destination simultaneously (like InfluxDB), then you could use Node-RED to do the calculation (Distance/(SecondsRemaining/60^2)=Speed) and then maybe have that new value go into a new panel to Grafana (using MQTT or Websocket or something as an intermediary).

Maybe you could use the svg approach but my question is, is there a way in grafana to make a dashboard query or poll a data source every second?

Bit more about the solution. RPI4 with a RS422 hat with a serial NMEA 0183 feed at 9600 baud. The serial feed is interpreted by SignalK which can read most NMEA data, then exported to a Prometheus endpoint (via a signalK plugin), Prometheus polls that endpoint every second, Grafana Polls Prometheus every second, and my dashboard refreshes every second. Rather complicated I know but I’m not aware of any other way to get the data I need results in a lag of a couple of seconds but that’s good enough if I can get the overall outcome.

I will have a look at NodeRed as maybe that’s the missing link? Can NodeRed monitor a serial feed from a RPI? Perhaps I don’t need any of the other complexity and I can do all the calcs in nodered.

The only slight issue is that SignalK currently sets the RPI clock as the RPI has no Internet and gets its time from the GPS data in the serial feed (processed by SignalK via another plugin that sets the time on the RPI from the GPS). So if I get rid of SignalK then I need a new solution to set the time from the serial feed.

Yes. There’s a way to adjust the minimum refresh in the settings file. Requires a restart.

What’s the SVG approach you refer to?

This should give you more idea