Custom integration, check if entity exists before creating it

Good day,

I am helping on the development of a custom integration. One thing that has been bothering me for a while is that I get an error Entity id already exists every time I reboot HA.

I believe it is caused by the following “for” loop:

for d in hilo.devices.all: 
     LOG.debug(f"Adding device {d}") 
     new_entities.extend(generate_entities_from_device(d, hilo, scan_interval)) 
     if d.has_attribute("power"): 
         # If we opt out the geneneration of meters we just create the power sensors 
         if generate_energy_meters: 
             create_energy_entity(d) 

I looked at the documentation but unless I looked wrong I was not able to locate a way to check if an entity already exists before creating it.

I’m not too sure how to check the entity registry and run the for loop against it.

Any pointer or help would be appreciated!

Im not sure being able to check the entity exists before creating it is your issue here. It maybe that you are trying to create the same entity more than once, in which case, you need to look at this. If you want to post a link to your full code, happy to have a look for you and give some pointers.

Thank you Mark. Sorry for the late reply I never received the notification.

Further digging has lead me to see that line 159 of this file is responsible

This calls line 24 in this file:

I am under the impression that line 29 of managers.py is not doings its job. Logging shows I’m not going through it at all.