The macro goes in the template itself. I’ve got an example at Home-AssistantConfig/packages/battery_alert.yaml at master · notoriousbdg/Home-AssistantConfig · GitHub that should help. As far as reducing the amount of duplication, you might be able to use yaml anchors and references (more info at daemonl). Look at the usage of &low_battery_check and *low_battery_check in the package above.
That being said, you might be interested in that package as a whole since it automates the entire process of creating sensors for batteries as well as notification when low. You can see more about that in this thread or in comments in the package itself: