Such a solution can be done with an Arduino and a relay, for example:
https://botland.com.pl/439-arduino-seria-podstawowa-oryginalne-plytki
https://botland.com.pl/829-moduly-przekaznikow