How to log to my own log file?

HI,
I would like to be able from scripts or automations to write into a file my own log details.
Ideally I would like something similar to the logging from python including RotatingFileHandler - something like : I define in the configurations.yaml some settings like : file name, file path, max file size, backup count of the files (e.g.: log.1, log.2, log.3 … etc) eventually to define some generic prefixes, like date/time, and then to have a service option like: log_to_my_custom_file with my string …

Is there anything like this existing?
Does it worth to try to make my own python script and then to … somehow … use it for loging by sending log messages to it ? (Is this possible?)

There’s the file notifier integration. I used to use it to log CSV data before I had InfluxDB set up.

1 Like

Here is a quick rundown on how to create and use your own logs.

  1. Set up a File notifier.
# File support
- platform: file
  name: critical_event
  filename: www/log_files/critical_events.txt
  timestamp: false
#
- platform: file
  name: diagnostics
  filename: www/log_files/diagnostics.txt
  timestamp: false
#

Your actual text files will be located in 'config/www/log_files/file_name.txt

To log data to that file do the following:

    # Critical Event Log start
    - service: script.turn_on
      entity_id: script.notify_critical_events_log
      data_template:
        variables:
          var1: "circuit_breaker_tripped"
          var2: >-
            Breaker number {{ trigger_message }} was triggerd by {{ triggered_by }} to status of {{ status }}.
    # Critical Event Log End

Then create a script to pass any number of variables to that actually writes the data.

#################################################################
#  Critical Events
#################################################################
notify_critical_events_log:
  mode: queued
  sequence:
  - service: counter.increment
    target:
      entity_id: counter.c_critical_events_log_entries
  - service: notify.critical_event
    data_template:
      message: >-
        <br>[{{ (states("counter.c_critical_events_log_entries")) }}] CRITICAL! {{now().strftime("%m/%d/%Y %H:%M:%S.%f")}}{{ "\n" -}}
        {% if var1 is defined and var1 != none -%}
        <br> - {{ var1 }} {{ "\n" -}}
        {% endif -%}
        {% if var2 is defined and var2 != none -%}
        <br> - {{ var2 }} {{ "\n" -}}
        {% endif -%}
        {% if var3 is defined and var3 != none -%}
        <br> - {{ var3 }} {{ "\n" -}}
        {% endif -%}
        {% if var4 is defined and var4 != none -%}
        <br> - {{ var4 }} {{ "\n" -}}
        {% endif -%}
        {% if var5 is defined and var5 != none -%}
        <br> - {{ var5 }} {{ "\n" -}}
        {% endif -%}
        {% if var6 is defined and var6 != none -%}
        <br> - {{ var6 }} {{ "\n" -}}
        {% endif -%}
        <br>-------------------------- {{ "\n" -}}
#

Additionally you can create other automations to reset the file, counters or other actions at a set time or once acknowledging.
Just ask if you need more help or some of my other code related to personal logging. I use A LOT of it and I feel like it gives me a much better understanding of what’s happening with all my complex automations.

Your text file will look something like this.

Home Assistant notifications (Log started: 2024-01-27T13:08:48.593945+00:00)
--------------------------------------------------------------------------------
<br>[1] =============== 01/27/2024 08:08 AM ===============
<br>[2] DIAGNOSTICS 01/27/2024 09:58:06.624463
<br>  - light.tv_left likely physical switched to OFF 
<br>  - This trigger was not a LIGHT entity. 
<br>--------------------------
<br>[3] DIAGNOSTICS 01/27/2024 16:58:04.927451
<br>  - light.living_room_cans_zw026 likely physical switched to ON 
<br>  - Brightness: 229 
<br>--------------------------
....
<br>--------------------------
<br>[8] SECURITY EVENT 01/27/2024 14:16:49.935720 
<br>Values Changed via script.arriving_home: 
<br>  - Security Notification, script.arriving_home started. 
<br>--------------------------
<br>[9] SECURITY EVENT 01/27/2024 14:16:55.042250 
<br>Values Changed via automation.set_alarm_to_disarmed_arriving_home: 
<br>  - Alarm changed to DISARMED. 
<br>--------------------------

This is some mixed text from other files, but you get the idea.

1 Like

Excellent, thanks a lot !!! What you both wrote fits my initial hopes :slight_smile: I will test it asap.
Should I care about file size? Should I try to find a solution to backup/rename/reset file if it gets above a certain size ? [if yes … is it possible in HA ? ]

I can’t tell you what you should do. But you certainly would benefit from giving some thought to long-term management of the files you create.

Yes, you can rename or delete (reset) files in HA. For example, I have log files showing the on/off state of my heating system’s boiler, and each zone valve. I have automations which rename them at the end of each month (appending the date to the file name.) Part of my routine backup process copies these to a location where I can analyze them easily, and after that I can delete them off the HA storage device. I could automate that too, but I haven’t bothered yet.

1 Like

I agree w CaptTom. You SHOULD care about long term file sizes, storage and housekeeping. My day to day events that I log get deleted and rebuilt every night at midnight UNLESS I turn off a toggle (like when I’m troubleshooting). I also have a few logs that I either manually reset or reset at a different time of day, based on what they do. This is all set up in automations that reset counters, toggles or any other details needed. Its really pretty simple to keep the file sizes low, my larges is 54K and is almost a years worth of data. Most are single digit K in size since the reset often.

Look at the FILE SIZE integration to get actual sizes of your logs and the DELETE Service to purge files. If you need help, just ask. Im glad to share my code if you need it.

Art

1 Like