Unable to get mcp9808 to function but works with exact same hardware and arduino and Adafruit_MCP9808 library

i2c:
  sda: GPIO13
  scl: GPIO15
  scan: True

sensor:
  - platform: mcp9808
    address: 0x18
    name: "Temperature Sensor 1"
    id: temperature_sensor_1
    unit_of_measurement: "°F"
    update_interval: 1s
    filters:
      - lambda: return x * 9.0 / 5.0 + 32.0;
  - platform: mcp9808
    address: 0x19
    name: "Temperature Sensor 2"
    id: temperature_sensor_2
    unit_of_measurement: "°F"
    update_interval: 1s
    filters:
      - lambda: return x * 9.0 / 5.0 + 32.0;

Logs indicate

[22:06:33][I][logger:171]: Log initialized
[22:06:33][C][safe_mode:079]: There have been 0 suspected unsuccessful boot attempts
[22:06:33][D][esp32.preferences:114]: Saving 1 preferences to flash...
[22:06:33][D][esp32.preferences:143]: Saving 1 prefere[E][component:119]: Component mcp9808.sensor was marked as failed.
[22:06:33][E][component:164]: Component mcp9808.sensor set Error flag: unspecified
[22:06:33][E][mcp9808:026]: Temperature Sensor 1 manufacuturer id failed, device returned 0
[22:06:33][C][mcp9808:021]: Setting up Temperature Sensor 2...
[22:06:34][E][component:119]: Component mcp9808.sensor was marked as failed.
[22:06:34][E][component:164]: Component mcp9808.sensor set Error flag: unspecified
[22:06:34][E][mcp9808:026]: Temperature Sensor 2 manufacuturer id failed, device returned 0

The same hardware works fine with arduino with same i2c settings and the Adafruit_MCP9808 library.

esphome version Version: 2024.12.2
HomeAssistant Core : 2024.12.1
Any ideas on how I can get this to work with esphome?

I would turn on debug logging.

The log says the sensor did not return a manufacturer ID (it was zero). This sensor uses the i2c bus, which requires a pull-up resistor. Your YAML (and log) says you have two sensors connected. I would start with a single one. I did not find a lot of talk on this type of sensor so it is possible it doesn’t work reliably and might take some work to get it working.

Thanks. Unfortunately the logs show no new information with level debug. This sensor is documented under esphome but I haven’t found a full example yaml file. I have tried with just one sensor and the result is the same and both sensors work fine with arduino code. The sensor already has 10k pull up resistors built in. In the log the i2c scan finds both devices. This sensor is a very simple i2c device.
Here is a full log

[08:25:12]E (199) psram: PSRAM ID read error: 0x00ffffff
[08:25:12][I][logger:171]: Log initialized
[08:25:12][C][safe_mode:079]: There have been 0 suspected unsuccessful boot attempts
[08:25:12][D][esp32.preferences:114]: Saving 1 preferences to flash...
[08:25:12][D][esp32.preferences:143]: Saving 1 prefere[E][component:119]: Component mcp9808.sensor was marked as failed.
[08:25:12][E][component:164]: Component mcp9808.sensor set Error flag: unspecified
[08:25:12][E][mcp9808:026]: Temperature Sensor 1 manufacuturer id failed, device returned 0
[08:25:12][C][mcp9808:021]: Setting up Temperature Sensor 2...
[08:25:12][E][component:119]: Component mcp9808.sensor was marked as failed.
[08:25:12][E][component:164]: Component mcp9808.sensor set Error flag: unspecified
[08:25:12][E][mcp9808:026]: Temperature Sensor 2 manufacuturer id failed, device returned 0
[08:25:12][C][rotary_encoder:128]: Setting up Rotary Encoder 'Brightness Encoder'...
[08:25:12][C][wifi:048]: Setting up WiFi...
[08:25:12][C][wifi:061]: Starting WiFi...
[08:25:12][C][wifi:062]:   Local MAC: C0:4E:30:13:2B:78
[08:25:12][D][wifi:482]: Starting scan...
[08:25:12][W][component:157]: Component wifi set Warning flag: scanning for networks
[08:25:16][D][wifi:497]: Found networks:
[08:25:16][I][wifi:541]: - 'GODLIKE 2190' (52:E0:85:BD:37:15) [redacted]▂▄▆█
[08:25:16][D][wifi:542]:     Channel: 1
[08:25:16][D][wifi:543]:     RSSI: -52 dB
[08:25:16][D][wifi:546]: - 'tsunami'[redacted] (E0:3F:49:93:D4:38) [redacted]▂▄▆█
[08:25:16][I][wifi:313]: WiFi Connecting to 'GODLIKE 2190'...
[08:25:17][I][wifi:617]: WiFi Connected!
[08:25:17][C][wifi:428]:   Local MAC: C0:4E:30:13:2B:78
[08:25:17][C][wifi:433]:   SSID: 'GODLIKE 2190'[redacted]
[08:25:17][C][wifi:436]:   IP Address: 192.168.137.155
[08:25:17][C][wifi:440]:   BSSID: 52:E0:85:BD:37:15[redacted]
[08:25:17][C][wifi:441]:   Hostname: 'm5stack_din_meter'
[08:25:17][C][wifi:443]:   Signal strength: -52 dB ▂▄▆█
[08:25:17][C][wifi:447]:   Channel: 1
[08:25:17][C][wifi:448]:   Subnet: 255.255.255.0
[08:25:17][C][wifi:449]:   Gateway: 192.168.137.1
[08:25:17][C][wifi:450]:   DNS1: 192.168.137.1
[08:25:17][C][wifi:451]:   DNS2: 0.0.0.0
[08:25:17][D][wifi:626]: Disabling AP...
[08:25:17][C][api:026]: Setting up Home Assistant API server...
[08:25:17][I][app:062]: setup() finished successfully!
[08:25:17][W][component:170]: Component wifi cleared Warning flag
[08:25:17][W][component:157]: Component api set Warning flag: unspecified
[08:25:17][I][app:100]: ESPHome version 2024.12.2 compiled on Jan  7 2025, 08:20:22
[08:25:17][C][wifi:600]: WiFi:
[08:25:17][C][wifi:428]:   Local MAC: C0:4E:30:13:2B:78
[08:25:17][C][wifi:433]:   SSID: 'GODLIKE 2190'[redacted]
[08:25:17][C][wifi:436]:   IP Address: 192.168.137.155
[08:25:17][C][wifi:440]:   BSSID: 52:E0:85:BD:37:15[redacted]
[08:25:17][C][wifi:441]:   Hostname: 'm5stack_din_meter'
[08:25:17][C][wifi:443]:   Signal strength: -56 dB ▂▄▆█
[08:25:17][C][wifi:447]:   Channel: 1
[08:25:17][C][wifi:448]:   Subnet: 255.255.255.0
[08:25:17][C][wifi:449]:   Gateway: 192.168.137.1
[08:25:17][C][wifi:450]:   DNS1: 192.168.137.1
[08:25:17][C][wifi:451]:   DNS2: 0.0.0.0
[08:25:17][C][logger:185]: Logger:
[08:25:17][C][logger:186]:   Level: DEBUG
[08:25:17][C][logger:188]:   Log Baud Rate: 115200
[08:25:17][C][logger:189]:   Hardware UART: USB_CDC
[08:25:17][C][i2c.arduino:071]: I2C Bus:
[08:25:17][C][i2c.arduino:072]:   SDA Pin: GPIO13
[08:25:17][C][i2c.arduino:073]:   SCL Pin: GPIO15
[08:25:17][C][i2c.arduino:074]:   Frequency: 50000 Hz
[08:25:17][C][i2c.arduino:086]:   Recovery: bus successfully recovered
[08:25:17][I][i2c.arduino:096]: Results from i2c bus scan:
[08:25:17][I][i2c.arduino:102]: Found i2c device at address 0x18
[08:25:17][I][i2c.arduino:102]: Found i2c device at address 0x19
[08:25:17][C][spi:064]: SPI bus:
[08:25:17][C][spi:065]:   CLK Pin: GPIO6
[08:25:17][C][spi:066]:   SDI Pin: 
[08:25:17][C][spi:067]:   SDO Pin: GPIO5
[08:25:17][C][spi:072]:   Using HW SPI: SPI
[08:25:17][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Digital Input 1'
[08:25:17][C][gpio.binary_sensor:016]:   Pin: GPIO1
[08:25:17][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Digital Input 2'
[08:25:17][C][gpio.binary_sensor:016]:   Pin: GPIO2
[08:25:17][C][ledc.output:180]: LEDC Output:
[08:25:17][C][ledc.output:181]:   Pin GPIO9
[08:25:17][C][ledc.output:182]:   LEDC Channel: 0
[08:25:17][C][ledc.output:183]:   PWM Frequency: 1000.0 Hz
[08:25:17][C][ledc.output:184]:   Phase angle: 0.0°
[08:25:17][C][ledc.output:185]:   Bit depth: 14
[08:25:17][C][ili9xxx:094]: ili9xxx
[08:25:17][C][ili9xxx:094]:   Rotations: 180 °
[08:25:17][C][ili9xxx:094]:   Dimensions: 135px x 240px
[08:25:17][C][ili9xxx:095]:   Width Offset: 52
[08:25:17][C][ili9xxx:096]:   Height Offset: 40
[08:25:17][C][ili9xxx:102]:   Color mode: 16bit
[08:25:17][C][ili9xxx:111]:   Data rate: 40MHz
[08:25:17][C][ili9xxx:113]:   Reset Pin: GPIO8
[08:25:17][C][ili9xxx:114]:   CS Pin: GPIO7
[08:25:17][C][ili9xxx:115]:   DC Pin: GPIO4
[08:25:17][C][ili9xxx:117]:   Color order: BGR
[08:25:17][C][ili9xxx:118]:   Swap_xy: NO
[08:25:17][C][ili9xxx:119]:   Mirror_x: NO
[08:25:17][C][ili9xxx:120]:   Mirror_y: NO
[08:25:17][C][ili9xxx:121]:   Invert colors: YES
[08:25:17][C][ili9xxx:126]:   Update Interval: 1.0s
[08:25:17][C][light:092]: Light 'Backlight'
[08:25:17][C][light:094]:   Default Transition Length: 1.0s
[08:25:17][C][light:095]:   Gamma Correct: 2.80
[08:25:17][C][homeassistant.time:010]: Home Assistant Time:
[08:25:17][C][homeassistant.time:011]:   Timezone: 'EST5EDT,M3.2.0,M11.1.0'
[08:25:17][C][mcp9808:037]: Temperature Sensor 1:
[08:25:17][C][mcp9808:038]:   Address: 0x18
[08:25:17][E][mcp9808:040]: Communication with Temperature Sensor 1 failed!
[08:25:17][C][mcp9808:042]:   Update Interval: 1.0s
[08:25:17][C][mcp9808:043]:   Temperature 'Temperature Sensor 1'
[08:25:17][C][mcp9808:043]:     Device Class: 'temperature'
[08:25:17][C][mcp9808:043]:     State Class: 'measurement'
[08:25:17][C][mcp9808:043]:     Unit of Measurement: '°F'
[08:25:17][C][mcp9808:043]:     Accuracy Decimals: 1
[08:25:17][E][component:082]:   Component mcp9808.sensor is marked FAILED
[08:25:17][C][mcp9808:037]: Temperature Sensor 2:
[08:25:17][C][mcp9808:038]:   Address: 0x19
[08:25:17][E][mcp9808:040]: Communication with Temperature Sensor 2 failed!
[08:25:17][C][mcp9808:042]:   Update Interval: 1.0s
[08:25:17][C][mcp9808:043]:   Temperature 'Temperature Sensor 2'
[08:25:17][C][mcp9808:043]:     Device Class: 'temperature'
[08:25:17][C][mcp9808:043]:     State Class: 'measurement'
[08:25:17][C][mcp9808:043]:     Unit of Measurement: '°F'
[08:25:17][C][mcp9808:043]:     Accuracy Decimals: 1
[08:25:17][E][component:082]:   Component mcp9808.sensor is marked FAILED
[08:25:17][C][psram:020]: PSRAM:
[08:25:17][C][psram:021]:   Available: NO
[08:25:17][C][rotary_encoder:160]: Rotary Encoder 'Brightness Encoder'
[08:25:17][C][rotary_encoder:160]:   State Class: ''
[08:25:17][C][rotary_encoder:160]:   Unit of Measurement: 'steps'
[08:25:17][C][rotary_encoder:160]:   Accuracy Decimals: 0
[08:25:17][C][rotary_encoder:160]:   Icon: 'mdi:rotate-right'
[08:25:17][C][rotary_encoder:161]:   Pin A: GPIO40
[08:25:17][C][rotary_encoder:162]:   Pin B: GPIO41
[08:25:17][C][rotary_encoder:176]:   Restore Mode: Always zero
[08:25:17][C][rotary_encoder:186]:   Resolution: 4 Pulse Per Cycle
[08:25:17][C][captive_portal:089]: Captive Portal:
[08:25:17][C][mdns:116]: mDNS:
[08:25:17][C][mdns:117]:   Hostname: m5stack_din_meter
[08:25:17][C][esphome.ota:073]: Over-The-Air updates:
[08:25:17][C][esphome.ota:074]:   Address: m5stack_din_meter.local:3232
[08:25:17][C][esphome.ota:075]:   Version: 2
[08:25:17][C][esphome.ota:078]:   Password configured
[08:25:17][C][safe_mode:018]: Safe Mode:
[08:25:17][C][safe_mode:020]:   Boot considered successful after 60 seconds
[08:25:17][C][safe_mode:021]:   Invoke after 10 boot attempts
[08:25:17][C][safe_mode:023]:   Remain in safe mode for 300 seconds
[08:25:17][C][api:140]: API Server:
[08:25:17][C][api:141]:   Address: m5stack_din_meter.local:6053
[08:25:17][C][api:143]:   Using noise encryption: YES
[08:25:32][D][api:103]: Accepted 192.168.137.83
[08:25:32][W][component:170]: Component api cleared Warning flag
[08:25:32][D][api.connection:1446]: Home Assistant 2024.12.1 (192.168.137.83): Connected successfully
[08:25:32][D][time:051]: Synchronized time: 2025-01-07 08:25:34
[08:26:08][I][safe_mode:041]: Boot seems successful; resetting boot loop counter
[08:26:08][D][esp32.preferences:114]: Saving 1 preferences to flash...
[08:26:08][D][esp32.preferences:143]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed

and my full yaml

esphome:
  name: m5stack_din_meter
  friendly_name: m5stack_din_meter

  platformio_options:
    board_build.mcu: esp32s3
    board_build.name: "M5Stack StampS3"
    board_build.upload.flash_size: 8MB
    board_build.upload.maximum_size: 8388608
    board_build.vendor: M5Stack
  
  on_boot:
    then:
      - light.turn_on: 
          id: backlight
          brightness: 80%
  
  # Custom libraries
  libraries:
    - https://github.com/pbolduc/AsyncTCP.git @ 1.2.0
      
esp32:
  board: esp32-s3-devkitc-1 #m5stack-stamps3
  variant: esp32s3
  framework:
    type: arduino

# Enable logging
logger:
  level: DEBUG
# Enable Home Assistant API
api:
  encryption:
    key: "Xm2lOS48ccQsZJdVr4qkphXmIiTapnjSJXlbL0yU7Tw="

ota:
  - platform: esphome
    password: "d588c78e6f936dc099ec04b9b22191c3"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "M5Stack-Din-Meter"
    password: "DUGys6yLNKFD"

captive_portal:

time:
  - platform: homeassistant
    id: homeassistant_time

i2c:
  sda: GPIO13
  scl: GPIO15
  scan: True

sensor:
  - platform: mcp9808
    address: 0x18
    name: "Temperature Sensor 1"
    id: temperature_sensor_1
    unit_of_measurement: "°F"
    update_interval: 1s
    filters:
      - lambda: return x * 9.0 / 5.0 + 32.0;
  - platform: mcp9808
    address: 0x19
    name: "Temperature Sensor 2"
    id: temperature_sensor_2
    unit_of_measurement: "°F"
    update_interval: 1s
    filters:
      - lambda: return x * 9.0 / 5.0 + 32.0;
  - platform: rotary_encoder
    id: encoder
    name: "Brightness Encoder"
    pin_a: 
      number: GPIO40
    pin_b:
      number: GPIO41
    min_value: 0
    max_value: 40
    resolution: 4
    restore_mode: ALWAYS_ZERO
    on_value:
      then:
      - logger.log:
            format: "Rotary value: %.0f"
            args: ["id(encoder).state"]
      - light.turn_on: 
          id: backlight 
          brightness: !lambda |-
            float raw = id(encoder).state;
            // scale between 158 and 255 over 255
            float brightness_val = 1.6 - raw / 40.0;
            if (brightness_val < 0.6) {
              brightness_val = 0.6;
            }
            return brightness_val;
            
binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO1
      mode:
        input: true
        pulldown: true
    name: "Digital Input 1"
    id: digital_input1
  - platform  : gpio
    pin:
      number: GPIO2
      mode:
        input: true
        pulldown: true
    name: "Digital Input 2"
    id: digital_input2

spi:
  clk_pin: GPIO6
  mosi_pin: GPIO5
  
output:
  - platform: ledc
    pin: GPIO9
    id: tft_backlight

light:
  - platform: monochromatic
    output: tft_backlight
    id: backlight
    name: "Backlight"

# 1.14 inch, 135*240 Colorful TFT LCD, ST7789v2
display:
  - platform:  ili9xxx    #st7789v
    model:  st7789v #TTGO TDisplay 135x240
    cs_pin: GPIO7
    reset_pin: GPIO8
    dc_pin: 
      number: GPIO4
    rotation: 180
    dimensions:
      height: 240
      width: 135
      offset_height: 40
      offset_width: 52
    invert_colors: true
    lambda: |-
      it.fill(id(color_black));
      it.rectangle(0, 0, 40, 50, id(color_green));
      it.printf(10, 5, id(f30), id(color_white), "1");
      it.strftime(55, 0, id(f30), id(color_green), "%H:%M", id(homeassistant_time).now());
      it.printf(10, 65, id(f30), id(color_white), "%.0f F", id(temp1));
      it.printf(10, 120, id(f30), id(color_white), "%.0f F", id(temp2));
      if (id(digital_input1).state) {
        it.filled_circle(25, 210, 20, id(color_green));
      } else {
        it.filled_circle(25, 210, 20, id(color_red));
      }
      if (id(digital_input2).state) {
        it.filled_circle(90, 210, 20, id(color_green));
      } else {
        it.filled_circle(90, 210, 20, id(color_red));
      }
      
globals:
  - id: temp1
    type: float
    restore_value: no
    initial_value: '0.0'
  - id: temp2
    type: float
    restore_value: no
    initial_value: '0.0'
  - id: brightness
    type: int
    restore_value: yes
    initial_value: '128'

color:
  - id: color_red
    red_int: 255
    green_int: 0
    blue_int: 0
    white_int: 0
  - id: color_green
    red_int: 0
    green_int: 255
    blue_int: 0
    white_int: 0
  - id: color_white
    red_int: 255
    green_int: 255
    blue_int: 255
    white_int: 255
  - id: color_black
    red_int: 0
    green_int: 0
    blue_int: 0
    white_int: 0

interval:
  - interval: 1s
    then:
      - lambda: |-
          id(temp1) = id(temperature_sensor_1).state;
          id(temp2) = id(temperature_sensor_2).state;

font:
  - file:
      type: gfonts
      family: Noto Sans Display
      weight: 500
    glyphs: ['&', '@', '!', ',', '.', '"', '%', '(', ')', '+', '-', '_', ':', '°', '0',
        '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
        'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
        'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', 'a', 'b', 'c', 'd', 'e', 'f',
        'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
        'u', 'v', 'w', 'x', 'y', 'z','å', 'ä', 'ö', '/', 'µ', '³', '’']
    id: f30
    size: 30
  - file:
      type: gfonts
      family: Noto Sans Display
      weight: 500
    glyphs: ['&', '@', '!', ',', '.', '"', '%', '(', ')', '+', '-', '_', ':', '°', '0',
        '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
        'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
        'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', 'a', 'b', 'c', 'd', 'e', 'f',
        'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
        'u', 'v', 'w', 'x', 'y', 'z','å', 'ä', 'ö', '/', 'µ', '³', '’']
    id: f20
    size: 20

The first log indicated the sensor did not return a correct manufacturer id. The second log does not show that error. What else did you change, if anything?

If I was working on this I would remove everything but the one sensor to make it as simple as possible and remove all other things that could be causing issues and also easier to see what is there.

I am guessing that you have never seen the sensor work and didn’t try the “hello world” test for a new (to you) sensor. I generally do that whenever I am working with something new (to me) so I can be sure I know how it works and what it needs.

It is also possible that the logs are telling you what you need to know, your sensors are both defective. Right now there are two many unknown to know for sure anything. So you need to remove unknowns until you find the issue.

What happens when you disconnect both sensors? Does the log show them as discovered?

I would also try with a different esp device to make sure there is nothing wrong with the m5 you are using.

I would also try with a different i2c device to make sure the i2c on the m5 device is working correctly.

I have been measuring temperature for a long time, just curious why you chose this sensor instead of any of the more common choices?

Sage advice indeed. As suggested I walked my code back to its simplest form to test just one sensor with nothing else. KISS. A single mcp9808 worked fine by itself. I then proceeded to add other parts of my code back until I found the issue was some conflict with the display component. I switched my i2c port / pins I was using to those I was using for my binary sensors and found there was no conflict. I then put back my binary sensor component pins to those previously used for i2c and everything functions now.

Thanks so much for your assistance.
To answer your question as to why I chose the mcp9808 sensor, is that it fit my application where comments from others seemed to indicate issues with some of the one wire sensors.

Funny thing was that with the original pin configurations in arduino code everything worked fine it just was not setup for esphome / HA.

Glad you got it figured out and reported back the reason. That is a weird incompatibility.

The DS18B20 has lots of fakes, but they still generally work. The one you are using looks more accurate, so it is a good choice too.

The ones I have I got from Adafruit. Are they Chinese knock offs? I don’t know. They seem to match each other and other sensors I have thus far.
I am curious what temperature sensors you use with your experience.
Thanks again!

Adafruit should have genuine parts, and it sounds like that wasn’t the problem anyway.

I mostly use the DS18B20, since that was pretty much the best choice 10-15 years ago. It seems there are a lot of fakes now. I think I noticed this many years ago, but they also worked well enough for my needs (freeze protection).

I have some of them in harsh conditions so like that there are epoxy and stainless steel covered ones. But even those vary. I have a compost pile that gets hot and I have to replace the sensors after some time.