Automatic switch power source for my PZEM-004T (For detect Power Outage)

Hi,everyone
I have a problem with my DIY Electricity meter (Pzem-004t + ESP8266). I made this for measure power usage in my home and detect power outage when I'm at not home. When the power outage happen. My DIY Electricity meter will send trigger to my Raspberry Pi running Home Assistant via MQTT (My Raspberry Pi and Access Point using UPS) to switch power source of my home to off-grid solar system and shutdown high power consuming electrical appliances such as refrigerator, heater or A/C (For extend battery time). The problem is my DIY Electricity meter will shutdown when the power outage is happen because using only main voltage. I need something like automatic switch circuit for switching from 5v power supply for DIY Electric meter to battery for make it trigger my Pi

I tried this
https://th.aliexpress.com/item/32969639665.html?spm=a2g0s.9042311.0.0.34e44c4dsHdgf3
using this for charging 18650 battery and supply power for my meter. But the circuit behind is getting hot and sometime it didn't supply power for my meter

Anyone have an idea how to solve this or another way?.Thanks