Script ending early when called from an automation

I have a problem that started a few months ago and I cannot get to the bottom of it.
I have an automation that checks the input from a button for controlling some switches which control a set of blinds.
This works in combination with an input_value.
The automation looks for the switch action (single press or double press). It looks at the input_value which can be Open, Closed or Raised.

When the button is double clicked and the Input_value is Raised, then the automation runs a script called “office_blinds_lower”

The script starts by turning off the “up” motor on the blinds, it then starts the down motor on the blinds, then waits 63 seconds, then stops the down motor on the blinds. This works perfectly when I just run the script.

The automation works fine. Checking the traces it follows the correct branch of a choose statement and runs the correct script.

The problem is that when the script “office_blinds_lower” is run from the automation, it somehow runs the command to stop the down motor after 2 seconds, not 63 seconds.

Here is the relevant bit of the automation:

    - conditions: 
      - condition: template
        value_template: "{{states.input_select.blinds_office.state == 'Closed' and trigger.from_state.state == 'Raised'}}"
      sequence: 
      - service: script.office_blinds_lower

Here is the relevant script

 office_blinds_lower:
  alias: Lower Office Blinds
  sequence:
  - service: switch.turn_off
    entity_id: switch.sonoff_1000xxxx_1
  - delay: 00:00:01
  - service: switch.turn_on
    entity_id: switch.sonoff_1000xxxx_2
  - delay:
      seconds: 63
  - service: switch.turn_off
    entity_id: switch.sonoff_1000xxxx_2

Why is the script behaving this way when run from the automation, and how can I fix it please?

Is it because you haven’t specified mode: and so it is aborting and restarting if the automation is re-triggering before the previous script completed?

try adding either:
mode: single
or
mode: queued
…depending on whether you want the script to complete before it re-runs.


Edit: Take a look here, but just realised that the default is single so it shouldn’t abort if re-triggered - sorry for the habit hole.

What do your logs say?

1 Like

I wouldn’t use states.input_select.blinds_office.state but states('input_select.blinds_office') (because of that) and for the delay:

delay: 63
1 Like

Thanks both.

I did some more poking around and cleaning up the code. I made all the script calls in the choose element of the automation simple “service: script.script_name” calls.

I updated the delay statements.

I did various experimenting with switching the order of things and leaving things out.

I am not sure why, but one of the changes worked and the scripts and automations now seem to be playing nicely again.

I’m sorry I can’t report exactly what fixed it but many thanks for your input. Much appreciated!

1 Like