Issue executing shell commands from automation

I have a Lutron pico remote to control a ceiling fan light. The two buttons are obviously light on and light off. I have shell commands defined in my configuration.yaml as follows:

Shell_command:
  curl_light_on: >
    curl -X PUT "http://<bridge ip>/v2/devices/<fan id>/actions/TurnLightOn" -d "{}" -H "BOND-Token: <bridge token>" 
  curl_light_off: >
    curl -X PUT "http://<bridge ip>/v2/devices/<fan id>/actions/TurnLightOff" -d "{}" -H "BOND-Token: <bridge token>"

If I execute either of these commands from the developer tools, action tab, they work 100% of the time. They’re absolutely perfect!

In my automation I have a condition as follows:

- conditions:
        - "{{ trigger.id == 'on_button' }}"
        sequence:
          - action: shell_command.curl_light_on

When this code is called it works sometimes. I can verify that the button is recognised via announcements. The button is recognise on every single press but I might have to push the button maybe three times to get the light to turn on.

Could anyone explain how it’s possible that the curl command when executed via the debugger works 100% of the time but when executed via the automation only works 30% of the time?

i think rest_command is better suited for HTTP actions like yours

rest_command:
  light_on:
    url: "http://<bridge ip>/v2/devices/<fan id>/actions/TurnLightOn"
    method: PUT
    headers:
      BOND-Token: "<bridge token>"
    payload: "{}"
    content_type: "application/json"
- conditions:
    - "{{ trigger.id == 'on_button' }}"
  sequence:
    - service: rest_command.light_on

in yours try adding a delay

script:
  turn_on_light_via_curl:
    sequence:
      - delay: "00:00:01"  # 1 second delay
      - service: shell_command.curl_light_on

and in your automation

- conditions:
    - "{{ trigger.id == 'on_button' }}"
  sequence:
    - service: script.turn_on_light_via_curl

in your automation use mode: queued or restart

I really appreciate you going to all the effort of showing me the code like this. Unfortunately, the rest command is working the same as the curl command. That is to say, inconsistently. It works 100% of the time from the debugger but only occasionally when I actually push the button.

Have you looked at the automation traces to see if it’s actually doing what you expect?

I have. It is executing as expected. I have a few HA Voice modules and they announce the button presses in the automation. In the Turn On condition, the rest call is first made then the announcement of which button was pushed is made. Every button push results in an announcement and no errors are generated.

Could you download a trace of one of the failed runs from the top-right of the Trace screen:

and paste it here, properly formatted with the </> button?

I am pleased to say that the problem is solved! Thanks to the post by WarLion it got me to thinking about delays. When I redid the automation adding a 1 sec delay before and after the shell call I found it worked every time! I am still playing around to see if I need both delays or just one and how long it needs to be. But at least the buttons work every time now!

Update: The delay after the shell command call is unnecessary. A one sec delay before the call is the key.

glad you solve your problem