Using substitutions in name:

I have several similar devices to deploy and substitutions are clearly going to make things simpler as there are 50 'names in each. However, I can’t determine from the docs just how substitutions can be used in name:.

If I have the following, for example:

  - platform: total_daily_energy
    name: "Garage Total Daily Energy"
    power_id: garage_total_power
    accuracy_decimals: 0

and use substitutions:

substitutions:
  area_id: garage
  area_name: Garage

would I then use it as:

  - platform: total_daily_energy
    name: "${area_name} Total Daily Energy"
    power_id: ${area_id}_total_power
    accuracy_decimals: 0

or is some form of concatenation required? (I have no expectation that the example below is correct!)

  - platform: total_daily_energy
    name: "${area_name}" + "Total Daily Energy"
    power_id: ${area_id} + _total_power
    accuracy_decimals: 0

or similar.

Hope that makes sense.

@ashscott

Here is an example from my YAML:

substitutions:
  devicename: esp17

button:
  - platform: restart
    name: '${devicename} Restart'
    internal: false

display:
  platform: lilygo_t5_47
  id: '${devicename}_display'
  update_interval: never

The substitution is exact - it just gets swapped out when the YAML gets compiled.

Thank you.

So, in this case, button shows up in HA as ‘esp17 Restart’?

Have I got that correct?

Are the single quotes key or does it also work with doubles?

It does show up with an Entity ID of button.esp17_restart with a name of “esp17 Restart”

I run everything in esphome and Home Assistant through a YAML validator - it tends to like single quotes. But I think double quotes are okay (it validates).

Or you can switch to:

- platform: total_daily_energy
  name: '${area_name} Total Daily Energy'
  power_id: '${area_id}_total_power'
  accuracy_decimals: 0

This was very useful, but I have an additional question regarding substitutions:
I would like to enter a text string (any text) in substitutions (i.e:

substitutions: 
   controller_type:  "ESP32S2MINI"

and in the text_sensor section have a template that simply returns a text string " Type: ${controller_type}"

text_sensor:
  - platform: template
    name: "Type"
    lambda: |- 
      return  $(controller_type);

(I would like to use this when I click VISIT on my ESPHome Device, so this information gets presented on the webpage, together with other info such as uptime etc. etc.)

I have searched for a solution in the ESPHome documentation/community & Googled the net, tried Copilot etc. but with no luck…

The error I get is :

/config/esphome/ny-s2-x-26.yaml: In lambda function:
/config/esphome/ny-s2-x-26.yaml:35:16: error: 'controller_type' was not declared in this scope

As it says that “the controller_type was not declared in this scope”, its probably very easy to fix , but not for a newbie …(but i am learning every day :slight_smile: .

I’m by no means an expert on the subject matter, but I don’t think you can use the substitutions inside a lambda function. At that point you are writing C code and no longer doing yaml.

What you could do (but it is probably overkill) is define a (internal) text sensor with the value and use that in the lambda. I do not know if there are easier ways to communicate information to the C code.

Use ${controller_type}.

Can. Substitutions replaced first,. then lambda being compiled.

I’ll add that it’s way easier, nowadays, to just set the friendly_name instead of prefixing ${devicename} in all component names.

Sorry I made a typo, i WAS using curly brackets but no luck :slight_smile:

substitutions:
  controller_type: "ESP32S2MINI"

esphome:
  name: ny-s2-x-26
  friendly_name: ny-s2-x-26

esp32:
  board: esp32-s2-saola-1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "EdK4EosyMOTAiB04gYYm2l0JOUZSy2tK4MvM8ORpDog="

ota:
  password: "ef2f81a98e779690b19dec6526f9d47c"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Ny-S2-X-26 Fallback Hotspot"
    password: "pd3FoXMyELNv"

captive_portal:

text_sensor:
  - platform: template
    name: "Kontroller: "
    lambda: |- 
      return ${controller_type};

/config/esphome/ny-s2-x-26.yaml: In lambda function:
/config/esphome/ny-s2-x-26.yaml:39:14: error: 'ESP32S2MINI' was not declared in this scope
       return ${controller_type};

That is because the quotes do not make it into the lambda. If you mean to return a string, place quotes around it in the lambda.

Try this;

return {"$controller_type"};

Perfect !! Works like a charm !!!
Thank you @zenzay42, @Edwin_D and @Masterzz for your support !!!

1 Like

This one is working too:

      return to_string("${controller_type}");

Yes both return lines seems to works fine…
Any differences (advantages/disadvantages) when to use return or return_to_string ??

I thought it might, but wasn’t 100%, whereas I knew my second suggestion would work.

In your case there’s no difference, however to_string() is mostly used to convert a number to a string.