Don't understand how to use shell_commands

Hello!
I am pretty new to home assistant and is currently struggling with creating an automation which will run ssh command when clicked on a button.
The issue is I can’t make even a simple automation work based on official samples from there Shell Command - Home Assistant.
For example I write such test automatization

description: ""
mode: single
trigger:
  - device_id: 67d969fc95fd8ec9ee437a055ca3253c
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: turn_on
condition: []
action: 
  service: shell_command.restart_pow
shell_command:
  restart_pow: touch ~/.pow/restart.txt

And when I try to save it I get “Message malformed: extra keys not allowed @ data[‘shell_command’]” error. Not sure what is wrong.
Could be important, I installed HA as a docker image on ubuntu, so I don’t have addons available, in case I need to install some addon to make it work.
Thanks in advance!

Seems strange. DO you not have a shell_command sensor defined named “restart_pow”? If yes then why are you sending the shell_command again?

Your “action” should be “call the service shell_command.restart_pow” and that is already defined, so you do not attempt to send the information again.

how exactly I suppose to define a shell_command sensor? based on the docs I though all you need is to add “shell_command” right in the same yml file as the automation itself.
in really I want to send ssh command, but for now I am trying to understand the basic syntax or executing any shell command

Ah, you are not getting any data back or care whether it succeeds or not or anything, you just want to shell to something? Then possibly the command itself needs to be in quotes …

"touch ~/.pow/restart.txt"

Not sure as anything I ever do with shell_commands I do with a sensor and get the resylts back as the state so I can understand whether it actually did something. Also I would add that “~” applies likely to the homeassistant user and not sure what that gets you, the HA gurus can tell you likely what directory you are executing it in.

Also I would assume that “automation:” is at the top within the YAML you have. Because it would be:

automation:
   - description:
     --- blah blah blah ---
   - trigger:
     --- blah blah blah ---
  - action:
      service: shell_command.{whatever_you_named_it}
  - shell_command:
       {whatever_you_named_it}: "{my shell command here with spaces and all and such with properly escaped quotes}"

Yeah, so ideally I want something like this

description: ""
mode: single
trigger:
  - device_id: 67d969fc95fd8ec9ee437a055ca3253c
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: turn_on
condition: []
action: 
  service: shell_command.toggle_vpn
shell_command:
  toggle_vpn: "ssh [email protected] -i ./.ssh/id_rsa ./togglevpn.sh"

Where I don’t need to read a value from ssh, only execute my command. But it gives me the same strange
Message malformed: extra keys not allowed @ data['shell_command']
And I can’t understand why so

Where in your YAML is that chunk of YAML? Meaning where did you get that? From the automation editor?

It is all I have in a newly created automation. After that I try to save it and this error appears.

I also tried to debug it with Developer tools - Services. I put there

service: shell_command.toggle_vpn

shell_command:
  toggle_vpn: "ssh [email protected] -i ./.ssh/id_rsa ./togglevpn.sh"

And when I try to run it I get
Failed to call service shell_command.toggle_vpn. extra keys not allowed @ data['sequence'][0]['shell_command']. Got {'toggle_vpn': 'ssh [email protected] -i ./.ssh/id_rsa ./togglevpn.sh'}

OK, maybe I can speculate what is wrong.
Let me ask … are you thinking you define “toggle_vpn” in the automation? Or is it defined elsewhere like in configuration.yaml?

I would think you would need to create any shell commands in configuration … :slight_smile:

# Example configuration.yaml entry
# Exposes service shell_command.restart_pow
shell_command:
  restart_pow: touch ~/.pow/restart.txt

Reboot so it lives.

Then use it. But I am only speculating. Just guesses. Note that is copied from the sample you are doing and it says you put the shell_command in configuration.yaml or anywhere else … but I think you are trying to define it in the automation which IMHO would never work unless you created something generic that would take the shell_command as a parameter.

Shell commands go in your configuration.yaml file. Not your automations.yaml file.

automations.yaml

description: ""
mode: single
trigger:
  - device_id: 67d969fc95fd8ec9ee437a055ca3253c
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: turn_on
condition: []
action: 
  service: shell_command.restart_pow

configuration.yaml

automation: !include automations.yaml

shell_command:
  restart_pow: touch ~/.pow/restart.txt

Yes, you are correct :slight_smile:
I am relatively new to HA and It is a first time I need to modify configuration.yaml indeed.

Initially I was referring to this last sample Shell Command - Home Assistant where it seems like the shell_command definition is really inlined right into the automation.

But yeah, after I changed configuration.yaml and restarted I was able to make my ssh request to work as I wanted. Thank you very much!

Shell commands go in your configuration.yaml file. Not your automations.yaml file.

The problem is that it is clearly not what’s suggested in HA documentation: Shell Command - Home Assistant (see AUTOMATION EXAMPLE paragraph).
The shell_command: set_ac_to_slider: is defined in the automation itself. (and by the way this is very handy)

Nope.

This is a configuration.yaml file without an automation or input number !include. Could also be a package.


.

The automation config ends just above input_number:

You can not define input_numbers or shell_commands in automations.

It is a poor example.

2 Likes