Help setting friendly_name via MQTT Discovery

I am sending autodiscovery MQTT messages from a utility I wrote. I want the Name/Friendly name (The text that displays on the UI) of the item to differ from the item name which is a long string.

I read in a post that you have to not set the name and just set the unique_id. you then set an attribute called friendly_name to what you want it to show as in the UI.

Referring to this post, Add (bring back) friendly_name to template sensors - #4 by petro
When I set the friendly_name attribute, it doesn’t set it. The UI shows ‘MQTT Sensor’ and the unique_id is then set to mqtt_sensor_8.

I also added a test_attribute to the autodiscovery attribute: section to see if that was carried through and it is not. (This isn’t in the MQTT sensor docs so I didn’t expect it to work)

Here is the autodiscovery JSON being sent over MQTT:

{
  "device": {
    "identifiers": [
      "network_test_host"
    ],
    "name": "Network Info for Host: test_host"
  },
  "state_topic": "device/test_host/system/network",
  "value_template": "{{ value_json.interface_name }}",
  "unique_id": "networkinfo_test_host_interface_name",
  "icon": "mdi:expansion-card",
  "expire_after": "3600",
  "attributes": {
    "friendly_name": "Interface Name",
    "test_attribute":"Test Attribute Value"
  }
}

I previously tried setting the unique_id and then also the name. The unique_id is ignored when the item is autodiscovered and I end up with a name of sensor.mqtt_sensor_2 which is not what I want.

    icon: mdi:ip-network
    friendly_name: MQTT Sensor

I also added a test_attribute on the autodiscovery to check that attributes weren’t importing and they’re not.

Referring to the docs, setting attributes: is not supported under MQTT sensors other than importing from a MQTT topic.

I can’t use customize: as there are simply too many items to change.

My question is, without using customize: how can I set the friendly_name on autodiscovery?

References:

For the MQTT Options I need, I’ve referred to this:

For the autodiscovery, i’ve been referring to this:

The name option is used by Home Assistant’s MQTT Discovery to create the entity’s friendly_name and object_id (the second half of its entity_id).

However, I believe if you also use the object_id option, you can make the entity’s object_id different from its friendly_name.

"name": "Network Info for test host",
"object_id": "network_info_test",
"unique _id": "networkinfo_test_host_interface_name",
1 Like

Thank you. That has fixed it. :grinning:

1 Like