Automation triggered by specific file modified

Hello,

I am trying to setup an automation that would be triggered when a specific file is modified.

First usecase that I am trying to implement is to have a real time copy of /config/home-assistant.log file, so that I can decide of the retention time. Reason is that I run HassOS on RPI, and whenever a crash occurs (once every few days), I currently lose the logs during restart so I cannot investigate the crash root cause.

To do so, I added the following in my configuration.yaml:

homeassistant:
  allowlist_external_dirs: 
    - '/config/'
folder_watcher:
  - folder: /config
    patterns:
      - 'home-assistant.log'

And then, to check if it works, I created a simple automation like this:

- id: 'id'
  alias: alias
  description: ''
  trigger:
  - platform: event
    event_type: folder_watcher
    event_data:
      event_type: modified
  condition: []
  action:
  - service: notify.mobile_app_xxx
    data:
      title: TEST
      message: LOG MODIFIED
  mode: single

Unfortunately, this automation is never triggered…
Do you notice something wrong in what I did ?
Thanks for the help !

Try replacing this:

With this:

homeassistant:
  allowlist_external_dirs:
    - /config

Hello,
Thanks for the quick reply but unfortunately no, it didn’t change anything :frowning:

What you have looks correct to me according to the docs.

I’m assuming you restarted after adding the folder_watcher config?

Things to try:

  1. Trigger the automation manually (developer tools / services, automation.trigger) to test your message action.
  2. Listen to the event in developer / tools / events when you change the log.

Try the pattern matching:

    patterns:
      - '*.log'

Yes I restart after every modification, and I also tried to exec the automation to ensure that notification works fine and it does !

Regarding the devtool, I tried to use it but not sure what I should listen to, and it is linked to your second advice regarding pattern matching —> I don’t understand how to define multiple watchers as they have no id or name, so I don’t know how to id them precisely in the listener or in an automation (meaning, what if I have multiple log files in the same folder, what happens ?)
Tell me if I am not clear !

Then if any of them change the folder watcher will generate an event. I know it is probably not ideal when you want to watch just one file, I was just interested from a debugging perspective.

Thank you !
So apparently it was a star issue, both *.log and *home-assistant.log work !
This does however highlight another issue (maybe I am dealing wrongly with original need): automation run = new logs … and new logs = automation run … hello infinite loop :smiley: I exploded the 300 notif limit in seconds before I could stop the automation ^^
Any idea to prevent a specific automation from writing logs ? Or maybe an idea on another way of dealing with this log retention issue ?
Thanks again for the help !

Haha. Oops.

Unfortunately I can’t think of a way to prevent it.

Maybe instead of the folder watcher, use this event trigger:

    trigger:
    - platform: event
      event_type: system_log_event
      event_data:
        level: WARNING
    - platform: event
      event_type: system_log_event
      event_data:
        level: ERROR

You could write {{ trigger.event.data.message }} to a file that would be persistent. More here: https://www.home-assistant.io/integrations/system_log/#events

Well, thank you for this proposition.
I tried it but unfortunately, I encountered so many issues when trying to implement something like that, that it’s becoming to complex for me ! There are issues with file rotation, file retention, message containing quotes, mounting /config folder, message truncated, … … …)
I think I’ll just hope that this feature request gets implemented in a near future Better logging !!!
But it was nice to play with something new, I learned a few things in the process, so that’s cool !!
Thanks again for your time and help on this topic !