Command Line Sensor returning incorrect value

I’m trying to use a command line to determine the status of a process/service running on another computer.
I have mythtv backened running on a seperate server running linux.
I am running HA OS on an Intel Nuc.

When I run this command from Terminal (using Terminal & SSH official add-on) it works

"ssh -i /config/.ssh/id_rsa -o UserKnownHostsFile=/config/.ssh/known_hosts [email protected] systemctl show mythtvbackend | awk -F= '/^SubState=/ {print $2}'"

However when I run the same command from my mythtv_backend.yaml file in the command_line folder I am not getting the same results as I get from running in Terminal.

When run in terminal I get the result of “running” but from the .yaml file the result is “dead”

I’ve tried as a Binary Sensor and I get “off” as the result, which also is incorrect as the process/service is indeed running.

Here is my setup.
I have a command_line folder which contains my command line yaml files.
The command_line folder is referenced in my configuration.yaml file with:

command_line: !include_dir_merge_list command_line/

I know this is working as I created a cpu_temp.yaml file and used the sample sensor config shown in the Command Line docs to be sure I had things setup correctly.

My sensor configs are as follows:

Sensor:

  - sensor:
      name: "MythTV Backend Status"
      command: "ssh -i /config/.ssh/id_rsa -o UserKnownHostsFile=/config/.ssh/known_hosts [email protected] systemctl show mythtvbackend | awk -F= '/^SubState=/ {print $2}'"
      scan_interval: 60

Binary Sensor:

  - binary_sensor:
      name: "MythTV Backend"
      command: "ssh -i /config/.ssh/id_rsa -o UserKnownHostsFile=/config/.ssh/known_hosts [email protected] systemctl show mythtvbackend | awk -F= '/^SubState=/ {print $2}'"
      scan_interval: 60
      payload_on: "running"
      payload_off: "dead"

Just to eliminate the possibility that it has something to do with the known_hosts, which was always problematic for me, try it with -o 'StrictHostKeyChecking no' instead.

I tried -o ‘StrictHostKeyChecking no’ and -o ‘StrictHostKeyChecking=no’ (just to be sure) and the results didn’t change.

Enable debug for the command_line integration and it will log all the output of your command to give you a better idea of what’s going wrong. You can enable it on the fly in the actions tab of developer tools:

action: logger.set_level
data:
  homeassistant.components.command_line: debug
  

Don’t forget to reset it when you’re done.