"Command failed" error on remote ssh command (for binary sensor)

Command is used for this binary sensor (IP address masked on purpose). It’s just to probe the state of systemd service on another machine:

binary_sensor:
  - platform: command_line
    name: Spy serwis
    command: "/usr/bin/ssh [email protected] '/bin/systemctl is-active spy.service'"
    payload_on: 'active'
    payload_off: 'inactive'

I have generated ssh key, uploaded to remote machine and I can login user homeassistant without the password. Command itself works flawlessly inside homeassistant env:

pi@hassbian:~ $ sudo -u homeassistant -H -s
homeassistant@hassbian:/home/pi $ source /srv/homeassistant/bin/activate
(homeassistant) homeassistant@hassbian:/home/pi $ /usr/bin/ssh [email protected] '/bin/systemctl is-active spy.service'
active
(homeassistant) homeassistant@hassbian:/home/pi $ 

However, HA throws me this log:

2018-08-19 18:53:46 ERROR (Thread-11) [homeassistant.components.sensor.command_line] Command failed: /usr/bin/ssh [email protected] '/bin/systemctl is-active spy.service'

Strangely, this happens only when spy.service on remote machine is inactive. When it’s active, HA runs the command just fine. When it’s inactive, HA reports above error and fails to change the state of binary sensor. I can still run the same command manually in HA env just fine, no matter what’s the state of spy.service.

What else am I missing? How can I find out why this command fails? I know about this post, explaining how to run remote ssh commands with Hass.io. Well, I’m using Hassbian, so does it apply here as well? It’s not running in Docker. Shouldn’t this just work, if it’s working in homeassistant environment?

When you run it manually and it’s inactive, what is the status code? (i.e., what does echo $? return right after you run the command manually?)

The “Command failed” error happens when the command returns a non-zero status code.

While service inactive i get answer unknown instead of inactive

I’m not sure if I understand. Service inactive, “echo &” run right after the command:

pi@hassbian:~ $ sudo -u homeassistant -H -s
homeassistant@hassbian:/home/pi $ source /srv/homeassistant/bin/activate
(homeassistant) homeassistant@hassbian:/home/pi $ /usr/bin/ssh [email protected] '/bin/systemctl is-active spy.service'
inactive
(homeassistant) homeassistant@hassbian:/home/pi $ echo $
$
(homeassistant) homeassistant@hassbian:/home/pi $

EDIT: “echo $?” run right after the command, it returns 3:

(homeassistant) homeassistant@hassbian:/home/pi $ echo $?
3

Not “echo &” or “echo $” but “echo $?”. The last one will echo the status code returned by the ssh command.

Ok, that’s your problem. You need to find a way to run the command but have it return a status code of zero. E.g., there might be a switch to ssh or systemctl that will return a code of zero even if the service is inactive. Or, you can follow the ssh command with “; true”.

1 Like

That did the trick. Seems so obvious now.
Thanks a lot!