How to deal with flaky switches in Home Assistant

published Dec 13, 2022, last modified Dec 14, 2022

Sometimes your smart switch can't be replaced. Here's a trick to cope with it: repeat the action!

Here's a tip coming straight from Gunnar's question in the Home Assistant community forum:


I try to switch on my boiler during the 4 cheapest hours with a smart plug. The smart plug seems to go to sleep, and often misses the turn-on message. Else the binary sensor for turning on seems to work. I will try to send the turn-on message a few times to the smart plug to make sure it wakes up and turns the boiler on. I have found a few examples of using repeat and repeat.index in the automation to do something similar, but I`m still not sure exactly how to do this. The code without repeat look like this:

- id: 'xxxxxxxxxxx'
  alias: Bereder på
  description: Slå på bereder innenfor de 4 billigste timer
  - platform: state
    - binary_sensor.billigste_4_timer
    from: 'off'
    to: 'on'
  condition: []
  - type: turn_on
    device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    entity_id: switch.bereder_kjeller_socket_1
    domain: switch
  mode: single

The answer to Gunnar's question is that the action in his automation should be replaced with the following code:

... some other stuff...
  - repeat:
      count: "16"
        - service: switch.turn_on
          data: {}
            entity_id: switch.boilah
        - delay:
            hours: 0
            minutes: 0
            seconds: 5
            milliseconds: 0

The code above would turn on the switch boilah sixteen times, with a wait of 5 seconds in between.