How best to deal with MQTT sensor name changing after battery change

I found a simple and relatively elegant solution:

  1. First, I used my rename_device+entities script (see Why do I get error setting state_class 'measurement' and device_class 'volume' - #11 by puterboy) to give the relevant devices meaningful names and remove the channel-specific portion of the name.
    This routine allows one to change all the name specific components that include the channel (including friendly_name, name_by_user, unique_id, etc.). Similarly for the MQTT discovery topic and message. This way there is no channel dependency in the internal naming conventions.
    For example, I renamed LaCrosse-TX141THBv2-1-53 to LaCrosse-TX141THBv2-KitchenFridge
    This only needs to be done once to create channel-invariant naming – though it is not strictly necessary, if you are OK with having legacy channel names present in your device and entity names even as the actual numbers change when you change your battery.

  2. In the MQTT discovery message, I set the state topic to coincide with the existing channel number.
    "stat_t": "+/+/RTL_433toMQTT/LaCrosse-TX141THBv2/1/153"
    This can be done automatically as part of the above rename_device+entities command or can be done manually using MQTT Explorer or mosquitto_pub or the HA service mqtt.publish

  3. Then any time the channel number changes (e.g., after changing batteries), all you need to do is repeat #2.

Note that if you are using something like rtl_433_esp32 you need to shut off auto-discovery so that you don’t get duplicate new sensors created corresponding to the default entity name containing channel numbers.