Get PagerDuty notifications to flash your bedside lamp

This article explains how I used AWS SES, AWS SNS, ngrok, nginx, Home Assistant and a TP-Link Kasa Smart TP-Link (KP400) to flash a beside lamp when I get paged on PagerDuty, before it falls back to a sound notification on my phone.

Note: there are probably simpler methods. I didn’t have access to the PagerDuty API and had to rely on emails for notifications.

Overview

Here’s the high-level configuration:

Architecture diagram

Prerequisites

The instructions below assume that:

Home Assistant Configuration

  1. Configuration > Automations > Add Automation > Start with an empty automation

  2. Click on the top right three dots and press “Edit as YAML”.

  3. You can use this sample configuration. It might be suboptimal (didn’t get to loops), it’s the first one I create. Replace something by your webhook path. This should be enough to expose a webhook that will trigger a lamp to flash with 1 second delay between turn on and turn off.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    alias: PagerDuty Notification
    description: ''
    trigger:
    - platform: webhook
        webhook_id: something
    condition: []
    action:
    - type: turn_on
        device_id: 1234
        entity_id: switch.kasa_smart_plug_4d21_0
        domain: switch
    - delay: 1
    - type: turn_off
        device_id: 1234
        entity_id: switch.kasa_smart_plug_4d21_0
        domain: switch
    - delay: 1
    - type: turn_on
        device_id: 1234
        entity_id: switch.kasa_smart_plug_4d21_0
        domain: switch
    - delay: 1
    - type: turn_off
        device_id: 1234
        entity_id: switch.kasa_smart_plug_4d21_0
        domain: switch
    - delay: 1
    mode: single
    

At this point, you should be able to send a request to http://homeassistant:8123/api/webhook/something to trigger the lamp to flash.

Nginx container on the Home Assistant Raspberry Pi

I wrote this configuration file to /mnt/data/docker/nginx/nginx.conf:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    server {
        server_name _;
        location /api/webhook {
            proxy_pass http://127.0.0.1:8123/api/webhook;
        }
    }
}

And then started a container with:

At this point, you should be able to send a request to http://homeassistant:80/api/webhook/something to trigger the lamp to flash.

ngrok

I downloaded ngrok on /mnt/data, prepared a sample configuration, and started it:

1
2
3
4
5
6
authtoken: ABCD1234
tunnels:
  hass:
    proto: http
    addr: 80
    subdomain: YOUR_SUBDOMAIN

At this point, you should be able to send a request to https://YOUR_SUBDOMAIN.ngrok.io/api/webhook/something to trigger the lamp to flash.

AWS SES and SNS configuration

  1. Create a new SNS topic.
  2. Follow this documentation to setup SES to receive emails. At step 3, configure the action to publish to your SNS topic instead of S3.
  3. Subscribe your webhook URL https://YOUR_SUBDOMAIN.ngrok.io/api/webhook/something to the SNS topic.

At this point, you should be able to send an email to x@y.xyz to trigger the lamp to flash.

PagerDuty

Add the extra email address (example taken from above: x@y.xyz) in your PagerDuty user profile. Configure notifications to trigger on this email address first if you want the lamp to flash before anything else like I did.