Warning. This module has no halvanic insulation for low voltage part (MCU) and need to be handled carefully. Flashing is only allowed when disconnected from AC power
Classic DIY approach to build AC powered device is to use AC/DC module like Hi-Link HLK-PM01. These nice modules are very common and cheap. However they are relatively big. So if you think about something that is small enough to fit into a standard electric junction box along with other wire connections there it will not work.
Why build something that should fit the junction box? Well, this is another way to retrofit smart home light automation. It is possible to install a device into the legacy junction box and keep current switches and lamps. Besides that it was the primary solution for the HomeMatic system that I've selected for my apartment several years ago. Devices I've used were HM-LC-Sw2-FM and HM-LC-Sw1-FM and they have size about 53x53x30 and not without issue but they all were placed into standard junction boxes along with other wires.
Unfortunately these HomeMatic relay modules are not reliable enough and some are already replaced. Nowadays I have no more option to buy new modules like this and I decided I can build my own.
Core of the module is nrf52832 which was chosen here mostly because of size restrictions (MCU+Radio in one chip). Power part is built on top of the DCDC converter which provides no galvanic insulation in exchange for the compact size of required components. Converter transform 35-240 v AC to 5 v DC. 5 v drives generic relays and with additional LDO produces 3.3v for MCU. Another high voltage part senses 2 "switch lines" and converts 35-240v AC levels to 3.3v for MCU GPIO pins. Due to size restrictions, the antenna is represented by 1\4 wave length wire.
Final module size inside the 3d printed box is 43x35x18 mm. It is pin compatible with HomeMatic HM-LC-Sw2-FM but even smaller in size. For curiosity, Hi-Link HLK-PM01 alone is 34x21x15 mm. Note that the custom made device has two 5A relays and 6 screw terminals along with some other electronics.
Check here example of HomeMatic relay inside junction box:
Typical connection: