Are there preprocessing directives in ESPHome to include/exclude blocks of Yaml code at compile time

I want to compile different Yaml code depending on the value of the substitution display_type

Grok tried to tell me I could do things like:

substitions:
  display_type: "st7789"
...
...
!if "${display_type}" = "st7789"
<Include code block>
!!if "${display_type}" != "1602"
<exclude code block>
!endif
...
...

But that gives compile errors so it seems like Grok is hallucinating.

Is there any way to do this in the file?
(Note I know you can conditionally include other files by using a substitution for the file name but I want all the code in one file)

ESPHome does not have conditional preprocessing commands.

This might work:

substitions:
  display_type: "st7789"
!include ${display_type}.yaml

In your config/esphome folder you would have st7789.yaml and 1882.yaml.

Indentation would be challenging. Maybe use packages.

yeah – that is what I had meant by:

(Note I know you can conditionally include other files by using a substitution for the file name but I want all the code in one file)

But I really would prefer to have all the code in a single file.
Having the ability to include/exclude Yaml code blocks at compile time (similar to the standard C-preprocessor) would be very helpful.

Why (do you prefer that)?
What problem are you trying to solve with having all code in one file?

This is a good introduction to a good book on the subject of software complexity.

1 Like

For me, I prefer to have a single file to maintain and distribute when the code itself is not very long.

I agree for large projects, it makes sense to split code into modules…

Stay away from the AI answers for esphome. They answer with certainty even when they are certainly wrong. If you trust them blindly you will be led away from your goal.

1 Like