Automation using SYSTEM_LOG_EVENT

Pretty new to this so please be kind! :slight_smile:

I am trying to capture the “MESSAGE” element of the SYSTEM_LOG_ERROR when an error is found in the log and input this into the message body of an email as below;

 alias: 'Notify Error in Hassio Log'
  id: ErrorinHassioLog
  trigger:
    platform: event
    event_type: system_log_event
    event_data:
      level: ERROR
  action:
    - service: notify.notifier_name
      data_template:
        message: Error in Hassio Log found
        title: Madden - Home Assistant Server
    - service: notify.send_email
      data:
        title: Hassio Error Log Found
        message: '{{ trigger.event.data.message }}'

The problem I am seeing is the last line I get this error;

Error while executing automation automation.notify_error_in_hassio_log. Error rendering template for call_service at pos 2: UndefinedError: 'trigger' is undefined

It’s probably so simple but I’m not getting it!

Try changing data to data_template in that last step.

Oh my god! As simple as that!!!
Thank you @pnbruckner!
:sweat_smile::laughing::grinning:

1 Like

Hello,

I don’t mean to hijack your post but when I paste the exact same code you provided and I manually fire an event, it shows on the console but the automation is not triggered.

Here is my code:

- alias: Create notifications for all errors
  trigger:
    platform: event
    event_type: system_log_event
    event_data:
      level: ERROR
  action:
    service: persistent_notification.create
    data_template:
      title: Something bad happened
      message: "{{ trigger.event.data.message }}"

Then I manually fire this event through the developer tools :

message: Something went wrong
level: error
logger: mycomponent.myplatform

It shows in the console but the automation is not triggered and does not output any error either.

2021-02-08 10:41:07 ERROR (MainThread) [mycomponent.myplatform] Something went wrong

Any help or input would be greatly appreciated. Thanks

You need to explicitly enable it with the following in configuation.yaml:

system_log:
  fire_event: true