Command_state: parameter with "quotes"

Sintax error:
command_state: ssh -i .ssh/id_rsa -o StrictHostKeyChecking=no [email protected] systemctl status kodi --full | grep -oc “Active: active”

very helpful

Please see this post in order to properly give all required information.

If i use this code:

command_state: ssh -i .ssh/id_rsa -o StrictHostKeyChecking=no [email protected] systemctl status kodi --full | grep -oc "Active: active"

it gives me syntax error:

complete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line at line 108, column 133:
… webmin --full | grep -oc “Active: active”

hope is OK now. Sorry

What integration is this for? Also, that’s not what I wrote. You’re trying:

command_state: ssh -i .ssh/id_rsa -o StrictHostKeyChecking=no [email protected] systemctl status kodi --full | grep -oc "Active: active"

I said use this

command_state: >
  ssh -i .ssh/id_rsa -o StrictHostKeyChecking=no [email protected] systemctl status kodi --full | grep -oc "Active: active"

This automation is to control linux services as switches. For example during night or when i’m home i want the VPN service to be off (cibersecurity).

I noticed the ‘>’ charecter but i thought was a typo. Unfortunately it remains not working.

Prior opening this request i did about 20 permutations and google it.

Thanks for help!

Comented, the working code i had at Home Assistant Core, this weekend i migrated to Docker. I can run commands without ’ or ", but not with them.

  - platform: command_line

      #command_on: "ssh [email protected] 'systemctl start kodi'"
      #command_off: "ssh [email protected] 'systemctl stop kodi'"
      command_on: "ssh -i .ssh/id_rsa -o StrictHostKeyChecking=no [email protected] 'systemctl start kodi'"
      command_off: "ssh -i .ssh/id_rsa -o StrictHostKeyChecking=no [email protected] 'systemctl stop kodi'"
      #command_state: "systemctl status kodi --full | grep -oPc 'Active: active'"
      command_state: >
        ssh -i .ssh/id_rsa -o StrictHostKeyChecking=no [email protected] systemctl status kodi --full | grep -oc "Active: active"
      value_template: '{{ value == "1" }}'

Ok, so what’s the state and what errors are in your logs with that configuration?

Sorry, but did you just include the ‘greaterthan’ or did you institute the new line as well ?

Isn’t your command missing some quotes? You’re trying to run the command systemctl status kodi --full on the other machine right? But you haven’t wrapped it in quotes like the others. Your command_on for example is this:

ssh -i .ssh/id_rsa -o StrictHostKeyChecking=no [email protected] 'systemctl start kodi'

with the command wrapped in single quotes to start kodi. But then your command_state is this:

ssh -i .ssh/id_rsa -o StrictHostKeyChecking=no [email protected] systemctl status kodi --full | grep -oc "Active: active"

which doesn’t have systemctl status kodi --full wrapped in single quotes.

Please share the error you’re seeing. But also please make sure its the quotes at the end and not that you are missing quotes earlier on

Also just an FYI, it looks like you generated id_rsa inside the normal .ssh folder in the home directory (which is actually /root/.ssh). If so, you need to move it out of there.

The /root folder is wiped every time HA updates so your sensor will break every update when your id_rsa file is wiped away. You should move any files you rely on somewhere inside the /config folder since that is preserved through updates and then reference the file there with the -i flag.

Is strange: on home assistant console the command works:

But on logs i see this error:

Host key verification failed.
Warning: Identity file .ssh/id_rsa not accessible: No such file or directory.
Permission denied, please try again.
Permission denied, please try again.
[email protected]: Permission denied (publickey,password).
2020-12-22 09:56:48 ERROR (SyncWorker_3) [homeassistant.components.command_line] Command failed: ssh -i .ssh/id_rsa -o StrictHostKeyChecking=no [email protected] systemctl status kodi --full | grep -oc “Active: active”

But the file is on place:

Thanks for support!

Thanks i was not aware of this. Will do so.

looks the root reason was not the ’ or " but the lack of access to id_rsa file.

Now is fixed but i’m facing another problem : “Interactive Authentication Required”

I’m doing sudoers changes to retry. Will let you know.

Thanks for support!

You need to mount the key to the docker container, the directory you are showing is on the host machine, to which the docker container doesnt have access.

following another user suggestion i moved it into /config. You meant that right? Thanks!

Probably works as well.

Because of your double quotes, put the whole ssh command in double quotes.
Inside the command used double quotes or backslash characters you have to escape with the \ (backslash character), see my ssh command to start and run VLC on a Windows machine using the task scheduler. Good luck

ssh [email protected] "schtasks /create /TN StartVLC /TR \"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe\" /SC ONCE /ST 00:00 & schtasks /run /tn StartVLC & schtasks /delete /tn StartVLC /f" >/dev/null 2>&1

Hi. The original problem is solved so i think i’m going to close this.

The switch can now see the state of the service.

Unfortunately i cannot enable/disable the service with the switch. Due the error is completely different i’ll close this one and open a new one.

Thanks you all!


Ah, well that is unfortunate.

FYI for future reference, the home assistant CLI is an add-on which means it is a separate docker container from HA application itself. It has its own set of dependencies and filesystem other then the few shared folders such as /share and /config. So the fact that your command works when running it while ssh’ed in to the HA using one of the Terminal & SSH add-ons actually doesn’t tell you anything about whether it will work when run from HA using a command line sensor or shell command.

If you plan to do a lot of command line sensors and shell commands my advice is to install the Portainer add-on. That will let you get to the homeassistant container (which is what actually runs HA) and access the console within that container. Then you can test your commands from there since that is running that command in the exact same way HA will be. If a command works in the console within the homeassistant container then you know it will work from a sensor or shell command.

Well as long as its not relying on a file outside of /config, /share or any of the folder subsystems which actually persist over update. That’s the other gotcha to watch out for like I mentioned earlier, since it will appear to work in that case but break next update.

1 Like

Thanks for this new advice. Is my first approach to docker world so i have realice i was making the tests on the wrong console as you said.

I taked your advice and i’m making tests via portainer now. Thanks once again.

1 Like

No problem! I’ve actually found myself repeating that advice a lot so I decided to make a guide. Hopefully that’ll help people head off some of these issues in the future.