Great!! It was the lan connection! The bulbs to switch on/off are wifi bulbs and without a lan connection probably the script hangs.
Yes, I have to put a try catch inside the loop (or somewere else) to check the lan connection.
Thank you very much, you give me my sleep back