Unable to make command_line work with ssh

Hi,

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:

[core-ssh config]$ ssh -i /config/.ssh/id_ed25519 -o StrictHostKeyChecking=no [email protected]   "\"C:/Program Files/Bitcoin/daemon/bitcoin-cli.exe\" -rpcuser=... -rpcpassword=... -rpcclienttimeout=35 -getinfo"
Chain: main
Blocks: 798867
Headers: 877428
Verification progress: ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░ 75.8379%

If I try to run the same command from the sensor I configured in the configuration.yaml:
command_line:

  - sensor:
      name: "test"
      command: >
        ssh -i /config/.ssh/id_ed25519 -o StrictHostKeyChecking=no [email protected]
         "\"C:/Program Files/Bitcoin/daemon/bitcoin-cli.exe\" -rpcuser=.. -rpcpassword=... -rpcclienttimeout=35 -getinfo"
      unique_id: testingme
      scan_interval: 300
      command_timeout: 40

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:

  - sensor:
      name: "test4"
      command:  which ssh
      unique_id: testingme4
      scan_interval: 300
      command_timeout: 45      

  - sensor:
      name: "test5"
      command:  ssh -V
      unique_id: testingme5
      scan_interval: 300
      command_timeout: 45

and the output for test4 is /usr/bin/ssh and for test 5 is blank even though the same command in HA

terminal returns:
[core-ssh config]$ ssh -V
OpenSSH_9.6p1, OpenSSL 3.1.7 3 Sep 2024

So what I am understanding so far is that the SSH command itself is not working or not capturing any output when run under command_line.

I am not sure how to solve this and, after 3 days of testing and frustration, I am stuck.
Can you please point me in the right direction?

The command line sensor runs inside the homeassistant container. Your terminal command testing doesn’t.

https://www.home-assistant.io/integrations/command_line/#execution

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?

Well, from the linked docs it says:

With a 0 exit code, the output (stdout) of the command is used as value.

The output of ssh -V goes to stderr, not stdout.

❯ ssh -V > /dev/null
OpenSSH_9.9p1, OpenSSL 3.3.2 3 Sep 2024

❯ ssh -V 2> /dev/null

❯

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:

  - sensor:
      name: "test"
      command: >
        ssh -i /config/.ssh/id_ed25519 -o StrictHostKeyChecking=no [email protected]
        "\"C:/Program Files/Bitcoin/daemon/bitcoin-cli.exe\" -rpcuser=.. -rpcpassword=... -rpcclienttimeout=35 -getinfo"
      unique_id: testingme
      scan_interval: 300
      command_timeout: 40

You might try turning on the debug logs to see if it shows anything: https://www.home-assistant.io/integrations/command_line/#troubleshooting

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.

You could also try putting the command in a bash script and use that instead to avoid all the complicated quoted arguments. https://www.home-assistant.io/integrations/command_line/#use-an-external-script

1 Like

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:

  - sensor:
      name: "test6"
      command: ssh -i /config/.ssh/id_ed25519 -o StrictHostKeyChecking=no [email protected] dir
      unique_id: testingme6
      scan_interval: 300
      command_timeout: 40

In system logs I got 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.

1 Like