Reliably automate WiFi smart-plug

Is there a way to write an automation to say, “keep trying to turn this group on until it really ends up on”? Here’s my problem…

I have my landscape lighting on a couple of SmartLife WiFi smart plugs, and am using automation to turn them on 20 minutes before sunset and off at sunrise. Most of the time it works great, but once or twice a week it seems like one or the other of the plugs will miss one of the automation events, and will remain on (or off) when it shouldn’t be.

The weird thing is, when I check HA after noticing a problem, it has the correct status for the plug. That is, if it should have turned off, but in fact remained on, HA will accurately show it as “on”.

I’m guessing maybe there are momentary network (or Tuya server) glitches that cause some events not to make it through. Is there a better way to code the automations? Say, so that they keep asking the switch to turn off until it really ends up off? Or some other way I could be approaching this?

Here’s the relevant parts of my config:

configuration.yaml

# ...
tuya:
  username: !secret smart_life_username
  password: !secret smart_life_password
  country_code: 1
  platform: smart_life

groups.yaml

exterior_lights:
  name: Exterior lights
  entities:
    - switch.front_porch_light
    - switch.front_landscape_lights
    - switch.rear_landscape_lights

automations.yaml

- alias: Exterior lights on at dusk
  initial_state: True
  hide_entity: True
  trigger:
    platform: sun
    event: sunset
    offset: "-00:20:00"
  action:
    service: homeassistant.turn_on
    entity_id: group.exterior_lights

- alias: Exterior lights off at dawn
  initial_state: True
  hide_entity: True
  trigger:
    platform: sun
    event: sunrise
  action:
    service: homeassistant.turn_off
    entity_id: group.exterior_lights

Thanks for any help and suggestions.

[Home Assistant 0.83.3; HassOS 1.12 on Raspberry Pi]

Are you running Node-Red per chance? Not suggesting you start, but some people run both and this would be easy there.

I’m not currently running Node-Red. (Wasn’t even aware of it until now—will take a look, thanks!)

(I work in Python and JS by day, so scripting something is fine if that’s what it takes. But I was hoping there might be something I’d overlooked in HA automation to handle cases like this.)

I understand. Sometimes there can be a little bit of a religious war over node-red vs yaml automations. With node red you can do looping. So I would start a loop after a trigger then break the loop when all lights are on/off.

Again, not sure how to do it in scripting, maybe someone else better than me (just about anyone) can help out there.