I am running HAOS 14.1 and I am trying to create a sensor using command_line, that runs an ssh command to a target server to obtain a status.
I’ve done all the work of making SSH work between HA and the target server and from the HA terminal it works correctly:
I get an unknown value for sensor.test and I can see in the logs this error: Command failed (with return code 127): ssh -i /config/.ssh/id_ed25519 -o StrictHostKeyChecking=no [email protected] "\"C:/Program Files/Bitcoin/daemon/bitcoin-cli.exe\" -rpcuser=.. -rpcpassword=.. -rpcclienttimeout=35 -getinfo"
So I created two more sensors to test why ssh is not working:
That explains some things, but still doesn’t explain why the output of test5 is not captured.
test4 proved that the container has access to ssh and the simple ssh -V should have been shown.
What else am I missing?
Try adjusting your command formatting, your single-space indentation on the 2nd line looks like it may be adding an unintentional newline. If you want them joined into a single line it shouldn’t be indented:
Another possibility is the quoting, which can be tricky. You could try simplifying the command (less so than you’ve done), or try with some single quotes.
I really didn’t expect that ssh prints that information into stderr instead of stdout.
Thanks for pointing this out !
You had a good idea of trying something less complex so I did a single dir command:
This is the output on the terminal:
[core-ssh config]$ ssh -i /config/.ssh/id_ed25519 -o StrictHostKeyChecking=no [email protected] dir
Volume in drive C is Win 10
Volume Serial Number is 16A3-1394
Directory of C:\Users\oby
30/12/2024 14:42 <DIR> .
30/12/2024 14:42 <DIR> ..
30/12/2024 14:33 39 ${AUTH_KEY_FILE}
30/12/2024 14:42 95 %programdata%
16/01/2024 22:51 <DIR> .ms-ad
30/12/2024 14:52 <DIR> .ssh
But still unknown on the sensor defined like this:
Failed to set state for sensor.test6, fall back to unknown
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1227, in _async_write_ha_state
hass.states.async_set_internal(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
entity_id,
^^^^^^^^^^
...<5 lines>...
time_now,
^^^^^^^^^
)
^
File "/usr/src/homeassistant/homeassistant/core.py", line 2373, in async_set_internal
state = State(
entity_id,
...<8 lines>...
timestamp,
)
File "/usr/src/homeassistant/homeassistant/core.py", line 1817, in __init__
validate_state(state)
~~~~~~~~~~~~~~^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 246, in validate_state
raise InvalidStateError(
...<2 lines>...
)
homeassistant.exceptions.InvalidStateError: Invalid state with length 1365. State max length is 255 characters.
The last thing points me to the actual error:
Invalid state with length 1365. State max length is 255 characters.
OK, so that tells me that the SSH command actually works.
Later edit, you were correct on the new line thing as well, I corrected that and now I got the correct output from ssh.
YEsssssssss thank you so much. I’ve spent countless hours to make this work and you pointed out the error in seconds.