Command Line Switch for TV - Command Failed

Been playing around today with an older TV, a Pi Zero, and passing CEC commands to turn the TV on and off.
Up to this point I have been able to get all commands running directly on the RPI, through the HA SSH Web Terminal, and I also have an SSH key installed (and confirmed working) for password-less remote SSH commands.

But, it just won’t run from within a HA entity.

I’ve taken a copy of the example from the Bravia TV component page, and changed the IP and name.
This is sitting inside a switches yaml file alongside another working switch template.

- platform: command_line
  switches:
    tv_media:
      friendly_name: TV - Media
      command_on: ssh [email protected] "echo 'on 0' | cec-client -s"
      command_off: ssh [email protected] "echo 'standby 0' | cec-client -s"
      command_state: ssh [email protected] "echo 'pow 0' | cec-client -s |grep 'power status:'"
      value_template: '{{ value == "power status: on" }}'

The log says the following, and it is the same for the other commands.

Command failed: ssh [email protected] "echo 'on 0' | cec-client -s"

I have been looking at this for hours, and I can’t tell if my syntax is incorrect, despite being the same from the component page, or if it’s something else I’m unaware of? I can’t understand why it would work from the HA SSH Web Terminal, but not an entity?

Hoping someone could cast an eye over it. Thanks.

As the user running home assistant, does the ssh command work without intervention?

I believe so.

What user did you use to log into that session with though?

If it was root then you are not getting the same privileges as the home assistant user would.

Oh, I think I see what you are saying. I’m not sure, but I will investigate.

There are many threads about this but I cannot find the one that had the darned answer!

Yes, I went through many, but was unable to find anything relevant to my situation.

Success! Here is an update for anyone else reading this thread.

Firstly, SSH and keys are not a strong point for me, so it was a bit of a learning curve, and the addition of not quite reading another person’s instructions properly. :roll_eyes:

A couple of points:

  • I believe that I was using and referencing the correct users. Both on the RPI and on Hassio.
  • I had been generating the keys and sending them to the RPI using ssh-copy-id -i root@ip_address, which I believe created different keys and put them in a different location to what the above-linked instructions said. :roll_eyes: Again, note to self to RTF instructions.
  • And lastly, in the above-linked instructions, they had additional options in the ssh command which weren’t in the HA command line switch example. I believe these options were important for success.

Don’t take my word for it that it was all the above, but I know that having addressed those I now have it working.

I appreciate those comments that did chime in.

Hi
I have the same problem, do you remember what additional options in the ssh command they where talking about? The link: https://megamorphf.github.io/homeassistant/hyperion/ssh/hassio/2018/01/22/controlling-anything-via-ssh.html , is not working anymore and I can’t get this to work.

@Grepen
In the beginning I had the same problems as you. The steps and additional options you need are as follow:

  1. Create a folder in your config directory called something like “ssh”
  2. Copy your private keyfile to the newly created folder
  3. SSH into your hassio server and chown the keyfile so only that user has access to it (otherwise your keyfile will be ignored because all users are able to read your private key). Chown command: chown -R hassio:hassio /config/ssh/id_rsa
  4. Lastly change your ssh commands to: command_on: "ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] '*your command to execute*"

These steps helped me to successfully execute my python scripts on a raspberry pi. Hope it is usefull to you guys.

4 Likes

Thanks @Darkraider, much appreciated.
I will try that out as soon as I got time for it.

Thanks for this :slight_smile:

Worked perfectly, and I didn’t have to do step 3, said there was no hassio user but it didn’t matter anyway.

Cheers!