Pause automation till shell_command is done

When my door opens an automation fires which calls a shell command to get the current picture from my front door camera and sends the pictures with telegram to my phone.
Sometimes it happens that the shell command takes longer and I receive pictures from the last time the door was opened.
Is there a simple solution to wait till the shell command is done and only after that continue the automation?
I know that there is wait_template, but I wouldn´t know how to use it for this purpose.
I don’t want to use a simple delay because I don’t know how long the shell command will take.

This is my automation:

- alias: notify_1_dafang_1_send_picture_door_open
  initial_state: true
  hide_entity: false
  trigger:
    - platform: state
      entity_id: sensor.front_door_node_1_front_door_status
      from: 'closed'
      to: 'open'
  action:
    - service: shell_command.dafang_1_get_snapshot_door_open
    - delay: '00:00:00'
    - service: notify.notify_1
      data_template:
        message: Front Door Snapshot 1 - 5 from Door open
        data:
          photo:
            - file: '/config/www/dafang_1/dafang_1_snapshot_door_open_1.jpg'
              caption: "
                *Open* Front Door Snapshot 1
                
                {{now().strftime('%Y-%m-%d %H:%M:%S')}}"
            - file: '/config/www/dafang_1/dafang_1_snapshot_door_open_2.jpg'
              caption: "
                *Open* Front Door Snapshot 2
                
                {{now().strftime('%Y-%m-%d %H:%M:%S')}}"
            - file: '/config/www/dafang_1/dafang_1_snapshot_door_open_3.jpg'
              caption: "
                *Open* Front Door Snapshot 3
                
                {{now().strftime('%Y-%m-%d %H:%M:%S')}}"
            - file: '/config/www/dafang_1/dafang_1_snapshot_door_open_4.jpg'
              caption: "
                *Open* Front Door Snapshot 4 
                
                {{now().strftime('%Y-%m-%d %H:%M:%S')}}"
            - file: '/config/www/dafang_1/dafang_1_snapshot_door_open_5.jpg'
              caption: "
                *Open* Front Door Snapshot 5
                
                {{now().strftime('%Y-%m-%d %H:%M:%S')}}"
                
    - delay: '00:00:02'
    - service: notify.notify_1
      data_template:
        message: Get current snapshot?    
        data:
          inline_keyboard: 
            - 'Get 1 current snapshot or get:/dafang_1_get_1_snapshot'
            - ' 2:/dafang_1_get_2_snapshots, 3:/dafang_1_get_3_snapshots, 4:/dafang_1_get_4_snapshots, 5:/dafang_1_get_5_snapshots'
            - 'Home:/home'   

Shell command:
dafang_1_get_snapshot_door_open: 'bash /config/sh_bash_commands/dafang_1_get_snapshot_door_open.sh'

This is my bash script:

#!/bin/bash

curl --insecure https://root:[email protected]/cgi-bin/currentpic.cgi --output /config/www/dafang_1/dafang_1_snapshot_door_open_1.jpg
cp /config/www/dafang_1/dafang_1_snapshot_door_open_1.jpg /config/www/dafang_1/saved/`date '+%Y-%m-%d_%H.%M.%S'`_dafang_1.jpg

curl --insecure https://root:[email protected]/cgi-bin/currentpic.cgi --output /config/www/dafang_1/dafang_1_snapshot_door_open_2.jpg
cp /config/www/dafang_1/dafang_1_snapshot_door_open_2.jpg /config/www/dafang_1/saved/`date '+%Y-%m-%d_%H.%M.%S'`_dafang_1.jpg

curl --insecure https://root:[email protected]/cgi-bin/currentpic.cgi --output /config/www/dafang_1/dafang_1_snapshot_door_open_3.jpg
cp /config/www/dafang_1/dafang_1_snapshot_door_open_3.jpg /config/www/dafang_1/saved/`date '+%Y-%m-%d_%H.%M.%S'`_dafang_1.jpg

sleep 4

curl --insecure https://root:[email protected]/cgi-bin/currentpic.cgi --output /config/www/dafang_1/dafang_1_snapshot_door_open_4.jpg
cp /config/www/dafang_1/dafang_1_snapshot_door_open_4.jpg /config/www/dafang_1/saved/`date '+%Y-%m-%d_%H.%M.%S'`_dafang_1.jpg

curl --insecure https://root:[email protected]/cgi-bin/currentpic.cgi --output /config/www/dafang_1/dafang_1_snapshot_door_open_5.jpg
cp /config/www/dafang_1/dafang_1_snapshot_door_open_5.jpg /config/www/dafang_1/saved/`date '+%Y-%m-%d_%H.%M.%S'`_dafang_1.jpg

What I would do is create an input_boolean, set it to false before executing the shell_command, then wait for it to become true. Then add to the end of the shell script a curl command that will set the input_boolean to true via HA’s REST API.

1 Like

Thank you I will try that.

But meanwhile I can’t access the camera from hassio anymore.
Running the curl command via ssh results in a time out.

core-ssh:~# curl --insecure https://root:[email protected]/cgi-bin/currentpic.cgi --output /config/www/dafang_1/dafang_1_single_snapshot.jpg -v
*   Trying 192.168.1.249...
* TCP_NODELAY set
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:02:12 --:--:--     0* 
connect to 192.168.1.249 port 443 failed: Operation timed out
* Failed to connect to 192.168.1.249 port 443: Operation timed out
* Closing connection 0
curl: (7) Failed to connect to 192.168.1.249 port 443: Operation timed out

Running the curl under Windows in cmd works.
I also restarted hassio, the camera and my router, nothing helps.

It suddenly stopped working, any ideas why?