Using shell command in automations

Hi,

I have the following commands in config.yaml, and I am using this object in the automatons the command is not being triggerd.

When running the curl command in crontab it works fine (i see output in the file).
Any ideas what is wrong?

shell_command:
# Command to update DDNS with public IP address
  update_dns: 'curl https://freedns.afraid.org/dynamic/update.php?UUswNXgweabcdefghijk1pzOjE4Njk0MTE3 >> /tmp/freedns.log 2>/dev/null'

The shell commands in my configuration do not have the single quote marks. Donā€™t know if it matters, but you might try removing them. You might also try using the full path to curl, for example /usr/bin/curl ā€¦

I have some shell commands using curl and for mine the entire command is in single quotes but the http address is double quoted

command:  'curl "https://blah blah"'

would you mind pasting the full automation because I am not sure the curl command is within the Automation or in the config.yaml

That is not from an automation that is a shell_commandā€¦ you shouldnā€™t have the actual command in the automation it should be shell_command.your_command

See examples: https://www.home-assistant.io/integrations/shell_command/

that is how i did it ā€¦ not urgent right now because I managed to do it with a crontab, but it would be nice to understand what is wrong

As others have suggested, quoting is usually the issue with shell_command

Note that I run HomeAssistant in a venv on Ubuntu under a dedicated service account - how you are setup could affect results. I tested my commands within the venv running as the service account user before implementing, which helped resolve some permission and path issues.

Some examples:

shell_command:
  extract_motion_image: 'ffmpeg -y -ss 04 -i /home/homeassistant/.homeassistant/snapshots/motion.mp4 -qscale:v 4 -frames:v 1 /home/homeassistant/.homeassistant/snapshots/motion.jpg &>/dev/null'
  extract_motion_cleanup: 'rm -f /home/homeassistant/.homeassistant/snapshots/motion.*'
  send_sms: 'gammu sendsms TEXT "{{ sms_to }}" -text "{{ sms_msg }}"'

and a curl command line example for a switch (calling a secondary hassio instance):

switch:
  - platform: command_line 
    switches:
      garden_lights:
         friendly_name: 'Garden Lights'
         command_on: >
             /usr/bin/curl -X POST \
                 http://192.168.1.249:8123/api/services/switch/turn_on \
                 -H "Content-Type: application/json" -d '{"entity_id": "switch.garden_lights_switch"}' \
                 -H "Authorization: Bearer secrettoken"
         command_off: >
             /usr/bin/curl -X POST \
                 http://192.168.1.249:8123/api/services/switch/turn_off \
                 -H "Content-Type: application/json" -d '{"entity_id": "switch.garden_lights_switch"}' \
                 -H "Authorization: Bearer secrettoken"
         command_state: >
             /usr/bin/curl -X GET \
                 http://192.168.1.249:8123/api/states/switch.garden_lights_switch \
                 -H "Authorization: Bearer secrettoken"
2 Likes

what do you think of this?
Still not working :frowning: do you have a simple curl command I can use (eg write something to file and then I check the file)

shell_command:
# Command to update DDNS with public IP address
  update_dns: '/usr/bin/curl "https://freedns.afraid.org/dynamic/update.php?UUswNXgwekabcdefghVCY1pzOjE4Njk0MTE3" >> /tmp/freedns_com.log 2>/dev/null'

I would try that command at the command line manually as the Homeassistant userā€¦ in the venv if you have one ā€¦ and see what happens

the problem might be that I am running through a dockerā€¦ will test and let you know

Looks like there have been some parameter changes since I last tested my send_sms shell_command - I tested this evening after making some other configuration changes and was getting some 127 errors which I traced back to the parameters in the command.

Iā€™ve just had to modify the command line (note the quote changes) to:

shell_command:
  send_sms: gammu sendsms TEXT "{{ sms_to }}" -text "{{ sms_msg }}"

EDIT: quote correction (copied wrong test line)

running the command from CLI works fine

pi@raspberrypi:~ $ curl ā€œhttps://freedns.afraid.org/dynamic/update.php?UUswNXgabcdefghFkzWUVCY1pzOjE4Njk0MTE3ā€ >> /tmp/freedns_mooo_com.log 2>/dev/null

ā€¦ at this point i am a bit confused

I donā€™t use docker so I donā€™t know how the different containers could affect how it runsā€¦ I would try putting the command you just ran as your shell command in your configā€¦ wrap the entire command in single quotesā€¦ restart Haā€¦ the go to the services tab and choose shell_command.update_dns and press call serviceā€¦ then see if you get an error and check the logs to see the details

the msg I am getting in the logfile is the following:`

2019-11-24 19:53:53 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1854561712] Received {ā€˜typeā€™: ā€˜call_serviceā€™, ā€˜domainā€™: ā€˜shell_commandā€™, ā€˜serviceā€™: ā€˜update_dnsā€™, ā€˜service_dataā€™: {}, ā€˜idā€™: 19}

`

not much help :slight_smile:
never mind mate. thanks for your help