Trying to invoke a bash script via an automation

Platform : OS/X running a Virtual Machine, Linux Ubuntu, * 2023.12.3

  • Supervisor2023.11.6

My bash script runs fine in terminal - basically, it runs FFMpeg to grab a still image from each of 6 RTSP cameras, and dumps them into media/cameras. I can see the images fine after capture. It waits 5 minutes, and then grabs new images, overwriting.

I have tried various methods to make this run automatically. I first set it up in crontab, but it is not running.

Then I tried to set it up as a service in configuration.yaml, via methods I’ve read about online – 'shell.command" and command_line." I don’t get an error on the config.

But when I try to add an automation, zero luck with anything. I don’t actually seem to have any entity define to call up for the action when trying to configure an automation.

Any thoughts on what I am doing wrong?

I have screenshots but can’t seem to post them,.

Which terminal? Are you ssh’d into Linux Ubuntu or to the terminal add-on within Home Assistant? If it is Linux Ubuntu and you are running HA in a docker container then HA does not have direct access to run scripts outside of the docker container. You would need to run ssh in HA to execute commands on the host OS (Ubuntu). For example:

in configuration.yaml

shell_command:
  my_host_script: 'ssh -i /config/.ssh/id_rsa [email protected] /home/pi/myscript.sh'

in automations.yaml

- id: 'my_host_script_id'
  alias: Run my_host_script on host every 5 minutes 
  trigger:
  - platform: time_pattern
    minutes: '/5'
    seconds: 0
  action:
    - service: shell_command.my_host_script

PS:

No problem, just click on the “Upload” button in the editor to upload a .jpg for example. Do not use this for code but use the “Preformatted text” button instead to post code.

Thank you so much. That fixed it.

I should mention the script is within HA. I do have a version that can run on OS/X, sending it over to same-machine-HA via SMB, ssh or sftp but that just seemed like an extra step.

Update.

I have a 2nd setup, exact same as the first one. This is at home ; the other at the chalet.

Configuration.yaml at chalet:

shell_command:
cam_capture: /share/cameras/camera-5.sh

Configuration.yaml at home:

shell_command:
cam_capture: /share/cameras/camera-5.sh

(Exact same script, just different cameras. Runs fine in HA terminal)

automations.yaml at chalet:

  • id: ‘my_host_script_id’
    alias: Run my_host_script on host every 5 minutes
    trigger:
    • platform: time_pattern
      minutes: ‘/5’
      seconds: 0
      action:
    • service: shell_command.cam_capture

automations.yaml at home

  • id: ‘my_host_script_id’
    alias: Run my_host_script on host every 5 minutes
    trigger:
    • platform: time_pattern
      minutes: ‘/5’
      seconds: 0
      action:
    • service: shell_command.cam_capture

The automation at the chalet works fine.
The one at home gives an error:

" Error in describing action: undefined is not an object (evaluating ‘e.services[t][I]’)"

Everything seems the exact same.

Could be an indentation issue. Yaml is very picky about how the code is formatted. Looks like “action” is not correctly indented. Can you please post your code using “preformatted text”.

alias: Camera script
trigger:
  - platform: time_pattern
    minutes: /5
    seconds: 0
action:
  - service: shell_command.cam_capture
1 Like

My suspicion here, is that for whatever reason the service shell_command.cam_capture does not exist on your home machine

Please post the properly formatted snippet of your home’s configuration.yaml where the shell command is.

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

  # Included script pages
#group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
#switch: !include switch.yaml
sensor: !include_dir_merge_list sensor/
#camera: !include camera.yaml
#light: !include light.yaml
#climate: !include climate.yaml
#input_text: !include input_text.yaml
input_datetime: !include input_datetime.yaml
input_number: !include input_number.yaml
input_boolean: !include input_boolean.yaml
binary_sensor: !include binary_sensors.yaml
# Example configuration.yaml entry
http:
    use_x_forwarded_for: true
    trusted_proxies:
        - 172.30.33.0/24
frontend:
  themes: !include_dir_merge_named themes
shell_command:
  cam_capture: /share/cameras/camera-5.sh

try:

  cam_capture: bash /share/cameras/camera-5.sh

That doesn’t work (adding bash)

And if you look upthread, I have the EXACT SAME syntax as another system I have (one at my chalet, one at home).,

As far as I can, both setups are the exact same but only one throws off the error.

Are the 2 HA’s on the same network? It shouldn’t be a problem as far as I know but I’m not sure. What happens if those scripts run in parallel? Could this be an issue?

Nope. One is at home, the other is at my chalet 150km away.

I have to give up here. No more ideas. I’d recommend that you post a new question and ask why a shell_command could behave differenty on two identical HA instances.