Is there any way that Grafana can use HomeAssistant to send notifications?

Lately, I’ve setup Grafana and I love it.
I’d like to use the Alarm functionality for a few things in my HomeAssistant system, but I’d like Grafana to use HomeAssistant’s notification service to send out important notifications.

Is there anybody that has done this?

2 Likes

Hi Timkoers,

I had the same question and did not find an answer in the blog.
I used the following approach:

I created a notification channel in grafana and used the type webhook and the following webhook settings:

URL: https://mydomain.duckdns.org/grafanapost/api/states/input_boolean.alrm_hightemp
http method: POST

I struggled with passing the autharization bearer key in the post.
Because I have a ngnix server and I used that one to extend the header with the key.

I used the following proxy rule in nginx:

 location  ^~/grafanapost/ {
    proxy_pass http://<my_local_ip>:8123;
    proxy_method POST;
    rewrite ^/grafanapost/(.*)$ /$1 break;
    proxy_set_body '{ "state": "on"  }';
    proxy_set_header  content-type "application/json";
    proxy_set_header  Authorization  "Bearer blabla...................................................................." ; 

}

It is not the best option, but it allows grafana to trigger a status change of an input_boolean in home assistant.

I would certainly be interested in a simpler solution…

1 Like

I build a tool exactly for this. You can find it here: https://github.com/pinpox/home-assistant-grafana-relay
It will listen for webhooks from grafana and send you a notification via home-assistant like this

2 Likes

This is great! I’m able to see the notifications on my phone - but they don’t seem to persist in Home Assistant. But I don’t seem to see any historical notifications in HA so it’s probably a different issue than yours. I do seem to get an exit status of 1 when a client timeout happens, though…

2021/10/26 14:01:30 {"title":"[Alerting] Test notification","ruleId":7876552415975495364,"ruleName":"Test notification","state":"alerting","evalMatches":[{"value":100,"metric":"High value","tags":null},{"value":200,"metric":"Higher Value","tags":null}],"orgId":0,"dashboardId":1,"panelId":1,"tags":{},"ruleUrl":"http://grafana.tylephony.com/","imageUrl":"https://grafana.com/assets/img/blog/mixed_styles.png","message":"Someone is testing the alert notification within Grafana."}

2021/10/26 14:01:35 Post "https://hass.tylephony.com:8123/api/services/notify/notify": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

exit status 1

Should I review over on your Github? Thanks again!

Looks like grafana added support for MQTT notifications. After configuring that, I created a quick automation to set up persistent notifications and send the alarms to my phone. This script also includes clearing those notifications:

 alias: Grafana MQTT Alert
 description: ""
 triggers:
   - trigger: mqtt
     topic: grafana/alerts
 conditions: []
 actions:
   - repeat:
       sequence:
         - choose:
             - conditions:
                 - condition: template
                   value_template: "{{ repeat.item.status ==  \"firing\" }}"
               sequence:
                 - action: persistent_notification.create
                   metadata: {}
                   data:
                     title: "{{ repeat.item.annotations.summary }}"
                     message: >-
                       Name: {{ repeat.item.labels.alertname }} <BR> Message: {{
                       repeat.item.annotations.message }} <BR> Host: {{
                       repeat.item.labels.host }} <BR> Labels: {{
                       repeat.item.labels }} <BR> Annotations: {{
                       repeat.item.annotations }}<BR> Values: {{
                       repeat.item['values'] }}<BR> Since: {{
                       repeat.item.startsAt }}<BR> Silence: {{
                       repeat.item.silenceURL }}
                     notification_id: "{{ repeat.item.fingerprint }}"
                 - data:
                     title: "{{ repeat.item.annotations.summary }}"
                     message: >-
                       Name: {{repeat.item.labels.alertname }} <BR>  Message:
                       {{repeat.item.annotations.message }} <BR> Host: {{
                       repeat.item.labels.host }} <BR>  Values: {{
                       repeat.item['values'] }}<BR> Since: {{
                       repeat.item.startsAt }} <BR> Labels: {{ repeat.item.labels
                       }} <BR>  Annotations: {{ repeat.item.annotations }}<BR>
                     data:
                       channel: GrafanaAlert
                       importance: high
                       tag: "{{ repeat.item.fingerprint }}"
                       actions:
                         - action: URI
                           title: Silence
                           uri: "{{ repeat.item.silenceURL }}"
                         - action: URI
                           title: Panel
                           uri: "{{ repeat.item.panelURL }}"
                   action: notify.mobile_app_pixel_7_pro
             - conditions:
                 - condition: template
                   value_template: "{{ repeat.item.status ==  \"resolved\" }}"
               sequence:
                 - action: persistent_notification.dismiss
                   metadata: {}
                   data:
                     notification_id: "{{ repeat.item.fingerprint }}"
                 - data:
                     message: clear_notification
                     data:
                       channel: GrafanaAlert
                       importance: high
                       tag: "{{ repeat.item.fingerprint }}"
                   action: notify.mobile_app_pixel_7_pro
       for_each: "{{ trigger.payload_json.alerts }}"
 mode: parallel
 trace:
   stored_traces: 30
 max: 10