Programmatically generate value for variable

The approach I’ve taken writing ESPHome config is to use a devicename variable, to then append/prepend to sensor IDs and such.

I’ve now got multiple devices that will use the same config, but for variation on the device name (using name_add_mac_suffix: true). This is all find & good, but for the different contexts where I then want to use devicename: some require an underscore (_) for word separation, and others give warning or refuse to use an underscore.

Is there a way to programmatically replace underscore for dash in a variable name to generate separate variable names that then can be used for either esphome.name or wifi.use_address (for example).

For example, if this were bash, I might set devicename to hvac_ir_remote, then where underscores aren’t allowed, I would set an entity id to: ${devicename/_/-}, resulting in that id being set to hvac-ir-remote.

Can you give a more concrete example of where you are striking this, use case, etc.