Best Practise for renaming unique_ids of YAML-based sensors?

My Integration for Sungrow Inverters uses the modbus integration and implements ~150 sensors with unique_ids given.

The integration grew over almost two years and now I want to consolidate the naming to ease maintanance (e.g., add a second inverter by find and replace “sg1” with “sg2”) and readability.

Simply changing the unique_id causes new sensors to appear named *_2 (which is not suprising, as altough the name is the same, the internal unique_id changes…)

Q1: What is the best way to change unique_ids while linking the “new and old” sensors and keeping a common history?


- name: Sungrow device type code
  unique_id: dev_code
  slave: !secret sungrow_modbus_slave
  address: 4999 # reg 5000
  input_type: input
  count: 1
  data_type: uint16
  swap: word
  scan_interval: 600

Changing uid:
unique_id: sg1_dev_code

Causes the original sensor to go unavailable and the new sensor “sensor.sungrow_device_type_code_2” is created.

One (BAD) way to deal with this could be (maybe not 100% correct, but would work)

  • remove YAML integration
  • Restart HA (do not simply reload!)
  • Manually delete all 150 sensors via GUI
  • Restart HA (do not simply reload!)
  • Update yaml-integration
  • Reload HA

… but nobody wants to manually delete 150 sensors and loose the history…

Q2: Is there a way to make a “migration script” to ease things for other users of my integration?

hmm, does no one have any ideas about this? :thinking:

Sorry, can only speak for myself, I don’t know any good ways to handle this. But I must admit I haven’t had that many cases, and for the ten or so, that I had to rename in the past, I deleted the first entry (the one without the “_2”) and renamed the second one by deleting the “_2”…

But I can see, that seems to be more “challenging” with this count of sensors…