ESPHome compile issue - possibly due to Lambda

I’m trying to create a Sound Sensor from a D1 Mini and I’ve run into a bit of an issue compiling the project. I’m a complete muggle and know nothing about ESPHome or compiling. I’m just a simple nerd trying to follow this video tutorial https://www.youtube.com/watch?v=Akt4rSyNS_E

Any help would be greatly appreciated.

Here’s the code:

esphome:
  compile_process_limit: 1
  name: soundsensor
  friendly_name: SoundSensor

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Soundsensor Fallback Hotspot"
    password: "subscribe"

captive_portal:

# Example configuration entry
web_server:
  port: 80

globals:

  - id: esphome_sensitivity
    type: float
    initial_value: '36.5'
    restore_value: yes

  - id: esphome_volume
    type: int

sensor:
  - platform: adc
    pin: A0
    id: esphome_db
    device_class: signal_strength
    name: "Db SoundEsp"
    icon: "mdi:volume-vibrate"
    unit_of_measurement: "db"
    update_interval: 2s
    raw: true
    filters:
      - lambda: |-
          unsigned int sample;
          unsigned long startMillis= millis(); 
          float peakToPeak = 0; 
          unsigned int signalMax = 0;
          unsigned int signalMin = 1024;
          while (millis() - startMillis < 500) {
            sample = analogRead(A0);
            if (sample < 1024){
                if (sample > signalMax){
                    signalMax = sample;
                }
                else if (sample < signalMin){
                    signalMin = sample;
                }
              }
          }
          peakToPeak = map((signalMax - signalMin),1,1024,1.5,1024);
          id(esphome_volume) = peakToPeak;
          float state = id(esphome_sensitivity)*log10(peakToPeak)+15;  
          return(state);
  - platform: template
    name: "Volume SoundEsp"
    icon: "mdi:volume-high"
    unit_of_measurement: "%"
    update_interval: 2s
    lambda: return(map((id(esphome_db).state),15,150,0,100));

  - platform: template
    name: "RAW SoundEsp"
    icon: "mdi:volume-source"
    unit_of_measurement: "%"
    update_interval: 2s
    lambda: return(map(id(esphome_volume),1,1024,0,100));

number:
  - platform: template
    id: sensitivity_slider
    name: "Sensitivity SoundEsp"
    icon: "mdi:knob"
    update_interval: 5s
    initial_value: "36.5"
    step: 0.1
    min_value: 20
    max_value: 40
    mode: slider
    set_action:
      then:
        - lambda:  id(esphome_sensitivity) = x;

And here’s the error:

INFO ESPHome 2023.8.2
INFO Reading configuration /config/esphome/esphome-web-33fbf3.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing soundsensor (board: d1_mini; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
Dependency Graph
|-- ESPAsyncTCP-esphome @ 1.2.3
|-- ESPAsyncWebServer-esphome @ 2.1.0
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- ArduinoJson @ 6.18.5
Compiling /data/soundsensor/.pioenvs/soundsensor/src/esphome/components/api/api_pb2.cpp.o
xtensa-lx106-elf-g++: fatal error: Killed signal terminated program cc1plus
compilation terminated.
*** [/data/soundsensor/.pioenvs/soundsensor/src/esphome/components/api/api_pb2.cpp.o] Error 1
========================= [FAILED] Took 171.05 seconds =========================

Thanks in advance for having a look.

Compiles OK here, but I am on esphome 2023.6.5.

A bit of googling tells me to ask what OS you are running on, and is it 32 bit or 64 bit?

Compiles fine here.
Home Assistant 2023.8.4

System Information

version core-2023.8.4
installation_type Home Assistant OS
dev false
hassio true
docker true
user root
virtualenv false
python_version 3.11.4
os_name Linux
os_version 6.1.21-v8
arch aarch64
timezone Europe/London
config_dir /config
Home Assistant Community Store
GitHub API ok
GitHub Content ok
GitHub Web ok
GitHub API Calls Remaining 5000
Installed Version 1.32.1
Stage running
Available Repositories 1272
Downloaded Repositories 4
Home Assistant Cloud
logged_in false
can_reach_cert_server ok
can_reach_cloud_auth ok
can_reach_cloud ok
Home Assistant Supervisor
host_os Home Assistant OS 10.5
update_channel stable
supervisor_version supervisor-2023.08.1
agent_version 1.5.1
docker_version 23.0.6
disk_total 116.5 GB
disk_used 5.6 GB
healthy true
supported true
board rpi3-64
supervisor_api ok
version_api ok
installed_addons Home Assistant Google Drive Backup (0.111.1), Ring-MQTT with Video Streaming (5.6.0), File editor (5.6.0), Terminal & SSH (9.7.1), Mosquitto broker (6.2.1), ESPHome (2023.8.2)
Dashboards
dashboards 1
resources 0
views 5
mode storage
Recorder
oldest_recorder_run 21 August 2023 at 10:52
current_recorder_run 30 August 2023 at 17:19
estimated_db_size 58.13 MiB
database_engine sqlite
database_version 3.41.2

Managed to get a bit further along by installing locally via WiFi as apposed to remotely via a VPN. However, it still fails.

INFO ESPHome 2023.8.2
INFO Reading configuration /config/esphome/esphome-web-33fbf3.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing soundsensor (board: d1_mini; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
Dependency Graph
|-- ESPAsyncTCP-esphome @ 1.2.3
|-- ESPAsyncWebServer-esphome @ 2.1.0
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- ArduinoJson @ 6.18.5
Compiling /data/soundsensor/.pioenvs/soundsensor/src/esphome/components/api/api_pb2.cpp.o
Compiling /data/soundsensor/.pioenvs/soundsensor/src/esphome/components/api/api_pb2_service.cpp.o
Compiling /data/soundsensor/.pioenvs/soundsensor/src/esphome/components/api/api_server.cpp.o
Compiling /data/soundsensor/.pioenvs/soundsensor/src/esphome/components/api/list_entities.cpp.o
Compiling /data/soundsensor/.pioenvs/soundsensor/src/esphome/components/api/proto.cpp.o
Compiling /data/soundsensor/.pioenvs/soundsensor/src/esphome/components/api/subscribe_state.cpp.o
Compiling /data/soundsensor/.pioenvs/soundsensor/src/esphome/components/api/user_services.cpp.o
Compiling /data/soundsensor/.pioenvs/soundsensor/src/esphome/components/captive_portal/captive_portal.cpp.o
xtensa-lx106-elf-g++: fatal error: Killed signal terminated program cc1plus
compilation terminated.
*** [/data/soundsensor/.pioenvs/soundsensor/src/esphome/components/captive_portal/captive_portal.cpp.o] Error 1
========================= [FAILED] Took 276.69 seconds =========================

To isolate the issue you could try commenting out everything except for a minimal config and then if it compiles add back code blocks until the failure appears.

Your compile times seem slightly on the long side, how much CPU/RAM on your compiling machine?

These are quite generic things to look at, I don’t have any clue about the specifics of your issue.

Edit: I see now you have a rpi3. People report resource issues with them and ESPHome.

Edit 2: Try dropping web_server.

" Please note that enabling this component will take up a lot of memory and may decrease stability, especially on ESP8266."

I think persistence was the key. I changed 1 value at a time until everything was accepted. Then I added the sensors to it. Took about 20 attempts but got there in the end. Thanks for all the suggestion and advice.