Sensor data from HA is not received in ESPHome

Hello, I’ve just followed this tutorial for displaying time and temperature on an OLED display.

However the sensor values from HA don’t show up at all. If I move the printf outside of the if statement so it always gets displayed, I get a NaN.
I thought that this might be an authentication issue, however the time section is also fetched from the HA instance and that works fine.

I’m on the latest HA/ESPHome, I haven’t done anything specific than to add the 2 sensors from HA.
In order to debug, I’ve changed one of them to the random_reading analog read sensor (pin not connected to anything, just noise). This shows up on the screen just fine.

However I’m unable to fetch any sensor information from HA to the ESP32.
I’ve tried this with multiple working sensors and two different HA instances - still nothing.

Full config:

esphome:
  name: lcd-screen-test

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "rSYZvkwWTlLfwqhh/6cYPcNrukLejisZ1iOewApI+0o="

ota:
  password: "61bd963f509796dfc6b2166ad2b3cddf"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Lcd-Screen-Test Fallback Hotspot"
    password: "MC2M96jA5mBl"

captive_portal:

time:
  - platform: homeassistant
    id: esptime

sensor:
  - platform: homeassistant
    id: livingroom_temp
    entity_id: sensor.temperature_livingroom

  - platform: adc
    pin: GPIO32
    id: random_reading
    update_interval: 1s
    unit_of_measurement: "V"
    
font:
  - file: 'fonts/slkscr.ttf'
    id: font1
    size: 8

  - file: 'fonts/BebasNeue-Regular.ttf'
    id: font2
    size: 48

  - file: 'fonts/arial.ttf'
    id: font3
    size: 14
    
i2c:
  sda: GPIO21
  scl: GPIO22
  scan: false

display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    address: 0x3C
    lambda: |-
      // Print "Mitt Smarta Hus" in top center.
      it.printf(64, 0, id(font1), TextAlign::TOP_CENTER, "Please work");

      // Print time in HH:MM format
      it.strftime(0, 60, id(font2), TextAlign::BASELINE_LEFT, "%H:%M", id(esptime).now());

      // Print inside temperature (from homeassistant sensor)
      if (id(livingroom_temp).has_state()) {
        it.printf(127, 23, id(font3), TextAlign::TOP_RIGHT , "%.1f°", id(livingroom_temp).state);
      }

      // Print outside temperature (from homeassistant sensor)
      if (id(random_reading).has_state()) {
        it.printf(127, 60, id(font3), TextAlign::BASELINE_RIGHT , "%.1f°", id(random_reading).state);
      }

Here are my startup logs.
The only thing that I can see is that weird api.connection - Socket operation failed: CONNECTION_CLOSED message.

Actual output:
Bottom right => Analog read value, correct.
Top Right => Nothing (or NaN)

image

Edit: Try removing the encryption key. Does it work?

api:

#instead of
#api:
#  encryption:
#    key: "rSYZvkwWTlLfwqhh/6cYPcNrukLejisZ1iOewApI+0o="

For the code:

sensor:
  - platform: homeassistant
    id: room_temp
    entity_id: sensor.phillips_room_temperature
    
interval:
  - interval: 1s
    then: 
      - lambda: |
            ESP_LOGI("log", "%f", id(room_temp).state);

I get the log result:

And the sensor looks like so:

Are you able to reproduce what I’m getting and does everything look consistent with your setup?

Try:

        it.printf(127, 23, id(font3), TextAlign::TOP_RIGHT , "%.1f°", id(livingroom_temp).state.c_str());

The printf function expects a char array, the state of the sensor is a string.

@phillip1 - No, no change when removing the encryption key.
The ESP_LOGI statement just outputs a NaN in the console.
I’ve added a second ESP_LOGI under that with the whole object, but

image

I tried playing with the different format options and got this:
warning: format '%s' expects argument of type 'char*', but argument 5 has type 'double' [-Wformat=]

I’m guessing originally it doesn’t want to format a double as float? Obviously I’m trying to force it to string now, but at least I got that out of the way. No idea how to solve that though.


@zoogara - id(livingroom_temp).state.c_str() throws an error:

/config/esphome/lcd-screen-test.yaml:77:91: error: request for member 'c_str' in 'livingroom_temp->esphome::homeassistant::HomeassistantSensor::<anonymous>.esphome::sensor::Sensor::state', which is of non-class type 'float'

Oops my bad.

Try:

          float temp = atof(id(livingroom_temp).state.c_str());
          it.printf(127, 23, id(font3), TextAlign::TOP_RIGHT , "%.1f°", temp;

Although the error message seems to indicate your home assistant sensor is a float already… Which is strange because I use them a lot and I thought they were always strings…

Nope, does not work either :frowning:

image

Yeah - see my edit above… I have very similar code for my weather display - let me look at it again.

This is a custom sensor that extracts the attribute of my AC unit, nothing special but I don’t know if that affects the underlying type.
I’ve just changed the entity_id to the temperature of a Shelly device and get the same behaviour.

I think that the id(livingroom_temp).has_state() returning false is the main issue here.

If that’s true, the printf line should render NaN on the screen, however it doesn’t.

That’s why I’m leaning to some sort of communication issue between the esp32 and the HA instance. However as I said, the time is fetched the same way and it’s working fine.

Ah - sorry I have been on the wrong track all along and just thought it wasn’t displaying - but you still aren’t getting the sensor data from HA - sorry.

Doing some reading, this can occur if the ESPhome device hasn’t been added to the ESPhome integration in HA. Maybe remove it and add again?

Removing and re-adding it from ESPHome didn’t work. I tried it before and I tried creating the same config (different encryption key) in another HA instance - no change.

However I’ve just deleted it from ESPHome and added it as a totally new device and it worked (Integrations => ESPHome => Add (IP, key). This is still not ideal because that way I can’t update the device at all, but at least it works.

Another odd thing I’ve noticed is that when I flash the board, sometimes the time does not update as well. Sometimes power-cycling it makes it reconnect to HA after 2-3 seconds, but some other times the only way to force it to update the time is to start the wireless log stream in the browser. Then it connects and the time uptates. Still no temperature tho.

When you tested the yaml:

sensor:
  - platform: homeassistant
    id: room_temp
    entity_id: sensor.phillips_room_temperature
    
interval:
  - interval: 1s
    then: 
      - lambda: |
            ESP_LOGI("log", "%f", id(room_temp).state);

Did you put it in with your other yaml?

If you did, try a bare minimum example instead removing the font, time, random_reading, i2c, display, encryption key.

These things can most definitely cause network issues in edge cases. Sometimes it’s a ram issue, sometimes the esp’s thread gets stuck doing something else instead of networking.

Still no luck when using only the sensor + interval… I will log this as an issue on the repo I guess…

And thank you a lot for the help so far!

Opened an issue on the ESPHome repo. Link:

That’s an encouraging error.

Sounds like that’s an encryption error bad_indicator errorno=11

You need to remove & add back the ESP node to HA since you used the same node which had an encryption key originally set up. Making some assumptions here.

I think your issue is duplicate:

Still have other ideas if that doesn’t solve it.
Such as extracting it within ESPHome instead, and trying a different, simpler sensor.

sensor:
  - platform: homeassistant
    id: current_temperature
    entity_id: climate.air_conditioner
    attribute: current_temperature

Hope someone can help with a soution
I have tried for 3 days now, and have the same problem.
Time is displayed, but my temperatures just read Nan
If i tries “text_sensor” and …state.c_str() it reads 0.0

esphome:
  name: "lcd-display"

esp8266:
  board: d1_mini

time:
  - platform: homeassistant
    id: esptime

i2c:
  sda: GPIO4
  scl: GPIO5


sensor:
  - platform: homeassistant
    id: kontor1_temperatur
    name: kontor1_temperatur
    entity_id: sensor.nodemcutemp1
    # internal: true
    attribute: state
  
  
display:
  - platform: lcd_pcf8574
    dimensions: 16x2
    address: 0x27
    update_interval: 30s
    lambda: |-
      it.strftime(11, 0, "%H:%M", id(esptime).now());  # WORKS fine
      it.strftime(11, 1, "%d/%m", id(esptime).now());  # WORKS fine
      it.printf(0, 0, "Ude :");
      it.printf(0, 1, "Inde:");
      if (id(kontor1_temperatur).has_state()) {
        it.printf(5, 0, "%5.1f", id(kontor1_temperatur).state);   # Does not work
      } else {
        it.printf(8, 0, "-");     # writes -
      }
      if (id(kontor1_temperatur).has_state()) {
        it.printf(5, 1, "%5.1f", id(kontor1_temperatur).state);   # Does not work
      } else {
        it.printf(8, 1, "-");     # writes -
      }
      if (id(kontor1_temperatur).has_state()) {
        it.printf(0, 1, "%2.0f", id(kontor1_temperatur).state);   # Does not work
      } else {
        it.printf(0, 1, "-");     # writes -    Yes know it is in text, but was trying to get from different sensors
      }


# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxx"

ota:
  password: "xxxxxx"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Lcd-Display Fallback Hotspot"
    password: "xxxx"

captive_portal:

@phillip1 is correct and the issue is resolved!

This is my debug ESP and I’ve tried multiple configurations on it. Because of that, it’s not auto-linked and HA is getting confused which ESP is that - the MAC is the same, the hostname is different, the encryption key is changed.

Simply removing it from the integrations page and re-adding it with the correct encryption key did the trick. I don’t know why I didn’t try that earlier but that solved it.

Thanks!

1 Like

For anyone who arrives here later; I had a very similar sounding/looking issue with ESPhome 2024.21 and HASSIO 2024.2.5 that was resolved in a different way;

HA Settings → Devices & Services → Integrations → ESPHome → → Configure → “Allow the device to make Home Assistant Service Calls”

3 Likes