I’m running environment sensors on multiple esp32 microcontrollers around my house. In the sensor: yaml, I have a section like
tvoc:
name: "TVOC (ENS160)"
id: ens160_tvoc
which then creates an entity in HA labeled sensor.tvoc_ens160
This would be fine except I have multiple of these sensors, which then creates entity IDs like sensor.tvoc_ens160_2, sensor.tvoc_ens160_3 etc. with no indication of which device they are connected to.
I was hoping to solve this by adding ${device_name} before the id for each sensor, but that’s not allowed since my device name contains hyphens. Then I thought to replace the hyphens in the device name with underscores, but that’s not allowed either.
So now I’m at a loss. I know I could manually add the device name to each sensor, but that would be a nightmare with so many devices. Any thoughts on how can I generate unique entity IDs for each sensor?
if you have multiple ESPs it’s a wise idea to work with
substitutions:
devicename: esplivingroom # espbedroom 1 unique name for each ESP device
esphome:
name: ${devicename}
tvoc:
name: ${devicename}_ens160_tvoc
so that you get unique entities
And you could go a step further and exclude the sensor part of your YAML into a separate file which you then
sensor:
- <<: !include ens160_tvoc.yaml
so that your main YAML gets pretty small and unique per ESP device while the ens160 part remains static across as many devices as you like.
and you musn’t use ‘devicename’ it could be even ‘a’ or ‘replaceme’ or whatever you like.
Search on the ESPhome site for ‘substitutions’ and ‘includes’ if you need more information.
And yes I’d recommend to fix it on the ESP side against try to split things later inside HA.
Thanks @justone After reading your answer and reading through Configuration Types > Substitutions, I realized that I could just create a separate substitution key-value pair for sensor_id that used underscores instead of dashes.
Unfortunately, after I did that, I realized that the sensor’s ID is not the source of my problem. It looks like HA is actually building the entity ID from a reformatted version of the name variable. Here again, I could add some sort of device ID to the name variable, but that will add a lot of unnecessary text to my dashboard.
So, I’m back to square one – but realizing that this may not be solvable in ESPHome but rather require a change to how HA creates entity IDs.
This will find all sensor entity_ids ending in “tvoc_ens160” and replace their names with “TVOC (ENS160)”. The entity ids however retain the device name and tvoc_ens160.
Thanks, @tom_l The solution you offer is workable, but it would require creating customize_glob entries for each type of sensor (both now and in the future).
Is there any option for changing default the way HA creates entity IDs to be something like sensor.<device name>_<sensor name> ?
It would be useful if the optional “id” in ESPHome was used as the “entity_id” in HA, or the current behaviour if not set. Then you could have all the names matching, but differentiate on id.
However, that would be like calling all of the lights in your house “Light” and each having a separate entity_id. I don’t really understand why you want them all to have the same name rather than, for example, “Lounge TVOC”’, “Kitchen TVOC”, etc. This is what I do. For example, each matching device has this at the start:
In my case, I tend to look at a lot of my sensors in the overview dashboard where they are already grouped by room, so having the room name in the sensor name just adds unnecessary clutter. And for the few where I do want custom cards, I tend to make one dashboard for the the specific type (like air quality) and then one view for each room, so again having the room name is superfluous.
The only time I really need to see the room name is when I’m setting up the card, which is why it would be helpful to have it (via the device name) in the entity ID.
I agree that it’s strange / unfortunate that ESPHome uses a slugified version of the friendly name rather than the ID field for the entity ID. I’d be interested to hear any admins on that project comment on the thinking there.
Interestingly, I noticed that Tasmota devices do include the device name in the entity ID (but not the friendly name) for each sensor, so there does seem to be some precedent for that. Don’t know if that’s done on the device side or the HA Tasmota integration, but I guess it does seem to indicate that it’s something that would need to be changed specifically for ESPHome and not in HA overall.
I just ran into a situation which I think is related to the discussions here. I set up an ESP286 with five Dallas temperature sensors. As part of the setup, I gave them generic names: “temperature_1” through “temperature_5.” After I’d installed the tangled mess of wires, I wanted to give them new names which reflect which location they were reporting on (e.g.; “cold_water_in” and “hot_water_out.”)
Unfortunately, they still show up in HA with the temporary “temperature_” names.
Is there any way to rename them in place, or delete the old names and start over?
hi, @cweinhofer any solutions to your problem? I’m facing the same situation and thinking of either a “entity_add_mac_suffix” feature similar to “name_add_mac_suffix” on ESPHome side, either some “entity_add_name_suffix” configuration on HA side.