3 Switches based on PV grid feed-in and operating hours

Heating control based on PV grid feed-in.
I have an instantaneous water heater with three heaters of 1kw each.
These should be switched on by the ESP depending on the PV grid feed-in.

In addition, the heating elements that have the shortest operating time should be used when switching on

and off in reverse order.

Pv feed > 1200 w for 2 minutes → switch on the heating element with the lowest operating hours.

wait 10 s and check again whether another heating element can be switched on.
what is the best way to go about it?

my code until now:

esphome:
  name: elektroheizung

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "fdf1873cf33913c147f6761493bee47c"
  
# Enable Web server.
web_server:
  port: 80
  
wifi:
 # use_address: oel-fuellstand-sensor
  networks:
    - ssid: !secret wlan_ssid        
      password: !secret wlan_password    

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Elektroheizung Fallback Hotspot"
    password: "W5ywu1mCj6ta"

captive_portal:

switch:
  - platform: restart
    name: "Neustart"
    
  - platform: gpio
    pin: 25
    id: heizstab_1
    name: 'Heizstab 1'
    restore_mode: ALWAYS_OFF 
    on_turn_on:
      while:
        condition:
          switch.is_on: heizstab_1
        then:
          - globals.set:
              id: global_Betriebsstunden_Heizung_1
              value: !lambda |-
                    return id(global_Betriebsstunden_Heizung_1) +1;
          - delay: 60s
    
  - platform: gpio
    pin: 26
    id: heizstab_2
    name: 'Heizstab 2'
    restore_mode: ALWAYS_OFF 
    on_turn_on:
      while:
        condition:
          switch.is_on: heizstab_2
        then:
          - globals.set:
              id: global_Betriebsstunden_Heizung_2
              value: !lambda |-
                    return id(global_Betriebsstunden_Heizung_2) +1;
          - delay: 60s
    
  - platform: gpio
    pin: 33
    id: heizstab_3
    name: 'Heizstab 3'
    restore_mode: ALWAYS_OFF 
    on_turn_on:
      while:
        condition:
          switch.is_on: heizstab_3
        then:
          - globals.set:
              id: global_Betriebsstunden_Heizung_3
              value: !lambda |-
                    return id(global_Betriebsstunden_Heizung_1) +3;
          - delay: 60s
    
  - platform: gpio
    pin: 32
    id: pumpe
    name: 'Pumpe'
    restore_mode: ALWAYS_OFF 
    on_turn_on:
      while:
        condition:
          switch.is_on: pumpe
        then:
          - globals.set:
              id: global_Betriebsstunden_Pumpe
              value: !lambda |-
                    return id(global_Betriebsstunden_Pumpe) +1;
          - delay: 60s
    
#Globale Variablen
globals:
  - id: temp_freigabe
    type: bool
    restore_value: yes
    initial_value: '0'
  - id: global_Betriebsstunden_Heizung_1
    type: int
    initial_value: '0'
    restore_value: yes
  - id: global_Betriebsstunden_Heizung_2
    type: int
    initial_value: '0'
    restore_value: yes
  - id: global_Betriebsstunden_Heizung_3
    type: int
    initial_value: '0'
    restore_value: yes
  - id: global_Betriebsstunden_Pumpe
    type: int
    initial_value: '0'
    restore_value: yes
  
  - platform: uptime
    name: Uptime Sensor
    id: uptime_sensor
    update_interval: 60s
    
 #Betriebsstundenzähler   
  - platform: template              
    name: "Betriebsstunden Heizung 1"
    id: Betriebsstunden_Heizung_1
    unit_of_measurement: min
    accuracy_decimals: 0
    lambda: |-
        return id(global_Betriebsstunden_Heizung_1);
      
  - platform: template              
    name: "Betriebsstunden Heizung 2"
    id: Betriebsstunden_Heizung_2
    unit_of_measurement: min
    accuracy_decimals: 0
    lambda: |-
        return id(global_Betriebsstunden_Heizung_2);

  - platform: template              
    name: "Betriebsstunden Heizung 3"
    id: Betriebsstunden_Heizung_3
    unit_of_measurement: min
    accuracy_decimals: 0
    lambda: |-
        return id(global_Betriebsstunden_Heizung_3);
        
  - platform: template              
    name: "Betriebsstunden Pumpe"
    id: Betriebsstunden_Pumpe
    unit_of_measurement: min
    accuracy_decimals: 0
    lambda: |-
        return id(global_Betriebsstunden_Pumpe);

#PV Überschuss von Homeassistant
  - platform: homeassistant
    id: grid_power_supplied
    name: "PV Überschuss"
    entity_id: sensor.grid_power_supplied
    unit_of_measurement: W
    accuracy_decimals: 0