Mi IO timing issues when powering up (state changes from OFFLINE to ONLINE)

Hi!
I have set up several yeelight devices using the miio binding.
One device is not powered permanently and triggered by an old power switch. Please don’t ask why (relais, fans,…).
I have a rule that changes the state of an item from OFF to ON if the yeelight device powers up (or to be more exact: when the device status changes to ONLINE) and a rule in the opposite direction.
Strangely, this does not work all the time.

Here is the log export for a working condition:

2022-07-17 07:57:56.172 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'miio:generic:04A0AF6B' changed from OFFLINE (COMMUNICATION_ERROR) to OFFLINE
2022-07-17 07:57:56.178 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'miio:generic:04A0AF6B' changed from OFFLINE to ONLINE
2022-07-17 07:57:56.180 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'BadDeckenlampe_Power' changed from OFF to ON

And this is one where the rules fail:

2022-07-17 07:31:10.547 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'miio:generic:04A0AF6B' changed from OFFLINE (COMMUNICATION_ERROR) to OFFLINE
2022-07-17 07:31:10.548 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'miio:generic:04A0AF6B' changed from OFFLINE to OFFLINE (COMMUNICATION_ERROR)
2022-07-17 07:31:10.549 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'miio:generic:04A0AF6B' changed from OFFLINE (COMMUNICATION_ERROR) to ONLINE
2022-07-17 07:31:10.559 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'BadDeckenlampe_Power' changed from OFF to ON
2022-07-17 07:31:10.559 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'BadDeckenlampe_Power' changed from ON to OFF

It seems like triggering the power switch does not always result in only an ONLINE event but sometimes also to a “COMMUNICATION ERROR”.

Any idea why that happens? Has it something to to with the timing of the wifi connect of the yeelight device? Any ideas how I could solve the issue?

Don’t really know.

The communication error is coming when the device does respond to a ping, but not respond to commands. Maybe during the startup it is behaving that way