Repeat automation if service-script fails

Hi all,
I got a simple automation, which activate a script during sunset (get shutters down).

alias: Sunset - shutters - bedroom - down
  - platform: sun
    event: sunset
    offset: '0'
condition: []
  - service: script.shutters_bedroom_down
mode: single

But from time to time, it fails to execute (in logs there is Error executing script) probably because the device is temporarily unavailable at that moment.

Is there a way - how to setup automation, which will repeat ist action if that script failed to execute ?

Thank you.

Probably or for certain?

Unless it indicates that its state value is unavailable, what will you use to determine if script.shutters_bedroom_down should be executed again?

My idea is to repeat to execute scrip for example every 10s until success.

What tells you it was a success or failure?

Log as I mentioned.

I’m going to rephrase my question one more time and if I don’t get a useful answer, I’ll let someone else help you with your problem:

What is the entity you have created to detect the presence of the error in the Log?

Ou sorry, we misunderstand.
There is a switch, which is controlled by this script (script turns this switch on for a certain time and than turns it back off).
I checked this switch’s entity history / logbook but it does not show that it was unavailable.

The challenge is that there is nothing that tells you the switch failed to turn on other than you visually inspecting the log for errors.

When the script commands the switch to turn on but it fails to turn on, does the switch’s state value still indicate that it is off?

If it continues to report off even after being commanded to turn on, you can try this technique. In your existing script where it turns on the switch, replace the service call with a repeat that turns on the switch every 10 seconds until the switch finally reports on or it has tried and failed 5 times.

      - repeat:
            - service: switch.turn_on
                entity_id: switch.your_switch
            - delay:
                seconds: 10
          until: "{{ is_state('switch.your_switch', 'on') or repeat.index >= 5 }}"

Thank you Taras, sounds promising.
I need a bit time to test it and let you know.

Thank you so far.

So after a couple days of testing it seems it works fine.

Thank you again.

Best regards Adam.

1 Like