Utilizing value from number template in lambda for sensor template

Hi,

I have issues with the following. I have two input numbers defined as follows:

number:
  - platform: template
    name: Failsafe voltage
    id: failsafe_voltage
    icon: mdi:lightning-bolt-outline
    entity_category: config
    unit_of_measurement: V
    mode: box
    device_class: voltage
    min_value: 0
    max_value: 250
    step: 1
    optimistic: true
    restore_value: true

  - platform: template
    name: Failsafe current (per phase)
    id: failsafe_current
    icon: mdi:lightning-bolt-outline
    entity_category: config
    unit_of_measurement: A
    mode: box
    device_class: current
    min_value: 0
    max_value: 32
    step: 1
    optimistic: true
    restore_value: true

Those values are used to calculate a failsafe power (sensor template) which I want to display as feedback to HA. I have defined it as follows:

sensor:
  - platform: template
    id: failsafe_power_pp
    name: Failsafe power per phase
    icon: mdi:lightning-bolt-outline
    entity_category: diagnostic
    unit_of_measurement: kW
    device_class: power
    lambda: |-
      return id(failsafe_voltage).state * id(failsafe_current).state / 1000;

However, when compiling, I get the following error:

marcus@borium:~/dev/esphome/b23sim$ esphome compile b23sim.yaml 
INFO ESPHome 2025.2.2
INFO Reading configuration b23sim.yaml...
INFO Generating C++ source...
INFO Updating https://github.com/espressif/[email protected]
INFO Compiling app...
Processing b23sim (board: esp32-s3-devkitc-1; framework: espidf; platform: https://github.com/pioarduino/platform-espressif32.git#51.03.07)
-----------------------------------------------------------------------------------------------------------------------------------------------------
HARDWARE: ESP32S3 240MHz, 320KB RAM, 8MB Flash
 - framework-espidf @ 3.50105.0 (5.1.5) 
 - tool-cmake @ 3.21.3 
 - tool-esptoolpy @ 4.8.1 
 - tool-mklittlefs @ 3.2.0 
 - tool-ninja @ 1.7.1 
 - tool-riscv32-esp-elf-gdb @ 12.1.0+20221002 
 - tool-xtensa-esp-elf-gdb @ 12.1.0+20221002 
 - toolchain-esp32ulp @ 2.35.0-20220830 
 - toolchain-riscv32-esp @ 12.2.0+20230208 
 - toolchain-xtensa-esp32s3 @ 12.2.0+20230208
Reading CMake configuration...
Dependency Graph
|-- noise-c @ 0.1.6
Compiling .pioenvs/b23sim/src/main.cpp.o
Linking .pioenvs/b23sim/firmware.elf
/home/marcus/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/12.2.0/../../../../xtensa-esp32s3-elf/bin/ld: .pioenvs/b23sim/src/main.cpp.o:(.literal._Z5setupv+0x1c4): undefined reference to `_ZTVN7esphome9template_14TemplateSensorE'
/home/marcus/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/12.2.0/../../../../xtensa-esp32s3-elf/bin/ld: .pioenvs/b23sim/src/main.cpp.o:(.literal._Z5setupv+0x1c8): undefined reference to `_ZTVN7esphome9template_14TemplateSensorE'
/home/marcus/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/12.2.0/../../../../xtensa-esp32s3-elf/bin/ld: .pioenvs/b23sim/src/main.cpp.o:(.literal._Z5setupv+0x398): undefined reference to `_ZN7esphome9template_14TemplateSensor12set_templateEOSt8functionIFNS_8optionalIfEEvEE'
/home/marcus/.platformio/packages/toolchain-xtensa-esp32s3/bin/../lib/gcc/xtensa-esp32s3-elf/12.2.0/../../../../xtensa-esp32s3-elf/bin/ld: .pioenvs/b23sim/src/main.cpp.o: in function `_ZN7esphome11Application15register_numberEPNS_6number6NumberE':
/home/marcus/dev/esphome/b23sim/.esphome/build/b23sim/src/esphome/core/application.h:139: undefined reference to `_ZN7esphome9template_14TemplateSensor12set_templateEOSt8functionIFNS_8optionalIfEEvEE'
collect2: error: ld returned 1 exit status
*** [.pioenvs/b23sim/firmware.elf] Error 1

I have the same calculation in lambdas in other places (modbus sensors’ read_lambdas) where it seem to work.

What can I do differently?

I don’t see that your code should cause that kind of error.
Try to clean the build before compiling.

That solved it, thank you for your help! I have never experienced a similar issue and thus never cleaned the build before.

I think it’s a bug more than a feature…