How to send an email with HA error details

I’m trying to get Home Assistant 2022.5.4 to email me event logs, including the contents of the log message. I can get it to send me an email when a log entry with this automation, but so far I can’t get the actual log message sent to me.

This sends me an email, but just tells me to check the log manually rather then emailing me the log message.

alias: Notifications System Log All
description: ''
trigger:
  - platform: event
    event_type: system_log_event
condition: []
action:
  - service: notify.email
    data:
      title: HA log entry made
      message: 'Check the log'
mode: single

When I use the format for the log message on the system log documentation page as below I get a message in my logs as follows.

ERROR (MainThread) [homeassistant.components.automation.2_system_log_message_test] Error while executing automation automation.2_system_log_message_test: template value should be a string for dictionary value @ data[‘message’]

alias: Notifications System Log All
description: ''
trigger:
  - platform: event
    event_type: system_log_event
condition: []
action:
  - service: notify.email
    data:
      title: HA log entry made
      message: '{{ trigger.event.data.message }}'
mode: single

I can see the event format as follows below, but I don’t know enough about HA yet to know how to get the message into the email. Can anyone help?

{
    "event_type": "system_log_event",
    "data": {
        "name": "homeassistant.components.automation.a2_generate_error",
        "message": [
            "A2 Generate Error: Error executing script. Invalid data for call_service at pos 1: must contain at least one of temperature, target_temp_high, target_temp_low."
        ],
        "level": "ERROR",
        "source": [
            "helpers/script.py",
            1718
        ],
        "timestamp": 1652691090.1839242,
        "exception": "",
        "count": 1,
        "first_occurred": 1652691090.1839242
    },
    "origin": "LOCAL",
    "time_fired": "2022-05-16T08:51:30.192725+00:00",
    "context": {
        "id": "0180cc10bb10bf1c491cfc649fbe6431",
        "parent_id": null,
        "user_id": null
    }
}

You are really close. Message is an array. Index it and you should be good.


trigger.event.data.message[0]

1 Like

Thanks @PeteRage! That worked perfectly. For anyone else trying to do this later, here’s the working code.

In configuration.yaml you have to enable “fire event”, and you need to define your email server.

# Allow system log entries to fire triggers. Max_entries defined how many logs the system event log holds, default is 50
system_log:
  max_entries: 500
  fire_event: true

# Email notifications
notify:
  - name: email
    platform: smtp
    server: smtp.emailhost.com
    port: 587
    timeout: 15
    sender: [email protected]
    encryption: starttls
    username: [email protected]
    password: password
    recipient:
      - [email protected]
    sender_name: Home Assistant

Then the working automation looks like this

alias: Send errors to System Log
description: ''
trigger:
  - platform: event
    event_type: system_log_event
condition: []
action:
  - service: notify.email
    data:
      title: HA log entry made
      message: '{{ trigger.event.data.message[0] }}'
mode: single
2 Likes

I added the automation and did a trial run but received error message

Event 2 fired 11:44 PM:
{
    "event_type": "system_log_event",
    "data": {
        "name": "homeassistant.components.websocket_api.http.connection",
        "message": [
            "[139811763724832] Error handling message: Error rendering data template: UndefinedError: 'trigger' is undefined (unknown_error)"
        ],
        "level": "ERROR",
        "source": [
            "components/websocket_api/connection.py",
            146
        ],
        "timestamp": 1655707487.242492,
        "exception": "",
        "count": 1,
        "first_occurred": 1655707487.242492
    },
    "origin": "LOCAL",
    "time_fired": "2022-06-20T06:44:47.244159+00:00",
    "context": {
        "id": "01G5ZXPK0CPT3TCW3J64XJK1WQ",
        "parent_id": null,
        "user_id": null
    }
}
Event 1 fired 11:44 PM:
{
    "event_type": "system_log_event",
    "data": {
        "name": "homeassistant.helpers.script.websocket_api_script",
        "message": [
            "websocket_api script: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'trigger' is undefined"
        ],
        "level": "ERROR",
        "source": [
            "helpers/script.py",
            1718
        ],
        "timestamp": 1655707487.2416089,
        "exception": "",
        "count": 1,
        "first_occurred": 1655707487.2416089
    },
    "origin": "LOCAL",
    "time_fired": "2022-06-20T06:44:47.244084+00:00",
    "context": {
        "id": "01G5ZXPK0C9SAVM0W70T167MVM",
        "parent_id": null,
        "user_id": null
    }
}
Event 0 fired 11:44 PM:
{
    "event_type": "system_log_event",
    "data": {
        "name": "homeassistant.helpers.template",
        "message": [
            "Template variable error: 'trigger' is undefined when rendering '{{ trigger.event.data.message[0] }}'"
        ],
        "level": "ERROR",
        "source": [
            "helpers/template.py",
            1862
        ],
        "timestamp": 1655707487.241342,
        "exception": "",
        "count": 1,
        "first_occurred": 1655707487.241342
    },
    "origin": "LOCAL",
    "time_fired": "2022-06-20T06:44:47.243921+00:00",
    "context": {
        "id": "01G5ZXPK0BHYYGGH3SESHPJER2",
        "parent_id": null,
        "user_id": null
    }
}

Not sure what I did wrong.

I followed this example with my settings but I get bombarded by emails. What am I doing wrong?

Could be your error levels. What’s in the error messages? Please post a few examples.

1 Like

I had to disable the automation for today. I’ll reenable it in a bit and post the errors here.
Thank you for replying.

You could look in the home assistant log and post that, that’s where the messages come from. You can access the log in the console, or with ssh I get it from somewhere like ~/homeassistant/.homeassistant/homeassistant.log

Sorry, I just found time to sit and look at this. I had to redo the Gmail app password because it wasn’t working. its working now but as soon as I restarted HA I got 16 emails.

HA log entry made!

‘Error handling request’
HA log entry made!

‘Send errors to system log: Already running’
HA log entry made!

‘Send errors to system log: Already running’
HA log entry made!

‘Send errors to system log: Already running’
HA log entry made!

‘Send errors to system log: Already running’
HA log entry made!

‘Send errors to system log: Already running’
HA log entry made!

‘Send errors to system log: Already running’
HA log entry made!

‘Send errors to system log: Already running’
HA log entry made!

‘Send errors to system log: Already running’
HA log entry made!

‘Send errors to system log: Already running’
HA log entry made!

‘Send errors to system log: Already running’
HA log entry made!

'HomeKit Front Door Doorbell cannot startup: entity not available: {‘include_domains’: [], ‘exclude_domains’: [], ‘include_entities’: [‘camera.front_door_doorbell’],

This is my yaml:

    platform: smtp
    server: "smtp.gmail.com"
    port: 587
    timeout: 15
    sender: "[email protected]"
    encryption: starttls
    username: "[email protected]"
    password: !secret gmail_pswd
    recipient:
      - "[email protected]"
    sender_name: Home Assistant```
this is my automation:
 alias: Send errors to system log
  description: 'HA system log errors emailed '
  trigger:
  - platform: event
    event_type: system_log_event
    context: {}
  condition: []
  action:
  - service: notify.gmail
    data:
      title: HA log entry made!
      message: '{{ trigger.event.data.message[0] }}'
  mode: single

Try changing mode to queued vs single.

1 Like

I don’t have any idea here sorry. I’d probably try to fix up the HomeKit error.

I have this in my configuration.yaml to reduce the errors sent to me.

logger:
  default: error
  logs:
    # log level for HA core
    homeassistant.core: info
    custom_components.smartir.climate: debug
    homeassistant.components.openweathermap.weather_update_coordinator: fatal
    homeassistant.components.tplink.coordinator: fatal
    homeassistant.components.brother: fatal
    homeassistant.components.broadlink.updater: fatal

will try that and report back. Thank you!

I’m using this automation with the following conditions to avoid spamming:


  condition:
  - not:
    - |-
        {%- set x = trigger.event.data.message -%}
        {%- set a = ":0:0 Script error." -%}
        {%- set b = "[Google Nest Mini(192.168.178.38):8009]" -%}
        {%- set c = "Heartbeat timeout" -%}
        {{  a in x or b in x or c in x }}

    - |-
        {% set last = state_attr(this.entity_id, 'last_triggered') %}
        {{ last is not none and now() - last > timedelta(seconds=5) }}

1 Like

I haven’t seen that syntax before, tried searching in the docs, couldn’t find a reference.

Is that basically shorthand syntax for “or”?

In many programming languages the | symbol means “or”.