The commands can be dynamic, using templates to insert values for arguments. 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.
You might be right, having others look at my code is always helpful and brings different/new perspectives. Indeed, I may have misunderstood: using pipe symbols to run multiple commands. Definitely the tail command pipes in and out. This is a bit over my knowledge base but I can sense now possible security issues with this (my code).
I don’t see what is wrong in the OP.
You could try using the fully qualified pathname /usr/bin/tail but the template substitution looks fine in principle, to me.
Or, the input_text is not resolving to a value you believe it should be. Use the template editor to get/confirm the value - using the full snippet ,i.e.
Hello @ninjaef, nice of you to chime in. Appending the prefix /usr/bin/ to my cmds is not needed since the path resolves that issue for all my cmds and they execute fine. The substitution works fine in the template editor, see result:
I am not sure what to make of this situation? The log spits out the cmd without the substitution and the template code stays unchanged, which make it difficult to guess if this is normal behavior or not, hence allowed or not?
great to hear, I have yet to master the art of mind reading, you did not mention it, and hence I introduced this possibility as it has been known as a root cause. The process is diagnostic and without reading your mind, and in the absence of you declaring known relevant information, the possibilities start at the wide end of the spectrum
as one works through that which you may not have considered in the absence of information.
You have ensured that the subdirectories and files exist? try breaking the compound command down to singular parts…
just the tail with a hardcoded input file path and remove the stdin “<” … tail cmd does not need it.
if that works then
add a hardcoded output path
if that works then
add the template part for the output path.
You can add the debug level to the logger too for extra output in the log.
Also, transpose the quotes.
use single quote to enclose the full command, and use double quote within the template , i.e states(“…”)
@ninjaef Here is the debug version of your last command requested:
Hmm… that one is interesting, but not sure if I can make any sense of the debug information?
load_cell_copy_log: 'tail -n +3 /var/log/homeassistant/drivetrain_efficiency_results/last_load_cell_result.csv
> sensor_log/{{ states("input_text.load_cell_recording_filename") }}.csv'
2021-02-10 19:59:04 DEBUG (MainThread) [homeassistant.components.shell_command] Stdout of command: `tail -n +3 /var/log/homeassistant/drivetrain_efficiency_results/last_load_cell_result.csv > sensor_log/{{ states("input_text.load_cell_recording_filename") }}.csv`, return code: 1:
b'==> /var/log/homeassistant/drivetrain_efficiency_results/last_load_cell_result.csv <==\n1, 19.9\n'
2021-02-10 19:59:04 DEBUG (MainThread) [homeassistant.components.shell_command] Stderr of command: `tail -n +3 /var/log/homeassistant/drivetrain_efficiency_results/last_load_cell_result.csv > sensor_log/{{ states("input_text.load_cell_recording_filename") }}.csv`, return code: 1:
b'tail: cannot open \xe2\x80\x98>\xe2\x80\x99 for reading: No such file or directory\ntail: cannot open \xe2\x80\x98sensor_log/test2.csv\xe2\x80\x99 for reading: No such file or directory\n'
2021-02-10 19:59:04 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `tail -n +3 /var/log/homeassistant/drivetrain_efficiency_results/last_load_cell_result.csv > sensor_log/{{ states("input_text.load_cell_recording_filename") }}.csv`, return code: 1
This is my file: sensor_log/test2.csv not sure what the rest means?
cannot open \xe2\x80\x98sensor_log/test2.csv\xe2\x80\x99
The problem is not his templates, the problem is that HA restricts the possible commands etc. omce you use a template in a shell command.
That’s why the hardcoded version works fine, but the template version results in an error.
From my understanding the problem is the > command.
typical linux shell interpreter reporting of errors and codes. the HA substitution is being passed to the shell , unmodified. I am drawn to conclude that the issue is in HA/config. That is where the attention needs to be. But I cannot see what the problem is and have no access to my HA box at the moment to trial anything. @Burningstone suggests that it might be the redirect (“>”) which is effectively a *nix pipe and I recall some issue regarding the use of pipes.
They are hex codes for non-displayable characters/unicode.
You didn’t say whether you tried - as suggested - transposing the quotes.
Can’t remember - it was setup a long time ago and I have had no reason to revisit it. I have no access to the firewalled OS at the moment, so will look when I get to the centre.
As mentioned multiple times, pipes don’t work with templates. HA doesn’t allow this.
I quote the docs again:
The commands can be dynamic, using templates to insert values for arguments. 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.
@ninjaef indeed, I did not say it explicitly, but I said I would try to cover all the permutations you asked for. The last example to this post is the requested example transposing the quotes!
There you go @Cognoquest . The redirects are in effect, pipes - so not allowed.
I do use them albeit in a (.sh) script that is called by the HA shell command defined. So the answer is to do this in your setup. The script will need full shell access and suitable owner+group+permissions and be invoked using /bin/bash or whatever is on your system.
The above script does not cater for any error or deviation in expected parameters, exitance of shell and executables, and syntax. Add these at your discretion.