Variable passed to Grafana not processed until refresh

Working (more like struggling) with Grafana using time series data from InfluxDB stored energie consumption (smart meter) and solar panel production. The basics work fine; I can display graphs with fixed time series intervals (see this post).

But now I want to display bar graphs with varying granularity depending on the time range (hour, day, week, month, year). So for a day graph I want details up to 10m, for month graphs I want just a bar per day displayed. I’ve added a variable to the Grafana dashboard and pass it in the URL to Grafana with a Webview statement depending on the Switch state (like ...&from=now-1w&to=now&var-granularity=1d

The issue: when changing from, for example, week view to day view, the graph gets updated in the browser, but with the ‘old’ granularity. Only after a manual refresh (F5), I get the correct granularity in the bar graph.
I see the Item state changing from ‘1h’ to ‘1d’ in the log when selecting another period with the Switch, but it doesn’t get passed in the URL until a manual refresh in the browser. Looks like an order of events issue where the new URL is sent in the Webview statement before the rule that changes the granularity parameter kicks in.

Sitemap entry:

Webview url="/static/pwroverview.html?dashboard=energieconsumptie&fromItem=Power_Period1&panelItem=Power_Panel1&panelInterval=Power_Gran1

Rule used to set new granularity based on change of Switch item:

rule "Power Graphing period parameter transformation"
when
  Item Power_Period1 changed
then
//  logError("PowerInterval", "Power_Period1 = "+Power_Period1.state)
  switch Power_Period1.state {
    case 'HOUR': Power_Gran1.postUpdate("10m")
    case 'DAY': Power_Gran1.postUpdate("1h")
    case 'WEEK': Power_Gran1.postUpdate("1d")
    case 'MONTH': Power_Gran1.postUpdate("1d")
    case 'YEAR': Power_Gran1.postUpdate("1w")
  }
end

BTW, I used a modified version of @wborn 's great library (added the granularity parameter).

To visualize it, a picture of a correct view:

Followed by a picture of the wrong view after selecting a different time range but before manual browser refresh:

Any thoughts on where to start looking/digging/debugging?

If you’ve added a new item for this, the state change events for new items sometimes only work after restarting openHAB.

You can test if this is the case by opening Basic UI in 2 browser tabs and then changing the item state in one browser tab. If the other browser tab doesn’t properly update the state then you’re having this issue. :frowning:

The other session does get updated if I change from Day to Hours (as an example), but both with the wrong (old) granularity.
Of course, I did restart openHAB (as well as InfluxDB and Grafana), troubleshooting 101… :sunglasses:

So, the issue is that the Item changes state but the Grafana URL doesn’t get updated yet. Just thinking out loud; could it be that the asynchronous Item change (COMMAND received, STATE updated) triggers the RULE execution a fraction later than the Webview update?
The next question then would be, is there a way to trigger a refresh?

Maybe you can post the rule? It should update the Grafana URL using the current state of the items whenever any of the items used in it are updated.

Thanks for your support!

It’s a very simple rule, see my first post above, as well as the Sitemap entry. And the Item definitions are:

String Power_Panel1 "Type grafiek" <none>
String Power_Period1 "Periode" <none>
String Power_Gran1 "Grafiek interval" <none>

When changing the granularity (clicking the switch) the log shows:

[ome.event.ItemCommandEvent] - Item 'Power_Period1' received command WEEK
[vent.ItemStateChangedEvent] - Power_Period1 changed from HOUR to WEEK
[vent.ItemStateChangedEvent] - Power_Gran1 changed from 10m to 1d

To make the information complete, this is the “code” (ahem) I added to your code:

...
                itemFunction: params.heightItemFunction
            },
            interval: {
                key: "var-granularity",
                value: resolveParam(p, "interval"),
                itemName: resolveParam(p, "panelInterval"),
                itemFunction: params.intervalItemFunction
            }
...

…plus a default interval parameter value.