Multiple sensors returning nan on esp32

I’ve recently started having an issue with an ESP32-based weather station I’ve had running for over 2 years now. The original ESP32 board failed and I stopped receiving values from my station, so I made the decision to completely rebuild the board because I already had most of the parts lying around in surplus. It is based on this project but adapted for esphome.

After rebuilding a new circuit board from scratch and flashing my code to a new esp32, I’m having an issue with almost every sensor returning nan.

Here is my esphome yaml:

esphome:
  name: weatherstation-garden
  platform: ESP32
  board: esp32dev
  #includes:
  #  - SI1145.h
  #libraries:
  #  - "Adafruit SI1145 Library"
  on_boot:
    then:
      - script.execute: consider_deep_sleep
  
substitutions:
  loc: "Garden"
  name: "Garden Weather Station"

wifi:
  ssid: "xxxx"
  password: !secret wifi_pw
  fast_connect: false
  manual_ip:
    static_ip: xxx.xxx.xxx.xxx
    gateway: xxx.xxx.xxx.xxx
    subnet: xxx.xxx.xxx.xxx
  ap:
    ssid: ${name} Fallback
    password: !secret fallback_pw

captive_portal:

logger:
  #level: INFO

api:
  encryption:
    key: "xxxx"

ota:
  password: !secret ota_pw
  
    
i2c:
  sda:                        GPIO21
  scl:                        GPIO22
  scan:                       True
  frequency:                  100kHz

sensor:
  # =========================================
  # Temperature, humidity, pressure sensor
  # =========================================
  - platform:                 bme280_i2c
    address:                  0x76
    update_interval:          5s
    temperature:
      name:                   ${loc} Temperature
      id:                     bme280_temperature
      oversampling:           16x
      filters:
        - sliding_window_moving_average:
            window_size: 3
            send_every: 3
            send_first_at: 3
        - lambda: return (x * (9.0/5.0) + 32.0) + id(temperature_calibration).state;
          # Calibration:
          # 3/10/22 - +1.7
      unit_of_measurement: "°F"
    pressure:
      name:                   ${loc} Pressure
      id:                     bme280_pressure
      accuracy_decimals: 2
      filters:
        - sliding_window_moving_average:
            window_size: 3
            send_every: 3
            send_first_at: 3
        - lambda: return x + id(pressure_calibration).state;
          # Calibration:
          # 3/10/22 | -0.75
    humidity:
      name:                   ${loc} Humidity
      filters:
        - sliding_window_moving_average:
            window_size: 3
            send_every: 3
            send_first_at: 3
        - lambda: return x + id(humidity_calibration).state;
  
  # =========================================
  # Pressure template sensors
  # =========================================
  - platform: template
    name: ${loc} Altitude
    lambda: |-
      const float STANDARD_SEA_LEVEL_PRESSURE = id(relative_pressure).state; // Current equivalent sea level pressure, in hPa
      const float c_temperature = (id(bme280_temperature).state - 32.0) * (5.0 / 9.0); // Temp in C
      return ((c_temperature + 273.15) / 0.0065) *
        (powf((STANDARD_SEA_LEVEL_PRESSURE / id(bme280_pressure).state), 0.190234) - 1); // in meter
    update_interval: 15s
    icon: 'mdi:altimeter'
    unit_of_measurement: 'm'
  - platform: template
    name: ${loc} Relative Pressure
    id: relative_pressure
    lambda: |-
      const float STANDARD_ALTITUDE = 282; // Current altitude, in meters
      const float c_temperature = (id(bme280_temperature).state - 32.0) * (5.0 / 9.0); // Temp in C
      return id(bme280_pressure).state / powf(1 - ((0.0065 * STANDARD_ALTITUDE) /
        (c_temperature + (0.0065 * STANDARD_ALTITUDE) + 273.15)), 5.257); // in hPa, multiply by 0.02953007 for inHg
    accuracy_decimals: 2
    update_interval: 15s
    unit_of_measurement: 'hPa'
    icon: 'mdi:gauge'
  - platform: template
    name: ${loc} Relative Pressure (US)
    lambda: |-
      const float STANDARD_ALTITUDE = 282; // Current altitude, in meters
      const float c_temperature = (id(bme280_temperature).state - 32.0) * (5.0 / 9.0); // Temp in C
      return (id(bme280_pressure).state / powf(1 - ((0.0065 * STANDARD_ALTITUDE) /
        (c_temperature + (0.0065 * STANDARD_ALTITUDE) + 273.15)), 5.257)) * 0.02953007;
    accuracy_decimals: 2
    update_interval: 15s
    unit_of_measurement: 'inHg'
    icon: 'mdi:gauge'
    
  # =========================================
  # UV Sensor (Offline)
  # =========================================
  #- platform: custom
  #  lambda: |-
  #    auto UV_sensor = new SI1145_sensor();
  #    App.register_component(UV_sensor);
  #    return {UV_sensor->visible_sensor, UV_sensor->ir_sensor, UV_sensor->uvindex_sensor};

  #  sensors:
  #  - name: ${loc} Visible Light
  #  - name: ${loc} IR Light
  #  - name: ${loc} UV Index
  #    accuracy_decimals: 2
  
  # =========================================
  # Voltage monitoring
  # ========================================= 
  - platform: adc
    pin: GPIO33
    id: "VCC"
    internal: true
    attenuation: 11db
    update_interval: 15s
    filters:
    # Calibration: +0.02
    - lambda: return x + id(voltage_calibration).state;
  - platform: template
    name: ${loc} Battery Voltage
    icon: 'mdi:lightning-bolt'
    id: battery_voltage
    unit_of_measurement: 'V'
    accuracy_decimals: 2
    update_interval: 15s
    # Total Calibration: +0.05
    # R1: 27000
    # R2: 100000
    # The below is the inverse of the voltage divider output voltage formula
    lambda: |-
      return ((id(VCC).state * 127000) / 100000);
    on_value:
      then:
      - lambda: |-
          using namespace std;
          //ESP_LOGW("main","battery = [%f]",x );
          static string voltage = to_string(id(battery_voltage).state);
          //ESP_LOGW("main","string_voltage = [%f]",id(battery_voltage).state );
          static string sleeptime = to_string((3.50-id(battery_voltage).state)*600+9);
          //ESP_LOGW("main","string_sleeptime = [%f]",(3.50-id(battery_voltage).state)*600+9 );
          if(x>3.49)
          {
            id(sleep_status).publish_state(9.25);
            id(deep_sleep_1).set_sleep_duration(9.25*60000);
            id(deep_sleep_1).setup();
          }
          else if(x<3.50)
          {
            id(sleep_status).publish_state((3.50-id(battery_voltage).state)*600+9);
            id(deep_sleep_1).set_sleep_duration(((3.50-x)*600+9)*60000);
            id(deep_sleep_1).setup();
          }
  - platform: template
    name: ${loc} ADC Voltage
    unit_of_measurement: 'V'
    accuracy_decimals: 2
    update_interval: 15s
    lambda: |-
      return id(VCC).state;
  - platform: template
    name: ${loc} Battery Level
    unit_of_measurement: '%'
    accuracy_decimals: 0
    update_interval: 15s
    lambda: |-
      return (((id(VCC).state - 2.00) / 1.30) * 100.00);
  - platform: adc
    pin: GPIO32
    attenuation: 11db
    name: ${loc} Soil Moisture
    update_interval: 5s
    filters:
    - lambda: |-
        //ESP_LOGW("main","soil = [%f]",x );
        if (x > 3.34) {
          return 0;
        } else if (x < 1.36) {
          return 100;
        } else {
          return (3.34-x) / (3.34-1.36) * 100.0;
        }
    - sliding_window_moving_average:
          window_size: 5
          send_every: 5
    unit_of_measurement: "%"
    
  - platform: wifi_signal
    name: ${loc} Signal
    icon: 'mdi:signal-variant'
    update_interval: 30s
  - platform: template
    name: "${loc} Sleep Time"
    icon: "mdi:sleep"
    id: sleep_status
    accuracy_decimals: 2
    update_interval: never
    unit_of_measurement: 'min'
  - platform: homeassistant
    id: temperature_calibration
    entity_id: input_number.${loc}_temperature_calibration
    accuracy_decimals: 1
    internal: true
  - platform: homeassistant
    id: pressure_calibration
    entity_id: input_number.${loc}_pressure_calibration
    accuracy_decimals: 2
    internal: true
  - platform: homeassistant
    id: humidity_calibration
    entity_id: input_number.${loc}_humidity_calibration
    accuracy_decimals: 1
    internal: true
  - platform: homeassistant
    id: voltage_calibration
    entity_id: input_number.${loc}_voltage_calibration
    accuracy_decimals: 2
    internal: true
      
deep_sleep:
  sleep_duration: 9.25min
  id: deep_sleep_1
  
binary_sensor:
  - platform: homeassistant
    id: prevent_deep_sleep
    entity_id: input_boolean.prevent_deep_sleep

script:
  - id: consider_deep_sleep
    mode: queued
    then:
      - delay: 45s
      - if:
          condition:
            binary_sensor.is_on: prevent_deep_sleep
          then:
            - deep_sleep.prevent: deep_sleep_1
            - logger.log: "Skipping deep sleep"
          else:
            - deep_sleep.enter: deep_sleep_1
      - script.execute: consider_deep_sleep
      
time:
  - platform: homeassistant
    id: esptime

It boots up fine and detects the bme280 sensor, but the only values I get returned look like this:

[22:21:54][D][sensor:094]: 'Garden Altitude': Sending state nan m with 1 decimals of accuracy
[22:21:54][D][sensor:094]: 'Garden Relative Pressure': Sending state nan hPa with 2 decimals of accuracy
[22:21:55][D][sensor:094]: 'Garden Battery Voltage': Sending state nan V with 2 decimals of accuracy
[22:22:04][D][sensor:094]: 'Garden Temperature': Sending state nan °F with 1 decimals of accuracy
[22:22:04][D][sensor:094]: 'Garden Pressure': Sending state nan hPa with 2 decimals of accuracy
[22:22:04][D][sensor:094]: 'Garden Humidity': Sending state nan % with 1 decimals of accuracy
[22:22:04][D][sensor:094]: 'Garden Soil Moisture': Sending state 100.00000 % with 2 decimals of accuracy
[22:22:05][D][sensor:094]: 'Garden Relative Pressure (US)': Sending state nan inHg with 2 decimals of accuracy
[22:22:06][D][sensor:094]: 'Garden ADC Voltage': Sending state nan V with 2 decimals of accuracy
[22:22:06][D][sensor:094]: 'Garden Battery Level': Sending state nan % with 0 decimals of accuracy
[22:22:07][D][sensor:094]: 'VCC': Sending state nan V with 2 decimals of accuracy
[22:22:08][D][sensor:094]: 'Garden Signal': Sending state -56.00000 dBm with 0 decimals of accuracy
[22:22:09][D][sensor:094]: 'Garden Altitude': Sending state nan m with 1 decimals of accuracy
[22:22:09][D][sensor:094]: 'Garden Relative Pressure': Sending state nan hPa with 2 decimals of accuracy
[22:22:10][D][sensor:094]: 'Garden Battery Voltage': Sending state nan V with 2 decimals of accuracy

and although I have a helper in homeassistant input_boolean.prevent_deep_sleep set up via the homeassistant binary sensor, the device always goes into deep sleep, as if it can’t read that value. What is going on? I never had these issues before. The only change I had to make to my code recently was changing the bme280 platform to bme280_i2c. I am using all new sensors and components, but even the adc sensor isn’t retuning any values, which should just be a voltage signal.

I did some further testing and noticed that my device was not showing up at all in homeassistant. As it turns out, it was coming up as an entirely new device that had not been added via the esphome integration. After deleting my old device instance and readding, everything is working now. I didn’t think to check homeassistant because the debug logs were sending nan values, but I believe that not being connected via the api was causing issues.