Helper based time switch fail - backup strategy

I have an automation to turn on/off the oil supply to my range cooker and it mostly works great, however I have had a number of instances where the turn on action fails due to network upgrades during the night coinciding with the switch on/off action. The automation is;

alias: Aga to automatic mode
description: ""
trigger:
  - platform: state
    entity_id:
      - schedule.aga_on_off
condition:
  - "{{ trigger.to_state.state in ['on', 'off'] }}"
action:
  - service: switch.turn_{{ trigger.to_state.state }}
    target:
      entity_id: switch.switch_2f4bc2

I would appreciate some help to modify or have a simple version of the above to re-trigger the service call based on the state of the switch (ie, if the switch becomes available then it forces to the current state of the schedule).

I have tried something like:

alias: Aga-on-off check based on switch
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.switch_2f4bc2_energy_apparentpower
    from: unavailable
condition: []
action:
  - service: switch.turn_{{ state.entity_id.schedule.aga_on_off }}
    data: {}
    target:
      entity_id: switch.switch_2f4bc2
mode: single

Hoping that when the sensor changes from unavailable, that the switch will be set to the current state of the schedule. I have searched the documentation but without success.

I put together this library to do this. I use it for all my switches, lights, climate controls etc., because command do not always work. Using it requires some skill with shell.

Here is a specific example generated by that library

# Script to turn a switch on
# Template Parameters
# switch
# turn_off
# off
rs_switch_turn_off:
  description: "reliable switch turn_off"
  fields:
    entity_id:
      description: "The switch entity"
      example: "switch.entity_id"
    message_id:
      description: "Message Text Destination"
      example: "input_text.rs_switch_turn_off_message"
    retry_count_id:
      description: "Retry Counter"
      example: "counter.rs_switch_turn_off_retry"
    failed_count_id:
      description: "Failed Counter"
      example: "counter.rs_switch_turn_off_failed"
    call_count_id:
      description: "Success Counter"
      example: "counter.rs_switch_turn_off_success"
    timeout_seconds:
      description: "Default timeout in seconds"
      example: "5"
  variables:
    max_retry: 3
  mode: parallel
  sequence:
    - service: counter.increment
      continue_on_error: true
      data_template:
        entity_id: "{{call_count_id}}"
    - repeat:
        sequence:
          - service: switch.turn_off
            continue_on_error: true
            data_template:
              entity_id: "{{entity_id}}"
          - choose:
              - conditions: "{{repeat.index == 1}}"
                sequence:
                  - service: system_log.write
                    data:
                      level: info
                      message: "turn_off {{entity_id}} - attempt 1"
                      logger: service
                  - service: input_text.set_value
                    data_template:
                      entity_id: "{{ message_id }}"
                      value: "turn_off - attempt 1"
            default:
              - service: counter.increment
                continue_on_error: true
                data_template:
                  entity_id: "{{retry_count_id}}"
              - service: system_log.write
                data:
                  level: warning
                  message: "turn_off {{entity_id}} - attempt {{repeat.index}}"
                  logger: service
              - service: input_text.set_value
                data_template:
                  entity_id: "{{ message_id }}"
                  value: "turn_off - attempt {{repeat.index}}"
          - wait_template: "{{ is_state(entity_id, 'off') }}"
            timeout:
              seconds: "{{ timeout_seconds * repeat.index }}"
            continue_on_timeout: true
          - if:
              - "{{wait.completed}}"
            then:
              - service: system_log.write
                data:
                  level: info
                  message: "turn_off {{entity_id}} - success"
                  logger: service
              - service: input_text.set_value
                data_template:
                  entity_id: "{{ message_id }}"
                  value: "turn_off - success"
        until: "{{ is_state(entity_id, 'off') or repeat.index == max_retry}}"
    - condition: "{{ not (is_state(entity_id, 'off')) }}"
    - service: system_log.write
      data:
        level: error
        message: "turn_off {{entity_id}} - failed"
        logger: service
    - service: input_text.set_value
      data_template:
        entity_id: "{{ message_id }}"
        value: "turn_off - failed"
    - service: notify.sms_telegram_admin
      data:
        message: "{{ state_attr('zone.home','friendly_name') }} turn_off {{entity_id}} - failed"
    - service: counter.increment
      continue_on_error: true
      data_template:
        entity_id: "{{error_count_id}}"
# Script to turn a switch on
# Template Parameters
# switch
# turn_off
# newwell
# 10
rs_newwell_switch_turn_off:
  description: "reliable newwell switch turn_off"
  mode: queued
  variables:
    start_time: "{{ now().timestamp() }}"
  sequence:
    - service: script.rs_switch_turn_off
      continue_on_error: true
      data_template:
        entity_id: switch.newwell
        message_id: input_text.rs_switch_newwell_message
        retry_count_id: counter.rs_switch_newwell_retry
        error_count_id: counter.rs_switch_newwell_err
        call_count_id: counter.rs_switch_newwell_calls
        timeout_seconds: 10
    - event: rs_call_complete_switch_newwell
      event_data:
        duration: "{{ ((now().timestamp() - start_time)*1000)|round(0) }}"

Thank you for this and I will dive into your solution when I get time. I have managed to write an automation that will ensure that the switch is turned on when the switch becomes available again, however, it would be useful to understand how to use the actual helper status as the switch type rather than having a specific condition:

alias: Aga Ensure on if it should be on
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.switch_2f4bc2_energy_apparentpower
    from: unavailable
condition:
  - condition: state
    entity_id: schedule.aga_on_off
    state: "on"
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.switch_2f4bc2
mode: single