History of Notifications

Checking notifications’ history log is useful, to check if you missed some critical notifications.

Please make the feature to save notifications history.
You could limit no. of notifications or those with “critical status” or unlimited with dependency from the entire HA database recorder history.

I use the file notification for a history of persistent_notifications.

notify:
  - platform: file
    name: pn_log
    filename: pn.log

automation:
  - alias: persistent_notification_log
    trigger:
      - platform: event
        event_type: call_service
        event_data:
          domain: persistent_notification
          service: create
    # condition:
    #   - condition: template
    #     value_template: >
    #       {{ trigger.event.data.service_data.notification_id != "config_entry_discovery" }}
    action:
      - service: notify.pn_log
        data_template:
          message: >
            {{ now().strftime('%Y%m%d %H%M%S') }} 
            {{ trigger.event.data.service_data.title }} / 
            {{ trigger.event.data.service_data.message }}

pn.log

20200515 040103 TPAD OS Updates / There are 5/5 ubuntu updates available.
20200515 100510 New Home Assistant Release / Home Assistant 0.110.0b2 is now available.
20200515 123626 HA 0.110.0b2 started
20200515 200002 Nexus TH / Sensor Timeout
20200516 232925  Test notification /  Please check your configuration.yaml.

Until someone will integrate a history into HA. :crossed_fingers:

5 Likes

How do you delete older entries (e.g. older than 7 days) from the file?

I don’t, just move or delete the file if it’s too big.

Hi @Jtarche I have just posted a way of triming the log file to the last 200 lines daily. In case you are still interested:

this won’t work for me, why? no file is created, I’ve searched for it everywhere…
not even if I include a path, like filename: /local/pn.log

Something in the logs?

Maybe you need to whitelist the /config directory in configuration.yaml.

homeassistant:
  ...
  allowlist_external_dirs:
    - /config

Probably not exactly what you want but you can kind of do this with traces. Make a script like this:

alias: Send and save notification
trace:
  stored_traces: 100 # Max number of notifications in "log"
fields:
  notifier:
    name: Notifier
    description: What comes after `notify.`
    required: true
    example: mobile_app_phone
    default: mobile_app_phone
    selector:
      select: # List any notifiers you use here
        - mobile_app_phone
  title:
    name: Title
    required: true
    selector:
      text:
  message:
    name: Message
    required: true
    selector:
      text:
        multiline: true
  data:
    name: Data
    required: false
    selector:
      object:
sequence:
  if: "{{ data is mapping }}"
  then:
    service: "notify.{{ notifier }}"
    data:
      title: "{{ title }}"
      message: "{{ message }}"
      data: "{{ data }}"
  else:
    service: "notify.{{ notifier }}"
    data:
      title: "{{ title }}"
      message: "{{ message }}"

Then any place you want to send a notification and have it “logged” use this script instead of directly doing a notify.whatever. The data of the service call should be identical.

If you want to see the log then go to the traces of this script. Once it hits 100 traces (or whatever you set stored_traces to) then it will start rotating out the oldest ones.

Kind of goofy I know but figured Id throw it out there.

1 Like

thanks, it worked. I’m not sure that this was it, as I noticed also that notify.persistent_notification doesn’t log there, just persistent_notification.create
seems logic now, as it uses domain: persistent_notification :man_facepalming:
so I had to change all my automations…

Sorry I get the message:
Message malformed: expected a dictionary for dictionary value @ data[‘fields’][‘notifier’][‘selector’]

Infact it is totally not logical to delete the notification after being opened. We should be able to see the history in the same window

3 Likes

Hi, i know this is an old post, but can you please help me make this work?

I have never - as in ever - done coding in raw yaml directly in home assistant, so i’m quite confused.
I directly copied your code into my automation.yaml file in the config folder and it throws a lot of errors that im not sure what to do with.

Is it not possible for me to directly copy what you posted?