Failure for automation to write to file

What is the proper way for an Automation to write to a file? I have been bashing my head trying to get it to work, and netiher myself or Chatgpt (not saying much) can figure this out. I have a custom android app which generates alerts. Eventually I will do something about them but for now I just want to log the occurence and title.

The sensor works, it collects the attributes and the automation is triggered however the automation cannot write the file. I tried writing to /config/www/notification.log or /homeassistant/www.notification.log and even tried under my own folder which had 777 permissions /mylogs/notification.log. In all cases, no file was present. I tried also to push it via a script with no result.

configuration.yaml

shell_command:
  log_notification: 'echo "{{ message }}" >> /homeassistant/www/notification_log.txt'

scripts.yaml

log_last_notification:
  sequence:
    - service: shell_command.log_notification
      data_template:
        message: >
          {{ now().strftime('%Y-%m-%d %H:%M:%S') }} - 
          Title: {{ state_attr('sensor.pixel_8_last_notification', 'android.title') }}

Automation

alias: Alert Notification
description: Logs the alert notification 
trigger:
  - platform: state
    entity_id:
      - sensor.pixel_8_last_notification
    attribute: post_time
action:
  - action: script.log_last_notification
    data: {}
mode: single

Any suggestions?

Any errors on screen or in the logs?

Also, did you include an allowlist_external_dirs in your configuration.yaml?

For example, I save some data to files in my config folder:

  allowlist_external_dirs:
    - "/config"

I imagine you’d have to add - “/config/www” but I’m not positive that’s the right syntax.

is there a reason you must use shell_command?

the more “built in” way is using notify.send_message to a file notify entity.

take a look at this:

I went with shell_command because the notify.send_message didn’t work either. I read that post and updated my settings but still no file present and no errors. The entity has updated data, the automation timestamp does show it triggered but no file. Not my day :stuck_out_tongue:

configuration.yaml

notify:
  - name: log_file
    platform: file
    filename: /homeassistant/www/notification.txt
    timestamp: false

Automation

alias: Alert Notification
description: Logs the alert notification
trigger:
  - platform: state
    entity_id:
      - sensor.pixel_8_last_notification
    attribute: post_time
action:
  - target:
      entity_id: notify.log_file
    data:
      message: >
        {{ now().strftime('%Y-%m-%d %H:%M:%S') }} - 
          Title: {{ state_attr('sensor.pixel_8_last_notification', 'android.title') }}
    action: notify.send_message
mode: single

did you add the parent directory of the file to the allowlist?

From this doc page:

Yes I have tried to /mylogs and even to within the homeassistant folder with no luck with just:

homeassistant:
  allowlist_external_dirs:
    - mylogs/

it got pretty annoything so I just ended up putting it into another entity and logged it in memory so I can at least see it as a card on the dashboard.

input_text:
  last_notification_log:
    name: Last Notification Log
    initial: ''
    max: 24000

I’ll leave it for now. Once I know how to proceed further I’ll try to figure out this log to file thing. Thank you everyone for the help!