System.powerSource() return when mulitple power sources are available

Hey,

to safe my battery life i wanted to detect whenever my system is not powered via vin, but the lipo battery to go into longer sleeps.
Before booting the system I have a power supply on Vin and the lipo connected.
System.powerSource() return Vin as power source.
After removing the power supply from Vin, System.powerSource() is showing that it is battery powered.
But if i put the power supply back into Vin, System.powerSource() still is showing that it is battery powered.

What is the logic behind the powerSource() function, how is it deciding what the current power supply is?

Thanks

The System.powerSource() value should switch back automatically. What device (Boron, E Series, etc.) and what version of Device OS are you using? Does the charge LED come back on when you reconnect VIN power?

I am using an E Series dev board with 1.5.1. The charge LED is turning on. But even after the LED turn off and the lipo is loaded again, the mistery remains.

One thing out found out:
This was my original code:

bool disconnect=false;

void setup(){
if (!waitFor(Particle.connected, 180000))
    {
        Serial.println("Cloud connection problem");
        System.reset();
    }
}

void loop(){
    delay(100);
    powerSource = System.powerSource();
    batteryCheck.loop();
    delay(1000);
    if(powerSource==POWER_SOURCE_VIN){
        if (disconnect)
        {
            Cellular.connect();
            Particle.connect();
            disconnect = false;
        }
    // sending data regulary
    }
    else //powered by battery, going to sleep and checking every minute if Vin is available again
   {
        disconnect = true;
        Particle.disconnect();
        Cellular.disconnect();
        SystemSleepConfiguration config;
        config.mode(SystemSleepMode::STOP)
            .gpio(D2, RISING)
            .duration(1min);
        SystemSleepResult result = System.sleep(config);
        delay(100);
    }
}

For debuging i added a led on D3 and made the powerSource() visible by blinking the amount according to the return value of the function. I added it in the beginning of the loop():

    delay(100);
    powerSource = System.powerSource();
    batteryCheck.loop();
    delay(1000); 
    for(int i=0; i<powerSource;i++){
        digitalWrite(D3, HIGH);
        delay(300);
        digitalWrite(D3,LOW);
        delay(300);
    }

For some reason it then works with giving back the right System.powerSource(). Really weird. Anyways. Also when I upload the code via USB (only data cable without 5V line) and remove the cable after upload, the System.powerSource() returns POWER_SOURCE_USB_HOST for ever. I have to reset the particle to get it to notice VIN again.