Slack Notifications and Customization

I went down the rabbit hole of trying to send “fancy” notifications via Slack that were color-coded and formatted properly. The best I could find was this article that showed the format in JSON. The overall documentation for this was a bit spotty.

Basically, instead of something like this:

HA-1

I wanted something like this:

HA-2

Anyways, here’s what the configuration looks like in an automation:

action:
    service: notify.default_alert
    data_template:
      message: ""
      data: 
        attachments:
        - color: >
            #800040
          title: >
            Motion While Everyone Is Away. 
          text: |
            Sensor: {{ trigger.to_state.attributes.friendly_name }}
            Time: {{now().strftime("%c")}}

Along those same lines, I also wanted to send the occasional notification with a quick view from the front camera included. The documentation for this was a bit better, but I figured I’d share this too. I haven’t bothered flipping it over to the above format since I’m happy with the result.

- service: notify.default_alert
      data_template:
        title: >
          Doorbell was pressed
        message: >
          Doorbell was pressed on {{now().strftime("%c")}}.
        data:
          file:
            path: /home/homeassistant/.homeassistant/www/doorbell_01.gif

Hope this helps!

5 Likes

There’s a hidden trick for converting functional JSON into YAML for cases where you have it working from the dev tool:

  1. In HA go to configuration > scripts > + to add new script
  2. enter in your functioning JSON for the service call
  3. save the script – it will have saved in your scripts.yaml or configuration.yaml properly formatted!
3 Likes