ESPHome Sensor naming driving me nuts

I’m experimenting with a simple ESPHome project:

substitutions:
  devicename: energy-meter-01
  friendly_name: Energy Meter 01

esphome:
  name: ${devicename}

switch:
  - id: relay
    platform: gpio

So far so good.
Now, if I name my switch

name: Relay

The resulting Home Assistant sensor is (friendly)named

"Relay"

and the entity_id is just

switch.relay

Good, but not very useful if you have multiple devices.
If I name the switch

name: ${friendly_name} Relay

the resulting sensor is now named

"Energy Meter 01 Relay"

(much better), however the entity_id now becomes

energy_meter_01_energy_meter_01_relay

I’m clearly missing something.
Please help!

The behaviour of friendly_name became a feature a few releases back, so possibly you should be working with that rather than substitutions (which was the old approach). Or try using another term than friendly_name if you want to use the substitution approach - you may be getting some interactions with a reserved field?

ESPHome Core Configuration — ESPHome.

Still, even if I do define friendly_name (as a part of the esphome domain) and do not prefix the sensor (i.e. relay) name using substitution, I don’t get automatic prefixes to the entities as per the documentation. If I do prefix them I get double prefixes…