I know this was not efficient when I originally set it up, and wanted to revisit it later - and now is the time - but looking for guidance on how to do this… Currently I have several command-line sensors in HA that grep the syslog (and other logs) to monitor activity on my RPI. When the logs get big and several of these firing off simultaneously the CPU briefly spikes, possibly due to 1. each sensor scanning the entire log file each time (some of those doing it at the same time), and 2. the value_template logic I use within the command_line sensors (not as much of an issue I don’t think). Below is an example of one of the sensors, and then my idea/suggestion/ask below on how to do this more efficiently -
Example - I wrote a low-level daemon that turns the fan on if the RPI CPU reaches 60 degrees C and turnes it off when the temperature drops back below 50 degrees C. Every 5 minutes I had that daemon add text like this to the syslog:
Sep 8 16:27:13 kruse-pi fancontrol.py: Fan still off 44.30°<50-60 (1694204833)
The related command-line sensor with this code:
command_line:
- sensor:
name: RPI_FAN_STATUS
command: "grep 'fancontrol.py' /share/syslog | tail -1 | cut -c-255"
scan_interval: 30
command_timeout: 5
value_template: >
{% set data = value | regex_findall('\(([0-9]+)\)') | first | int | as_datetime | as_local %}
{% set ts = data.year ~ ' ' ~ (value | regex_findall('(.+)kruse-pi fancontrol') | first).strip() %}
{% set str = (strptime(ts, '%Y %b %d %H:%M:%S', data) | as_local).strftime('%a %-I:%M:%S%p') %}
{% set str = str[:-2] ~ str[-2:].lower() %}
{% set str2 = value[value.find(".py:")+5:] %}
{% set indx = str2.find(" (") %}
{{ str ~ ": " ~ str2[:indx] }}
unique_id: RPI_FAN_STATUS
So I can show this on a dashboard:
Upon researching this, from what I can tell it appears a much better more CPU-friendly way is to run a bash script on the host that monitors the log file using inotify and updates a Home Assistant sensor - directly - when the matching text appears in the log - by using hass-cli?
(BTW the above log entry is the only one where I have control over the process that adds to the log - so I cannot change code in any of the others to send an MQTT message or anything like that to HA…)
So - is the bash script on the host with inotify with hass-cli the best way to most efficiently get log data into an HA sensor (I am always looking for the most recent instance of a string when it appears in the log) - and how the heck to I do that - ? or is there a way to have a sensor which would just continuously tail the log directly from within HA - would that be even better (and easier) and how would I do that (I thought I could do something through the recorder but that’s not going to work I don’t think)?
Any ideas would be very helpful - ?
(P.S. I just checked some of my logs, they are bigger now than I have ever seen them so I cleaned them out due to other unrelated recent errors that were in them - and the problem is not as much of an issue now - but I still do want to get rid of all of these GREP statements - )