I have the same problem with my tellstick sensors, however their value does not say “undefined” it just stays the same temperature as the last reading. I assume your solution wont work for this case?
Just want to make sure that you actually have the sensor say “undefined” as a value when its battery died
i never had any battery operated sensors in HA.
and my solution was something i figured out for someone else, 4 years ago!
i doubt if it still would work with the amount of changes that HA made in those 4 years.