Use this file to discover all available pages before exploring further.
Lambdas — also called templates — are inline C++ code blocks that you embed directly in your ESPHome YAML configuration. They allow you to go far beyond what the built-in actions, conditions, and filters support, expressing arbitrary logic wherever ESPHome accepts a templatable value. If you’ve never written C++ before, don’t worry: the patterns you’ll use in ESPHome lambdas are simple and highly repetitive once you’ve seen them a few times.
The !lambda YAML tag tells ESPHome that the following indented block is C++ code rather than a YAML value. The |- block scalar strips trailing newlines and preserves indentation:
lambda: !lambda |- // This is C++ code return some_value;
In most contexts where a lambda: key is used, the !lambda tag is implied and you can write lambda: |- directly. Both forms are equivalent.
# These two are identicallambda: !lambda |- return true;lambda: |- return true;
ESPHome does not validate lambda expressions during YAML parsing — it copies them verbatim into the generated C++ file. Syntax errors only surface during compilation. Check <NODE_NAME>/src/main.cpp when debugging lambda issues.
Template components (template sensor, template cover, template binary sensor, etc.) compute their state entirely from a lambda. The return type must match what the component expects.
Any parameter marked “templatable” in the docs accepts a lambda that returns the appropriate type. This lets you drive action arguments from sensor readings:
Inside a lambda you have full access to the ESP-IDF C API (and Arduino API if using the Arduino framework). This enables direct hardware access when no ESPHome component covers your use case:
# Read a GPIO pin value directly via ESP-IDFlambda: |- int level = gpio_get_level(GPIO_NUM_4); ESP_LOGI("direct_gpio", "GPIO4 level: %d", level); return (float)level;
# Read free heap memory and publish it as a sensorsensor: - platform: template name: "Free Heap" unit_of_measurement: "bytes" lambda: |- return (float)esp_get_free_heap_size(); update_interval: 30s