Variables in YAML config - How to set them and can they be changed by the webpage?

I have a delay variable I want to use in an ESPHome device. I’ve gone through a few YAML tutorials and see them using key/value pairs to set variables, but that doesn’t work in the config for ESPHome. It looks like I have to set up a substitution, like this:

substitutions:
  VentFanDelay: 120 #delay in seconds

And then, later, I use it by typing:

${VentFanDelay}

Is that right? It seems I can’t just set it early in the file with a line like:

VentFanDelay: 120 #delay in seconds

since that always gives me an error. And, since I can’t do that, it looks like I can’t just reference it by using the variable name, VentFanDelay.

Is using a substitution the correct or preferred way to do this, or is there a way to use variables like I would in a language like Python, where I just define it and use the variable name later?

And, once I set this variable (or substitution), is there a way to change the value through the web interface? And once I change it, how persistent is the new value? Does it stay until I reboot the chip? Is there a way to store it so it survives a reboot?


Addendum:

While it might be possible to set it through HA, I’d like to do it through the website for several reasons. While, ultimately, this will work with HA, it’s a critical system and needs to be operational every day, and I don’t want it to depend on HA, which can take several days to fix and get up and running if an update breaks something. This is part of a system that involves 3 ESPHome devices and I want them to work both with HA and directly with each other, so I have an option and don’t lose functionality when HA is down.

No, you can’t change a compile time constant at run time. What you can do is set up a number component with an initial value which can be controlled via HA or the web server.

Okay, that gives me a number I can edit from the web interface, which is a big part of what I need.

Is there a way to preserve the value between boots, so, once it’s set, the value will be permanent, even after rebooting?

(It also occurs to me that this would be useful to me if I could do this with a string, so I’m looking for that now (trying “template string”). If anyone can point me to something better, then, I’m open to it.


Addendum:

My bad - I missed the restore_value ability in the description, which keeps the value across a reboot by storing it in flash.

Also - found Template Text and got it set up, so those issues are solved.