How to identify MQTT entity when object_id has changed?

I have some scripts on my router that send some status data via MQTT to Home Assistant. The scripts support discovery messages and use object_ids and also unique_ids. This works so far.

Now I want to be able to request the state of some of those entities as they may have changed from within Home Assistant. Therefore I have a twig template to request the entities of the device via curl. This also works.

But only as long as the object_id hasn’t been changed. As soon as the user changed the object_id, I cannot safely identify the read data any more. I can still retrieve information about all entities, but I cannot safely match the read data with the according data on the router any more.

How do you handle such cases?

Provide a unique identifier to the entity, and supply it in a place you can access it. Like an attribute.

Indeed, that was the way I want to go now. Maybe it could make sense to expose the unique_id in future releases so that one can avoid building this manually.

Typically, you don’t change the object_id of the entity in question as it’s meant to be the identifier for the entity.

I’m using a setup that may be a bit special: I’m creating the device and its entities via MQTT device discovery once. Every now and then I query the formerly created switches using curl by sending a template script.

That seemed simpler to me than subscribing to a topic via MQTT but I need a possibility to safely identify and map the entities with my topics. The problem is that I can set the desired object_id via MQTT, but I get no feedback whether it was valid or it already existed and got any suffix. Moreover the user could change the object_id and I would also loose the mapping.

I have a working solution with a custom uid now.