PagerDuty and RESTful Notifier

I’m trying to use the RESTful Notifications platform to trigger an event with the PagerDuty alerts service from within Home Assistant.

The PagerDuty REST API wants data sent to the endpoint https://events.pagerduty.com/v2/enqueue in the json structure:

{
  "routing_key": "*************************",
  "event_action": "trigger",
  "payload": {
    "summary": "Example summary data",
    "source": "homeassistant.local",
    "severity": "info"
  }
}

I have this working with the RESTful Command integration but the message is not customisable at this point

rest_command:
  pagerduty_command:
    url: https://events.pagerduty.com/v2/enqueue
    method: POST
    payload: '{"routing_key":"*******************","event_action":"trigger","payload":{"summary":"Example summary data","source":"homeassistant.local","severity":"info"}}'

I would like to use the Notifications integration so I can send messages with the alerts. I have tried this:

# NOT WORKING (DON'T COPY THIS)
notify:
  - name: pagerduty_notification
    platform: rest
    resource: https://events.pagerduty.com/v2/enqueue
    method: POST
    message_param_name: payload.summary
    data:
      routing_key: **************************
      event_action: trigger
      payload:
        # summary: Example notification data
        source: homeassistant.local
        severity: info

But this is not working currently. I get the error Client error. Response 400: Bad Request: in the logs when I trigger it. Looking at the source code for homeassistant/components/rest/notify.py it looks like the request is constructed by adding {"payload.summary":"message text"} into the payload data and then sending that as a JSON.

How can I achieve my goal of posting PagerDuty alerts with customisable text from a Home Assistant automation?
Is there some way to log the REST requests so I can see how the RESTful Notifier is formatting the JSON request data? Maybe that will make it easier to debug.

Here is my solution, for the record:
In my configuration.yaml:

rest_command:
  pagerduty_message:
    url: https://events.pagerduty.com/v2/enqueue
    method: POST
    payload: >-
      {
        "routing_key":"********************************",
        "event_action":"trigger",
        "payload":
        {
          "summary":"{{ message }}",
          "source":"homeassistant.local",
          "severity":"info"
        }
      }

And I call the service like this to customise the PagerDuty notification message:

- service: rest_command.pagerduty_message
  data:
    message: Custom text for message here.

(Note that the routing key must be replaced in with your own API key)

Maybe I’ll try writing an integration at some point!

3 Likes