Truncate the home-assistant.log file Using a Shell Command

Smart Home

When my Home Assistant instance goes a couple weeks between restarts, the home-assistant.log file can grow to nearly a gigabyte in size if there are any integrations that are throwing error messages regularly. Even on an Intel NUC with a 128GB SSD this was making my snapshots/backups too large in size and using up all of my hard drive space (I run Home Assistant OS). I needed to find a way to automatically reduce the size of this file when it grew too large. I used the following approach.

  1. A command_line sensor to monitor the home-assistant.log file size
  2. A shell_command to truncate the log file to 100MB
  3. An automation to execute the shell command when the file grows to 200MB

File Size Sensor

Creating a sensor to monitor the size of the log file is pretty straightforward. Although there is a file_size sensor I went with a command_line sensor because it was easier to test and debug using the Reload Command Line Entities button in the Server Controls panel. This is the YAML code I used to get the file size in megabytes in my configuration.yaml file:

sensor:
  - platform: command_line
    name: Log File Size
    command: du -m home-assistant.log | cut -f1
    unit_of_measurement: MB
    value_template: "{{ value|int(0) }}"
    scan_interval: 3600

Shell Command

The shell_command to truncate the log file size took a some trial and error. Ultimately I created a bash script file that the shell_command executed so I could make changes to the script without restarting Home Assistant every time I wanted to try a different shell command to get things working right.

I created the shell script file using the Visual Studio Code Add-on, but you could do this however you edit YAML files in your current Home Assistant instance. I named it truncate_log.sh and saved it in the config folder for Home Assistant. The file contains the following script:

#!/usr/bin/env sh

truncate -s 100MB /config/home-assistant.log

Once the shell script file is created, you have to make it executable using the following command in the config directory: chmod +x truncate_log.sh. I did this via the Terminal/SSH Add-on. Then you can create the shell_command to call the bash script file in your configuration.yaml file:

shell_command:
  truncate_log: /config/truncate_log.sh

Automating Things

Finally, I created an automation that uses a numeric_state trigger to run the shell_command when the log file gets over 200MB in size. I did this via the UI, but the following YAML will work:

alias: 'Truncate Log File'
description: >-
  This truncates the home-assistant.log file to 100MB when it grows past 200MB
  to prevent it from ballooning in size between restarts.
trigger:
  - platform: numeric_state
    entity_id: sensor.log_file_size
    above: '200'
    for:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
action:
  - service: shell_command.truncate_log
mode: single

Comments

  1. Hi. Thanks for this script. How would you change script if files are two or more like this? -rw-r--r-- 1 root root 151753 Feb 6 01:35 home-assistant.log -rw-r--r-- 1 root root 176463 Feb 6 01:30 home-assistant.log.1 -rw-r--r-- 1 root root 0 Feb 6 01:31 home-assistant.log.faul So it cleas both of files?
    1. To change the bash script, I think you could just add another line for each additional file. You would also want to watch the file sizes of those files and update the automation with triggers for each file.
      #!/usr/bin/env sh
      
      truncate -s 100MB /config/home-assistant.log
      truncate -s 100MB /config/home-assistant.log.1
      truncate -s 100MB /config/home-assistant.log.faul
  2. Excellent blogs. I don't suppose your able to update your blog on Aeotec Water Sensor for the newer version of home assistant / z-wave JS or open comments on it? I'm struggling with being able to pass parameters with this new version (menu/parameters aren't the same) and the sensors just aren't reporting as they should (they never update regardless of the sensor being activated or not).
    1. I too would like to see an update on the z-wave as the UI has changed a lot since that blog post.
    2. I just posted an update to that Aeotec blog post. I believe you need to enable parameter 121 setting for "On Trigger: Send Binary Sensor Report" in the Z-Wave JS integration UI.

Comments are closed