When the microcontroller is in Standby mode and an RTC interrupt occurs the WUF: Wakeup flag will be set by the hardware in the PWR control/status register (Page 162).
Bit 0 WUF: Wakeup flag
This bit is set by hardware and cleared by a
system reset or by setting the CWUF bit in the PWR power control
register (PWR_CR)
0: No wakeup event occurred
1: A wakeup event was
received from the WKUP pin or from the RTC alarm (Alarm A or Alarm B),
RTC Tamper event, RTC TimeStamp event or RTC Wakeup).
Initially this is cleared by a system reset so that is why your first cycle is OK. But after a wake-up from standby you have to clear it manually using the CWUF bit in the PWR control register. If you do not do this then the controller will wake up at once as the this bit signals an occurred wake-up event.
You can access the register directly to set this bit or with HAL library the following macro can be used:
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
something like this is missing on start up. – Constriction