Repeat automation if service-script fails

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:
          sequence:
            - service: switch.turn_on
              target:
                entity_id: switch.your_switch
            - delay:
                seconds: 10
          until: "{{ is_state('switch.your_switch', 'on') or repeat.index >= 5 }}"
2 Likes