Your phone does that already. When the screen is off, 5g doesn't get used. If you turn your screen on in a 5g area, you should see the icon say 4g for probably under a second, then it swaps to 5g.
Also be aware the 5g icon with an outline and non solid inside means 4g is being used but 5g is avaliable. A solid sorta bubble means 5g. Even when the 5g icon is there, the phone can decide to use 4g if it thinks that's the better option, as indicated by the outlined 5g icon.
Plus 5g doesn't actually drain any extra power even when being used. Extra note to that, power save mode used to disable 5g, now it doesn't as modern 5g doesn't use extra power.