Getting device name in template

I am trying to get the name of the device in a template but am having some trouble. I have the entity ID and looking at the documentation the best I can find is the device_attr function. However when I do device_attr("some_entity_id", "name") it gives me the original name of the device, not the one I entered in the HomeAssistant UI. For an entity I see that there is the friendly_name attribute, but doing device_attr("some_entity_id", "friendly_name") returns None, so it seems like friendly_name is not for device objects.
So my question is how to get the name of the device I entered in the HomeAssistant UI? And yes I really do mean the device name and not the entity name.
For some devices where I can rename it externally (eg. Z-Wave devices in zwavejs) I could rename it outside HomeAssistant so that the name attribute contains the more meaningful name, but this is not possible for certain devices (eg. those using the ZHA integration).

1 Like

name_by_user

2 Likes

Thanks does what I need perfectly. A couple of follow up questions: Do I need to check whether this is defined and do manual fallback to name if not, or will HomeAssistant do this for me? Also where can I find a list of the attributes for a device as I never found this when looking in the docs.

no you don’t need to check to see if it’s defined, it’s always defined. Look in your .storage folder for core.device_registry. That file will show you what devices have what attributes.

config_entries
connections
identifiers
manufacturer
model
name
sw_version
hw_version
entry_type
id
via_device_id
area_id
name_by_user
disabled_by
configuration_url

4 Likes

I have a payload here. I have used a previous reply of yours to generate the payload…

This is your reply which I am referring to

https://community.home-assistant.io/t/issue-mqtt-publish-with-sensor-values-in-json-format/41775/8

Everything works fine here and the payload gets delivered to ESP32 client

payload: '{% set mydict = {"Google Chromecast": states.media_player.gf_lr_google_chromecast.state, "HomePod Mini": states.media_player.gf_lr_homepod_mini.state} %} {{ mydict | tojson }}

Here I am specifying the device name manually & the device state is read from the existing state. How can I manually avoid entering the name & read the name / friendly_name of each device too just like the state?