Esphome conditional compile?

Hi
Does the esphome yaml have conditional compile functionality?
I have several LD2450 sensors. They can count the number of people in up to three zones.
Everything regarding these is defined in a file include/ld2450.yaml.
For some of them I need all three zones, but for some one is enough. Today I have separate include files for this.
To avoid unnecessary/confusing sensors and stuff, I would like to add something like this to my include file (where zones could be defined in substitutions):

$if (${zones} > 1)
    zone_2:
      x1:
        name: ${prefix} Zone2 X1
      y1:
        name: ${prefix} Zone2 Y1
      x2:
        name: ${prefix} Zone2 X2
      y2:
        name: ${prefix} Zone2 Y2
$end if
$if (${zones} > 2)
    zone_3:
      x1:
        name: ${prefix} Zone3 X1
      y1:
        name: ${prefix} Zone3 Y1
      x2:
        name: ${prefix} Zone3 X2
      y2:
        name: ${prefix} Zone3 Y2
$end if

Have you had a look at ‘Packages as Templates’ not sure if this will achieve what you want or some way toward?

I was about to suggest this too. Put the additional zones in a separate package. You can even pass parameters to packages with some effort. This comes straight from some of my yaml files:

packages:
  diagnostics: !include packages/diags.yaml
  presence: !include { 
    file: 'packages/presence_2410.yaml', 
    vars: { 
      pres_uart_tx: 'GPIO17', 
      pres_uart_rx: 'GPIO16', 
      pres_pin: 'GPIO25' 
      }  
    }
  ble_tracker: !include packages/ble_tracker.yaml

Some of these packages add parameters to preexisting sections like ble:
The package files contain yaml with sensors etc. just like you would put it in the esp yaml.

I was working on a similar problem.

Packages is the only way you can somehow compose your files.

There is no real condition, if/then logic in yaml.
It is purely declarative.

The only thing you could do is to add a step with Jinja templates and add conditionals in there.
But then you have to generate the yaml files from templates and then compile the yaml for your device.
Quite cumbersome.

Thanks! I’ll give it a try!
I’ve seen that some yaml files can contain conditional compilation, but they are for very proprietary solutions.
I guess it should be possible in esphome too, but it’s not extremely important.

Yeah another thing I noticed is you can’t conditionally load a module. For example this does not work.

substitutions:
  libary_folder: "/my_library"

packages:
  # If your touch screen has a backlight include this
  backlight: !include ${libary_folder}/modules/base/backlignt.yaml

That has nothing to do with YAML.

This would be a templating language on top of YAML. Something like jinja or others.
YAML can only be descriptive and therefore there is no logic.

I agree, but in esphome as well as in other environments, yaml is being used as a preprocessor for c++ or other languages.
Or, even as something resembling a scripting language in it’s own right.

This is an example from some microsoft product, where the “${{ if” decides whether to include the following section or not.
Something like that would be helpful in esphome as well.

  - ${{ if eq(parameters.paramA, 'true') }}:
    - powershell: |
        "Conditional insertion from parameter whose value came from a variable"
      displayName: conditionally inserted task from variable
1 Like