[How To] Automation for Historical Log for All Notifications, and Dashboard Card

I use notifications for many things : person location changes, home activities (door/window, locks, security panel, lighting scenes, etc)

I like to see a Historical Log of what triggered, and when but unfortunately the HA notification doesnt make that easy.

This code only requires 1 helper and 1 automation; easy to ‘see’ it exists, no ‘hidden’ templates sensors in the config.

The included code also writes a Persistent Notification. You can remove that code, or use a template to include/exclude notifications as you wish.

enjoy

-- Automation --

alias: Log All Notifications to Helper
description: Stores All Notifications in input_text.log_all_notifications and creates a persistent notification
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: notify
actions:
  - variables:
      notif_data: "{{ trigger.event.data.service_data }}"
      notif_service: "{{ trigger.event.data.service }}"
      notif_title: "{{ notif_data.title | default('Notification') }}"
      notif_message: >-
        {{ notif_data.message | default(notif_data.title | default('No content')) }}
      notif_target: |-
        {% if notif_service == 'notify' %}
          All devices
        {% elif 'mobile_app_' in notif_service %}
          {{ notif_service.replace('mobile_app_', '') | replace('_', ' ') | title }}
        {% else %}
          {{ notif_service | replace('_', ' ') | title }}
        {% endif %}
      timestamp: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
      full_message: "{{ timestamp }} - {{ notif_title }}: {{ notif_message }} → {{ notif_target }}"

  - service: input_text.set_value
    target:
      entity_id: input_text.log_all_notifications
    data:
      value: "{{ full_message }}"

  - service: persistent_notification.create
    data:
      title: "Logged Notification"
      message: "{{ full_message }}"

mode: queued


-- Dashboard Card : create a Helper for input_text.log_all_notifications --

type: custom:logbook-card
entity: input_text.log_all_notifications
show:
  state: true
  duration: false
  start_date: true
  end_date: false
  icon: false
  separator: false
  entity_name: true
desc: true

4 Likes

That’s pretty cool, especially on iOS where the device has no PN history. It happens to me often that as I’m swiping one, a new one comes in and now I’ve cleared that instead of the one I intended.

1 Like

Glad it helps you. I also see that issue. I have used Pushover for many years having a clean (not verbose), single and easy to access log… I miss a lot. I like that the HA notification system has nice Actionable buttons in the notifications. But the system needs some critical improvements. Not sure I can make workaround for the other issues.

I implemented this last night, but tweaked it a bit to eliminate the helper and not to display the location update and clear notification messages (just my preference).

template:
  - trigger:
      - platform: event
        event_type: call_service
        event_data:
          domain: notify
        variables:
          notification_data: "{{ trigger.event.data.service_data }}"
          notification_service: "{{ trigger.event.data.service }}"
          notification_title: "{{ notification_data.title | default('Notification') }}"
          notification_message: >-
            {{ notification_data.message | default(notification_data.title | default('No content')) | trim }}
          notification_target: |-
            {% if notification_service == 'notify' %}
              All devices/recipients
            {% elif 'mobile_app_' in notification_service %}
              {{ notification_service.replace('mobile_app_', '') | replace('_', ' ') | title }}
            {% else %}
              {{ notification_service | replace('_', ' ') | title }}
            {% endif %}
          timestamp: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
          full_message: "{{ timestamp }} - {{ notification_title }}: {{ notification_message }} → {{ notification_target }}"
    condition:
      - condition: template
        value_template: "{{ notification_message not in ['request_location_update', 'clear_notification'] }}"
    action:
      - service: persistent_notification.create
        data:
          title: "Logged Notification"
          message: "{{ full_message }}"
    sensor:
      - name: Last Notification
        icon: mdi:email-alert-outline
        state: "{{ full_message }}"