First we need to establish the type of key that you have - diamond type or rubber covered with replaceable battery.
The diamond type does not have a replaceble battery but is rechargeble nevertheless. It can be recharged in the ignition for around 30 minutes or may need as much as 25 hours if it has been fully discharged according to BMW.
The rubber covered type is equipped with a red LED that flashes when an operation is initiated and is therefore easier to troubleshoot.
From what you describe, I assume the following:
That your door lock actuators are fine since you can use the manual function to lock/unlock the car with the key on the drivers door (integrated lock).
You may have a problem with the relays in your General Module (GM circuit board that are the brains of the lock/unlock and window functions in your car). The relay contacts are generally the first to go on the board and can be sourced separately for around $15 each - the GM is however around $500 and needs to be coded by the dealer. check this website for a full explanation of the GM and relays here (it is for the E46 but I am led to believe that a similar module resides in the E39s)
BMW E46, E85-Z4, E83-X3, E52-Z8 GM5 Module Repair for Door Lock/Unlock Problem
The key that is intermittent may not be transmitting a proper signal each time it is initiated. I am assuming that one of the keys is not working consistently as you describe in your post. In this case check that this is not repeatable on both keys. If so you can isolate the problem to one key.
I personally do not think that it is a problem with improper innitailization of your keys but you can try to re-innitialize them as per the following BMW procedure: (it is a bit terse and confusing - I suspect that it was translated from the original technical German document)
InitializationFunction
The initialization procedure allocates the key transmitter to the general module. During initialization, a code is generated in the key transmitter and then transferred to the general module. The code is stored in the key
transmitter and in the general module. The general module recognizes the keys belonging to the system on the basis of this code. Only commands from these keys are then carried out.
A maximum of 4 keys can be initialized for each vehicle. The codes of all other keys are deleted when a key is reinitialized. This means that all the keys of a system must be initialized simultaneously.
Initialization procedure
Carry out key transmitter initialization in the vehicle interior.
Unlock vehicle via central locking and close driver's and passenger's doors.
Briefly switch on terminal R (position two) in vehicle (maximum 5 seconds) and switch off again. => The system is now ready to be initialized.
Press and hold ”unlock” button on the key transmitter.
Point key transmitter in direction of antenna and, with the ”unlock” button pressed, press ”lock” button three
times within 10 seconds. => A new code is generated in the key.
Release both buttons. => The general module signals that initialization of the key transmitter was successful by locking and unlocking the central locking system.
Repeat the procedure as of Point 3 (= press and hold ”unlock” button on key transmitter) to initialize the other
keys. Terminal R must not be varied during this procedure.
The initialization procedure is terminated if terminal R is switched on.
Note
A voltage-independent memory in the radio-control key ensures that the initialization data is not deleted even
when the battery or rechargeable battery is discharged.
Good luck