Hey there!
Today I found out how to add annotations in grafana automatically with a rule from OpenHAB. I didnt find any instructions so I thought I’d share my knowlegde.
What you can do after this tutorial
Add annotations to grafana automatically from a rule via OpenHAB. So you can add an annotation when i.e. the climate control goes on for your power consumption graph or when a device goes offline for your online monitoring graph
What you need
- A running OpenHAB instance
- A running Grafana instance. It doesn’t matter if it runs on the same host as the OpenHAB instance
How to
- Create an API-Key: For that click on “Settings” → “Api Keys” in your Grafana dashboard. Set your API Key to not expire The name of the key doesn’t matter and is only used for naming purposes inside Grafana
- Create the rule in OpenHAB:
val String url = "http://api_key:API-KEY@IP:3000/api/annotations"
val String DashboardID = "2"
rule "Add annotation to Grafana if Bettlampe goes offline"
when
Item Netzwerk_Bettlampe_Online changed to OFF
then
logInfo("Rule triggered", "Rule \"test.rules: Testrule\" started")
var String json = '{
"panelId":10,
"dashboardId":' + DashboardID + ',
"time":' + Long::toString(now.millis) + ',
"tags":["Online"],
"text":"Bettlampe offline"
}'
sendHttpPostRequest(url, "application/json", json)
end
-
Exchange your IP address and your API-Key (the upper case “API-KEY”, the lower case stays the same). It should look something like this:
val String url = "http://api_key:eyJrIXXXXXXXXXXXXXXXXXXXXXXiLCJuIjoib3BlbmhhYiIsImlkIjoxfQ==@192.168.178.108:3000/api/annotations"
-
Exchange your DashboardID and the panelId
a. You can find the DashboardID by going to your desired dashboard and then “Dashboard settings” (in the upper right) → “JSON Model” and then "id"
As you can see the ID of my desired dashboard is “2” and since I only have this one dashboard I made it a global variable.
b. You can find the panelID by clicking on share on the desired panel and then searching in the link for "viewPanel="
In this case the panelId is “10”.
Now you can edit the rule as you want to do add annotations as you desire.
You can find more information on whats possible in the Grafana documentation.
Finding mistakes
When you get this message in your log
Fatal transport error: java.util.concurrent.ExecutionException: org.eclipse.jetty.client.HttpResponseException: HTTP protocol violation: Authentication challenge without WWW-Authenticate header
you did something wrong at the authentification. Check your settings there.
The end
Questions/Suggetions/Other ideas?
I hope I was able to help you and give you some ideas whats possible with this!
Greetings
Felix