Grafana Canvas - without Persistence!

Hi all!

I’ve been playing around with Grafana for some time now. I built some graphs, but one thing kept nagging me - I had to persist each value I wanted to see on a dashboard, although sometimes I didn’t care about historical data. And as it turns out, I’m not the only one. After some failed attempts, I finally have a working solution I’d like to share with you :blush:

Assumptions

  • openHAB API enabled and allows unauthenticated requests (“implicit user role” enabled)
    • At your own risk. Since I use openHAB and Grafana in a separate network, this is no issue to me. I might extend this tutorial with authentication…
  • Grafana is installed, including Plugin “JSON API” in version 1.3.23.
    • Again, at your own risk. Yes, this plugin is in maintenance mode and the version is not the most recent one. The latest version is broken and the recommended plugin “Infinity” is bloated and confusing, can’t get it running. I might extend this tutorial, once I get infinity running…

Configuration

In Grafana, create a new Datasource using JSON API. Enter the following fields and then press “Save & test”:

  • Name: Your Choice :sweat_smile:
  • URL: http://{opehab-host:openhab-port}/rest/items/
  • Authentication: No Authentication

That’s it. You can verify the URL in your Browser. Firefox will create a browsable tree, other Browsers only show plain text. Either way, you know it’s working.

Create Canvas

Create a new canvas and add a query. Select the Data source you created before. Now it depends on the amount of data you want to visualize. There is an open issue that using multiple queries will end in “NO DATA” eventually. For me, it stopped working after five values. If in doubt, use the multi-value setup to be safe.

Single/few values

Add a query for each value. Configure each as follows:

  • Fields
    • Field $number($split(state, " ")[0])
    • JSONata
  • Path
    • GET
    • URL: /{name of your item in openhab}

Several values

Add a single query, configure a field for each item:

  • Fields
    • Field $split($single($, function($v, $i, $a) { $v.name=“name of your item in openhab” }).state, " ")[0]
    • JSONata
    • Alias is mandatory
  • Path
    • GET
    • URL: /

For Both

Disable cache (e. g. cache time 0s) for each field to always fetch the most recent value.

Conclusion

Add metric values to dashboard and select the fields you created. Now you always get fresh values without persistence.

2 Likes

Nice.

To add the Authorization:

Setup a Token in openHAB, so

  • In Main UI klick on the username, then
  • Create new API Token
  • Insert credentials and a name for the token ([A-Z,a-z,0-9], no special characters, no spaces)
  • click the button to create the token
  • copy the token (complete line) to the clipboard. Be aware that the token is not stored but only a hash, so if the token is lost, you will have to create a new one.

Now in Grafana Data Source JSON API, Add a HTTP Header:

  • Header = Authorization
  • Value = Bearer <content of clipboard>