The Shell Command integration somewhat restrictive, " When using templates, shell_command runs in a more secure environment which doesn’t allow any shell helpers like automatically expanding the home dir ~ or using pipe symbols to run multiple commands. Similarly, only content after the first space can be generated by a template. This means the command name itself cannot be generated by a template, but it must be literally provided." I have been using the Command Line integration instead. Most of my command-line sensors are a “tail -f” grabbing the latest specific entries in the log files (note not all underlying directories are available to Home Assistant especially if you are running in a container, but I digress).
Ok all I can do is show you what I have in my configuration.yaml and what it does. First I have this so that HA knows to watch what is in that directory:
(I had to make OS config changes so that my SYSLOG file is put into this directory, and the above entry allows it to look there - and only some directories on the host are visible from within the container). That “/share” directory actually is this on the actual hardware outside of the container:
/usr/share/hassio/share
I have an app called WEEWX running on the host RPI (outside of HA) which grabs information from my weather station and sends it out to various websites. I have the weewx.log file therefore in that same share directory mentioned above…
Then, here are two command_line sensors in my configuration.yaml, watching the weewx.log file and syslog, showing data (note the “cut -c-255” is required as that is the maximum length of a the string that is returned that can be handled). The secoind sensor just shows the error message (only when there is one…)
command_line:
# Add-Ins for monitoring weewx published weather data
- sensor:
name: WEEWX_LAST1_SENT_TO_AWEKAS
command: "grep 'AWEKAS: Published' /share/weewx.log | 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 weewx') | first).strip() %}
{% set time = strptime(ts, '%Y %b %d %H:%M:%S', data) | as_local %}
{% set str = time.strftime('%a %-m/%-d %-I:%M:%S%p') ~ ' (5 min)' %}
{% set indx = str.find("M (") %}
{% set oldstr = str[indx-1:indx+1] %}
{{ str.replace(oldstr,oldstr.lower()) }}
unique_id: WEEWX_LAST1_SENT_TO_AWEKAS
- sensor:
name: SYSLOG1_ERRORS
command: "grep 'ERROR' /share/syslog | tail -1 | cut -c-255"
scan_interval: 60
command_timeout: 5
# Value template format is an issue here because the error could be in almost any format.
# {% set data = value | regex_findall('\(([0-9]+)\)') | first | int | as_datetime | as_local %}
# {% set ts = data.year ~ ' ' ~ (value | regex_findall('(.+)kruse-pi weewx') | first).strip() %}
# {% set time = strptime(ts, '%Y %b %d %H:%M:%S', data) | as_local %}
# {% set str = time.strftime('%a %-m/%-d %-I:%M:%S%p') ~ ' (5 min)' %}
# {% set indx = str.find("M (") %}
# {% set oldstr = str[indx-1:indx+1] %}
# {{ str.replace(oldstr,oldstr.lower()) }}
unique_id: SYSLOG1_ERRORS