Sensor friendly name including variable

Hi.
I am trying to give a sensor a friendly name where the string includes a variable from the state of another sensor as below:

friendly_name: "Smart Plug Week "+hass.states.get(sensor.iso_week_number).state+" Usage:"

with the intention of having the current iso week number given as part of the name e.g.:

Smart Plug Week 25 Usage:

This gave the following error:
“…expected block end>, but found ‘scalar>’…”

I then tried the following with similar result:

friendly_name: "Smart Plug Week "+sensor.iso_week_number.state+" Usage:"

Is this a simple syntax error or is it not possible?

Thanks in advance.

As long as i know it’s only possible in Template sensors.

Thanks for the quick response.
Ok, are there examples of this in the documentation? (haven’t found any - and better add I’m still quite new to configuration in yaml :slightly_smiling_face:)

  • There’s an example in the link that i posted above.

  • Here’s all about Templating.

  • You can test and play with your templates in Dev Tools/templates.

Great, got it working. Many thanks.
For others interested, here is my final config:

# Sensor to read weekly power consumption of smart plug
  - platform: template
    sensors:
      nodon_smart_plug_weekly_total:
        friendly_name_template: >-
          {% if states('sensor.iso_week_number')|float > 0 %}
            Smart Plug Week {{ states('sensor.iso_week_number') }} Usage:
          {% else %}
            Smart Plug Week Usage:
          {% endif %}
        value_template: "{{ (states.input_number.current_week_total) | float | round(2) }}"
        unit_of_measurement: kWh
1 Like