Homeassistant Sensor in ESPHome causing undefined reference error

My first forum post. Thanks for all the help you guys provided so far!.
I hope you’ll excuse the rather trivial ‘Looroll Monitor’ use case. I’m using it to get to know ESP Home.

I’m running on a Home Assistat Yellow installed following the setup instructions. My ESPHome board is an S2 mini. I have a Ultrasonic Sensor which measures distance. From this I calculate Thickness and Length using copy sensors, I display the results on a screen. This much works fine.

I am now trying to capture change in length over past day (say, I use 5 min to get started) using a statistical sensor in Home Assistant which want to bring in to ESP home using a ‘homeassistant’ sensor so I can display it.

The statistical sensor is visible in HA ‘Helpers’ and is showing data.
I cut and pasted the id into the ESP Home yaml.
The error message crops up when I try to install the code with the ‘homeassistant’ sensor added. Without it, it all installs fine.

This is my YAML

esphome:
  name: loo-roll-monitor
  friendly_name: Loo Roll Monitor

esp32:
  board: lolin_s2_mini
  variant: esp32s2
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "SOW4UTKa8b6uJfU8P6A4QdP/qaTf4InUqHUSBCe7+cQ="

ota:
  - platform: esphome
    password: "0e19c7e525483d3d2e2185995139cb0d"

wifi:
  ssid: ******
  password: ******

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Loo-Roll-Monitor"
    password: "Ki07zxEORnUq"

captive_portal:

time:
  - platform: homeassistant
    id: ha_time

sensor:
- platform: ultrasonic
  trigger_pin: 18
  echo_pin: 16
  id: Distance
  update_interval: 10s

- platform: copy
  source_id: Distance
  id: Thickness
  filters:
  - lambda: |-
      return(9.6 - (x*100.0));

- platform: copy
# 25m * % of full roll where full is 4cm thick and 25m long
  source_id: Thickness
  name: "Loo Roll Length"
  id: Length
  filters: 
  - lambda: |-
       return 25*x*(x+4.0)/32.0;

- platform: homeassistant
  entity_id: sensor.looroll_useage
  id: Useage

font:
    # gfonts://family[@weight]
  - file: "gfonts://Roboto"
    id: font1
    size: 20

spi:
  clk_pin: 5
  mosi_pin: 9

display:
  - platform: waveshare_epaper
    cs_pin: 7
    dc_pin: 11
    busy_pin: 3
    model: 1.54inv2
    full_update_every: 1
    update_interval: 30s
    lambda: |-
      it.strftime(0, 0, id(font1), "%b-%d %H:%M", id(ha_time).now());
      it.printf(130, 0, id(font1), "%.2f cm", id(Thickness).state);
      it.circle(140, 140, 55);    
      it.filled_ring(140, 140, 20 + 10*id(Thickness).state, 20);
      it.printf(0, 20, id(font1), "%.1f metres", id(Length).state);
      it.printf(0, 40, id(font1), "Distance %.1f cm ", id(Distance));
#      it.printf(9, 60, id(font1), "Useage %1f m", id(Useage));

and the log with the error:

INFO ESPHome 2024.12.2
INFO Reading configuration /config/esphome/loo-roll-monitor.yaml...
INFO Detected timezone 'Europe/Berlin'
INFO Generating C++ source...
INFO Updating https://github.com/espressif/[email protected]
INFO Compiling app...
Processing loo-roll-monitor (board: lolin_s2_mini; framework: espidf; platform: https://github.com/pioarduino/platform-espressif32.git#51.03.07)
--------------------------------------------------------------------------------
HARDWARE: ESP32S2 240MHz, 320KB RAM, 4MB 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.10.2 
 - 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-esp32s2 @ 12.2.0+20230208
Reading CMake configuration...
Dependency Graph
|-- noise-c @ 0.1.6
Linking .pioenvs/loo-roll-monitor/firmware.elf
/data/cache/platformio/packages/toolchain-xtensa-esp32s2/bin/../lib/gcc/xtensa-esp32s2-elf/12.2.0/../../../../xtensa-esp32s2-elf/bin/ld: .pioenvs/loo-roll-monitor/src/main.cpp.o:(.literal._Z5setupv+0x118): undefined reference to `_ZTVN7esphome13homeassistant19HomeassistantSensorE'
/data/cache/platformio/packages/toolchain-xtensa-esp32s2/bin/../lib/gcc/xtensa-esp32s2-elf/12.2.0/../../../../xtensa-esp32s2-elf/bin/ld: .pioenvs/loo-roll-monitor/src/main.cpp.o:(.literal._Z5setupv+0x11c): undefined reference to `_ZTVN7esphome13homeassistant19HomeassistantSensorE'
collect2: error: ld returned 1 exit status
*** [.pioenvs/loo-roll-monitor/firmware.elf] Error 1
========================= [FAILED] Took 40.39 seconds =========================

Thanks for any help you can provide.

Is there a sensor in home assistant with that entity_id?

Yes. This one:

The error is from the linker, not the compiler, mostly a “Clean Build File” action solves this kind of problems. If you use the ESPHome add-on this action is in the three dots menu by your device.

That fixed the problem!
Thanks for swift answer!