How to programmatically assign a unique ID?

I am programmatically creating entity entries (via pyscript’s set.state() - it works) but they do not have a unique_id.

Is there a way to provide it when creating the entity?

I tried to add unique_id as an attribute (according to Entity | Home Assistant Developer Docs). It is accepted (as an attribute) but is is not the unique ID of the entity (apparently):

image

The text states that there is no unique ID and therefore it cannot be managed from the UI

image

The attribute is set and even recognized (or not?) as “Unique ID”

1 Like

Do you know if it is possible to assign a unique_id by hand? I would be interested in that for template entities, since they don’t have the friendly_name attribute any more.

You can’t assign a unique_id using this method. You have to set the attributes when setting the state or use customize to assign attributes like friendly_name, etc.

If you’re referring to built in template entities, they accept a unique_id in the configuration of them.

put the friendly_name in the attributes for the configuration

1 Like

Thank you, but I am not sure I understand:

  • You can’t assign a unique_id using this method → you mean by adding unique_id when creating (by setting it) the entity?
  • You have to set the attributes when setting the state → I am not sure how this related to the above?

You can’t create a unique_id through the pyscript set state. That method only adds an item to the state machine. Technically, home assistant still doesn’t know about the entity using this method.

You’re trying to edit something on that entity, correct? Instead of using the UI to set what ever you’re trying to set, you use the associated attribute via the set state in pyscript.

OK thank you.

Ah yes sure - this is what I am doing. It just bothers me that I cannot also control it from the UI (to set an icon from there for instance) and generally speaking - that such entities are not defined in HA as well as the others are.

I started to read about integrations and wee whether I could not go that way.

Thank you for your help!

it can if you define the entity on startup through the “New” startup methods, i.e. the startup process has a unique_id and it creates a config_entry that attaches the unique_id to the state machine entity. This is the piece you’re missing. Old integrations or creating the entity through un-conventional means will not do this.

so… if you want to get this working you can probably fake out that config_entry through a template sensor that never updates.

if your entity_id is sensor.x_y_z

template:
- sensor:
  - unique_id: something_unique
    name: X Y Z
    state: "{{ 'unknown' }}"

Just make sure that your entity_id will be generated by taking the name and sluggifying it. I.e. replacing spaces and special characters with _ and making everything lowercase. In the example above, my entity_id is sensor.x_y_z, so I named it X Y Z. On startup (Not reloading) that should create the config entry and the sensor will never update. Then use pyscript to update it to your hearts content. If you add that template and the entity already exists through pyscript, it will create an entity with _1. This is why you want to restart and not reload, as this ensure’s the entity doesn’t exist in the state machine.

I have several entities where I forced a unique_id manually (in configuration.yaml). My problem here is that I do not know what the entities will be upfront - they will be generated automatically.

I could consider doing this the manual way (because the dynamism I mentioned is not that enormous) but this is not really scalable and I would prefer to handle that programmatically end to end.

Except of course if

(…) probably fake out that config_entry through a template sensor that never updates

could be done from a script (I mean adding to HA, not programmatically edit configuration.yaml by parsing the file).

Well, I don’t know pyscript very well, it may give you access to creating config entries

OK, so you mean that it is conceptually possible to create config entries outside of configuration.yaml (in other words - programmatically directly in HA, not through parsing and rewriting a file).
I will investigate that, it would be an excellent solution.

FWIW, that’s what MQTT Discovery can do. You publish a payload, containing the configuration of the entity you want to create, to a specific “discovery” topic and Home Assistant will instantiate the entity (and register it). As long as the published payload exists on the MQTT Broker, the entity will exist.

I use MQTT Discovery to create dozens of entities (via scripts) and assign a unique_id to all of them (I also create devices this way).

1 Like

I do the same thing as 123

Ah yes, of course - thanks a lot for the input. I use MQTT discovery as an “end-user” (meaning that the devices are registered through the integration, or the device itself) but I also sent my own discovery messages in the past and it worked fine (the structure is very simple).

This means that I could set them up via MQTT (it is asynchronous but near real-time and good enough for me) and change their values either via MQTT or directly.

Hi
Nearly one year ago since your post, but came here following all the subjects on the KLF200. Succeeded to add my 16 covers, but none of them got a unique ID. Would you mind explaining with a few more details how to use MQTT Discovery to create a device, (a cover in my case) to have a unique ID created by HA? If I understood well, I could then use it as a template with a valid unique ID to include my cover in HA

The core of the documentation for MQTT discovery is here: MQTT - Home Assistant
You need to craft a specific message (the discovery message) that will register your device with HA via MQTT and it will appear as a device in your dashboard.

This is really useful because you can create any new entity this way. This may be a problem for you as your entities are already there. I do not know how they were added to HA (an integration? they sent a MQTT discovery?) but if you want to do it your self you need to disable that.

If you want an example, I wrote long ago a bit of code that does the registration. It starts here: meross-local-mqtt/meross.py at 17ffb449652c280aa7306e4393db785e309fe52b · wsw70/meross-local-mqtt · GitHub

It could actually be useful for you if you want to build the entities yourself (that was one of the reasons for that script) and run it via pyscript. You may want to read the README as there are some details about the messages sent by the various components

Hi
Thanks for passing by.
The entities were added to HA by the Velux integration. All my 17 covers were added without any unique ID.
I managed to add them to the dashboard, so I can control them quickly. But I struggled for literally hours to understand that they were in HA, but hidden. (Uncountable resets, new installs, stupid ssh command lines, pure joy!).
I’m currently learning how to deal with HA, as my home is currently managed by Jeedom. It’s been nearly 10 years, so the work to move everything is quite huge. MQTT could help me a lot to make them communicate with each other during the transition.
Thanks for the several link, I will try to go through all of them tonight.