Template sensor: how to set a friendly name with a non-legacy syntax?

With a legacy syntax I can set a friendly name like this:

sensor:
  - platform: template
    sensors:
      xxx_yyy_zzz:
        friendly_name: "Some friendly name"
        value_template: >-
          {{ ... }}

How to set a friendly name with a new syntax?
I tried this:

template:
  - sensor:
      - unique_id: xxx_yyy_zzz
        name: "Some friendly name"
        state: >-
          {{ ... }}

The sensor is NOT CREATED without any errors/warnings in a Log.

Probably I could use this:

homeassistant:
  customize:
    sensor.xxx_yyy_zzz:
      friendly_name: "Some friendly name"

but this is an extra effort.

OK, what I can do is:

template:
  - sensor:
      - name: "Some friendly name"
        state: >-
          {{ ... }}

which will (probably) create a ā€œsensor.some_friendly_nameā€ sensor.
But I need to create the ā€œsensor.xxx_yyy_zzzā€.

If setting a friendly_name is NOT possible with a new syntax - then I will be disappointed.

If you use unique_id youā€™ll be able to assign a friendly name via UI.

  1. If I do not want to use UI - does it mean that I cannot customize a friendly_name?
  2. The sensor is NOT CREATED. Nothing to customize.

Iā€™m not sure if I can follow.
I donā€™t mean using it in a dashboard. You can customize a friendly name under /config/entities.

May be I did not understand you.
My steps:

  1. Declare a sensor:
template:
  - sensor:
      - unique_id: xxx_yyy_zzz
        name: "Some friendly name"
        state: >-
          {{ ... }}
  1. Restart HA or reload template entities.
  2. Go to entities list in Settings - there is no ā€œsensor.xxx_yyy_zzzā€ sensor (as well as ā€œsensor.some_friendly_nameā€). The sensor is not created.
  3. Goto Log - it contains no info about this sensor.

What I expected:
3. The sensor "sensor.xxx_yyy_zzz" is created, it has a friendly_name ā€œSome friendly nameā€.
or
3. The sensor "sensor.some_friendly_name" is created, now I can open itā€™s settings and manually input a friendly name ā€œSome friendly nameā€.
or
3. The sensor "sensor.some_friendly_name" is created, now I can customize it (using YAML) with friendly name ā€œSome friendly nameā€.

Thatā€™s the way it normally works ( sensor.some_friendly_name will be created and you can customize it via UI or customize.yaml / customize section). I canā€™t say why nothing is created in your case.
Can you please post your full sensor code?

Friendly name is just an attribute, so you add it as you would any other attribute:

template:
  - sensor:
      - unique_id: xxx_yyy_zzz
        name: "Some name I want the entity_id based on"
        state: >-
          {{ ... }}
        attributes:
          friendly_name: "This is my friendly name"

Sorry, do not know how it happened - but I was wrong.
The sensor IS created.
And it has a entity_id ā€œsensor.some_friendly_nameā€, a name ā€œSome friendly nameā€.

Now I can change itā€™s entity_id & name via UI.
But - I need this sensor to be "sensor.xxx_yyy_zzz" - and why I cannot get it via YAML?

OK, I may specify ā€œxxx_yyy_zzzā€ as a name in YAML:

template:
  - sensor:
      - unique_id: xxx_yyy_zzz
        name: xxx_yyy_zzz
        state: >-
          {{ ... }}

and the created sensor is ā€œsensor.xxx_yyy_zzzā€.
Now to set a friendly_name I need to either use UI or customize option.
Why it is not possible to specify a friendly_name while declaring a sensor? (like it is possible for legacy syntax)

I do not like a trend to replace YAML by UI. I am trying to keep all code in YAML.
Currently I will have to use the ā€œcustomizeā€ option to set a friendly_name. This is not a progress.

2 Likes

I am not getting a logic of the new syntax.
This code:

  - sensor:

      - unique_id: xxx_yyy_zzz
        name: "Some friendly name"
        state: "some state"

      - unique_id: xxx_yyy_zzz_2
        name: xxx_yyy_zzz_2
        state: "some state"

      - unique_id: xxx_yyy_zzz_3
        name: xxx_yyy_zzz_3
        state: "some state"
        attributes:
          friendly_name: "This is my friendly name"

creates these sensors (I even restarted HA):

  1. Why names & entity_ids are mixed?
  2. Where is the 3rd sensor?

Seems that with the new syntax some code is stored in json files and CANNOT be changed by YAML.
That is why I do not call it a ā€œprogressā€.

I just started using the new syntax (probably after more than a year when it was invented), currently seems that it is not as predictable as the legacy syntax.

See my post above - no sensor is created ))))).
Unpredictable behaviour.

Just donā€™t use the new style template. use legacy.

Easy-peasy.

Except if you want to use the new trigger based syntax which is super powerful.

Letā€™s just add the attribute in.

Easy-peasy to give similar advices.

I tried this from version to version. Seems that friendly_name isnā€™t accepted. The attribute is only built if I use another alias (e.g. friendly: or details: ).

1 Like

Did you see any records in the Log?)
I did not.
The sensor is just not created. Silently.

Moreover, the ā€œfriendly_nameā€ is not supposed to be added as a simple attribute. So, that advice of another person ā€œadd this attributeā€ was not based on knowledge (((

I can access the attribute, but itā€™s not displayed as one:


  - sensor:
      - name: "what we'll not see in frontend"
        unique_id: wwnsif
        state: "{{ this.attributes.friendly_name |default('Value when missing') }}"
        attributes:
          friendly_name: "attribute 'friendly_name'"

If you use unqiue_id, then your entity_id will be sensor.template_xxx_yyy_zzz. If you omit unique_id and jus t use name, your entity_id will be sensor.some_friendly_name. Your entity is created, but youā€™re searching for the wrong term.

FYI. Everything I just said is what happens when you FIRST create the sensor. If you make changes to the configuration but maintain the unique_id, everything I just said is thrown out because itā€™s now inside your entity_registry and you have to manage the names in the UI.

If you want everything set via the YAML with a name and friendly nameā€¦

  - sensor:
      - name: xxx_yyy_zzz
        state: "{{ blah }}"
        attributes:
          friendly_name: Some Friendly Name

Second to Lastly, as @finity saidā€¦ just donā€™t use the new style if you want a unique_id, entity_id, and name to be all different. The sole purpose of supplying a unique_id is to manage the entity from the UI. If you donā€™t have those intentions, donā€™t use it with the new style.

Lastly, the other option is to also customize your entity in the customize section.

And how to display this friendly name?

It should be like any other entity and be an attribute on the entity itself. Again, keep in mind that you cannot have a unqiue_id and you have to be editing a new entity. As soon as you create a sensor with unique_id, any yaml edits to name may not appear depending on what you did in the UI.

1 Like

friendly_name equals name. There is no visible attribute ā€˜some friendly nameā€™.