Templates - start with sensor: or template: to get either friendly_name or icon ?!?

Hi,

I am still learning and have spent some time reading the original Home Assitant documentation, which proved the best way to learn so far for me.

Still I have a rather simple (?) question that I cannot answer:
I realized I can achieve a templated sensor in two ways (both work, and are equally listed in the states under development tools):

Way one, starting with “sensor:” and then “-platform: template”.

Here’s the configuration:

sensor:
  - platform: template
    sensors:
      air_quality:
        friendly_name: "Air quality level"
        value_template: >-
          {% if states('sensor.air_quality_co2')|int < 850 -%}
          low
          {%- elif states('sensor.air_quality_co2')|int < 1550 -%}
          medium
          {%- elif states('sensor.air_quality_co2')|int > 1 -%}
          high
          {%- else -%}
          unknown
          {%- endif %}

Or way two, starting with "template: " and then "sensor: ":

template:
  - sensor:
      - name: air_quality
        icon: "mdi:air-filter"
        state: >-
          {% if states('sensor.air_quality_co2')|int < 850 -%}
          low
          {%- elif states('sensor.air_quality_co2')|int < 1550 -%}
          medium
          {%- elif states('sensor.air_quality_co2')|int > 1 -%}
          high
          {%- else -%}
          unknown
          {%- endif %}

The obvious differences are

  • using the one way I can assign a a friendly-name, but no icon
  • using the way two, I can assign an icon, but no friendly-name (it is derived automatically from the name)

Apart from that, the end result seems identical.

  • What does each way exactly do within Home Assistant?
  • Which way do you see as “better” and why, are there any further disadvantages or advantages that I should be aware of?

The second one is the “new” way. It was introduced probably a year ago or something.

Are you sure you can’t set friendly name/icon on both?

1 Like

Nope, no friendly_name in the new method. However the name: option can be used as long as you don’t mind the entity id being auto generated.

e.g.

name: "Air quality level" would create the entity sensor.air_quality_level.

When changing from the old to the new method I had to do a bit of using customize to prevent having to change the entity id in a lot of places:

template:
  - sensor:
      - name: "offpeak_cost_today" # Customized as, friendly_name: Offpeak
        icon: "mdi:currency-usd"
        unit_of_measurement: "$"
        state: "{{ (states('sensor.energy_from_grid_daily_offpeak')|float(0) * states('input_number.t93_offpeak_energy_cost')|float(0) )|round(2) }}"

There is an alternative using unique id, see:

1 Like

Yes, tried them all out, as this seemed not so well documented.
Do you know why the “new” way was chosen ? I guess the advice is, better use way two then ?

I have more and more question marks on this… if the second way is the “new” way, when should I use it instead of the first way for templates, and why?

There are still entities described the “old” way in the documentation
Template Fan - Home Assistant (home-assistant.io)
and I don’t see a straightforward way when this is applied, and when not. E.g., for min_max it seems not allowed as template.

Any insights from someone closer to the Home Assistant logic appreciated, I want to understand the concept and not just copy/paste examples that I modify.

https://www.home-assistant.io/integrations/template/#legacy-binary-sensor-configuration-format

https://www.home-assistant.io/integrations/template/#legacy-sensor-configuration-format

That has nothing to do with template sensors. It’s a completely different integration. This only applies to template binary sensors and template sensors.

1 Like