Sensor for monitoring the logfile

This is a simple sensor for monitoring the home-assistant.log file. This can then be used for sending a notify.

  - platform: command_line
    command: 'grep ERROR /home/homeassistant/.homeassistant/home-assistant.log | tail -1'
    name: HAFail
1 Like

Rather like the file sensor

I dont understand how the file sensor could work with HA’s log. You need to filter out only the “ERROR”, how is that done? To use the filter functionality it requires the file to be formatted as JSON.

The value_template does not have to be JSON, it can be plain text - there is some documentation on templating here.

Alternatively, you could filter in the action just before sending a notification.

The advantage of using the FileSensor is that it will work on systems that do not use bash.

Yes, read that, but it still only JSON examples for processing incoming data. If you could provide me with an example of filtering non-JSON data I would appreciate it.

The filesensor returns the last line and depending on how often the filesensor is updated some error messages could be missed if there are a lot of other lines in the log file before the next update of the sensor. So filtering in the notification is not a good option.
My example could also miss error messages (if several during 60 seconds) but at least I would catch the last one. That would alert me that something is going on and I need to look into it.

Try something like this

homeassistant:
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: !secret latitude
  longitude: !secret longitude
  # Impacts weather/sunrise data (altitude above sea level in meters)
  # Guessed
  elevation: 42
  # metric for Metric, imperial for Imperial
  unit_system: metric
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: Europe/London


logger:

frontend:

history:

recorder:

sensor:
  - platform: file
    file_path: /home/homeassistant/test_config/home-assistant.log
    value_template: >
        {% if "ERROR"  in value  %} 
          {# If ERROR is in the incoming value, use that but truncate it #}
          {# as the maximum length of a state is 255 characters #}
          {{ value | truncate(255) }} 
        {% else %}
          {# Carry on using the current state of the sensor #}
          {{ states('sensor.file') }}
        {% endif %} 

Reading the documentation would imply that every line is read -

This is similar to do $ tail -n 1 sensor.txt on the command-line.

However, I did try to append a text file to the homeassistant.log just to see what happened, and it did not change state for every line, so that may be a problem, or it may be an invalid test.

Your test is valid and the tail -n 1 command only return the last line so I suspect only the last line is read by the file sensor.

Therefore i could miss errors in the log file depending on update frequency off the file sensor. So I stick with my grep solution for time being.

- platform: command_line
  name: Latest error
  command: "grep ERROR /config/home-assistant.log | tail -1 | cut -c-255"

I had to add the | cut -c-255 to not exceed the maximum length of the sensor. Otherwise the sensor gave errors itself.