Just a quick additional information.
While deeply testing the full functionality, I realized that if the NodeMCU goes out of main
power AND the magnetic contact connected to D3 (GPIO 0) is closed, the loaded configuration is lost. I found out that usage of GPIO 0, 2, 15 is deprecated because these pin have a meaning during reboot.
For this reason I moved my switch to D0 (GPIO 16) and modified accordingly the sketch of my previous post.
Sorry