Trying to use notify.file but getting error of invalid service data required key not provided

Continuing the discussion from How to use notify.file:

Trying to figure out why I’m getting the error “Invalid service data for notify.file_write: required key not provided @ data[‘message’]. Got None”

notify:
  - name: file_write
    platform: file
    filename: output.txt
    timestamp: true

then inside Node-Red I’m calling this service with the test message of:

[
  {
    "data": {
      "message": " Testing output to file ",
      "title": " Did this work? "
    }
  }
]

Updated, found out what I had wrong… the JSON doesn’t use the “data” like Pushover or Twilio does (that’s what I was starting from to test it out. JSON should look like this:

{
  "message": " Testing output to file ",
  "title": " Did this work? "
}

since you revived this topic :wink:

are system notifications created in the file, and where it this file located?

In my setup nothing happens, while i have regular notifications going on…

So it depends on what you’re trying to capture into the txt file. In my case, I want to capture when my electric car starts charging and when it completes at 100% so I can eventually log my average charging time on a graph or something later. Here’s how mine works:

First we have a notify setup, I’m calling mine “file_write” which outputs to output.txt (the output.txt will show up in your config folder once you run an automation the first time and it writes to it)

notify:
  - name: file_write
    platform: file
    filename: output.txt
    timestamp: true

Next I have an automation setup (I’m using Node-Red but you can do it in Yaml). Here’s a sample of one automation if I was doing mine in Yaml. Note: I’d need to have one automation for when charging starts and one automation when charging stops.

- alias: BMW Charging
  trigger:
#### put your desired trigger here, mine is inside Node-Red
  action:  
	service: notify.file_write
    data:
      message: ' i3 Charging{{ states.sensor.i3_94_charging_status.state }} '
      title: ' BMW '

once this is triggered the first time the file is created and I can see it via Samba Share inside the Config folder

If you want to share what you’re trying to do I can try to help you build it as well but thought I’d show you mine to help you see the part where I’m capturing the current state of the charging sensor inside my message.

HI, thanks!

I was simply trying to get all notifications to the file being created in the setup, a bit like the system-log, but then for all and only notifications:

# https://home-assistant.io/components/notify.file/
- name: notifications
  platform: file
  filename: /config/notifications
  timestamp: True

Do I need to call this in each notify myself, or can the system do that automatically (as I was led to believe by the description on thedocumentation

I would change the filename line ( you don’t need to give it the path and you need to have .txt at the end) it will automatically go into the config folder for you when it executes.

- name: notifications
  platform: file
  filename: notifications.txt
  timestamp: True

So you’re saying that you want the file to catch every single notification your system sends? You’d probably need to call the service notify.notifications with each and every one of your automatons that are sending out notifications to you.

too bad the system doesn’t take of that itself. otoh, it might be consistent with the platform notify…

maybe i can create an automatic forward of all notifications, somewhere along the lines of this automatic persistent notification forwarding?

- alias: 'Forward persistent notifications'
  id: 'Forward persistent notifications'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: persistent_notification
      service: create
  condition:
    condition: state
    entity_id: input_boolean.notify_notify
    state: 'on'
  action:
    service: notify.notify
    data_template:
      message: >
        {% set message = trigger.event.data.service_data.message %}
        {% if 'invalid authentication' in message or 'login attempt' in message %}
          {{ message }}: http://www.ip-tracker.org/locator/ip-lookup.php?ip={{ message.split('from ')[1] }}
        {% else %}
          {{ message }}
        {% endif %}

but id need some guidance in doing so…

- alias: 'Forward notifications to notify.notifications'
  id: 'Forward notifications to notify.notifications'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: notify
      service: 
        - notify.notify
        - notify.presence
        - notify.motion
        - notify.developing
  condition:
    condition: state
    entity_id: input_boolean.notify_notifications
    state: 'on'

  action:
    service: notify.notifications # thats what the name is set to right now
    data_template:
      message: >
        {% set message = trigger.event.data.service_data.message %}
          {{ message }}

Yeah I’m not smart enough to help you with that, I’ve switched to Node-Red and it’s helped me a ton with writing automations (I’m not a YAML expert at all, have had to tinker my way through things to get where I’m at, switching to Node-Red has made making complex automations easier for me but I’m still learning.) You could test out the new notify feature to the text document to make sure it works by adding it to one of your automations then maybe hit someone smarter than me up for more detailed help about forwarding all notifications to it.

1 Like

cool, thanks for making me understand a bit better…:wink: i will do as you suggest and have a feeling @petro knows his way in making the automation to forward all notifications to the notify.file component.

Cheers,
Marius

well, starting to work, at least with an individual automation/notification:

# https://home-assistant.io/components/notify.file/
- name: filed_notifications
  platform: file
  filename: filed_notifications.txt
  timestamp: True

now got to get the forward_notifications automation to work, but its complaining the data_template trigger is undefined…

- alias: 'Forward notifications to notify.notifications'
  id: 'Forward notifications to notify.notifications'
  initial_state: 'on'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: notification
      service: notify.notify
  condition:
  action:
    service: notify.filed_notifications # thats what the name is set to right now
    data_template:
      message: >
        {% set message = trigger.event.data.service_data.message %}

will work it out eventually…
thanks for your help!

solved: How to forward all notifications to notify.file