Can't get temp/humidity data to display on a SSD1306

Ok, I’ve combed through the examples here and here and I can get the “Hello World” to show on the OLED screen (SSD1306 128x64). However, when I try to pull in temp/humidity info from homeassistant, the screen is blank. Has anyone done this successfully, can you take a look or share your code?

Here’s my yaml for this file. I will actually have the DHT22 sensor connected to the same NodeMCU as the SSD1306, so if anyone can offer code that would allow me to capture that info, that would work too.

sensor:
  - platform: homeassistant
    id: office_temperature
    entity_id: sensor.office_temperature
    internal: true
   
i2c:
  sda: D1
  scl: D2
  scan: False

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    reset_pin: D0
    address: 0x3C
    lambda: |-
     // Print inside temperature (from homeassistant sensor)
      if (id(office_temperature).has_state()) {
        it.printf(0, 8, id(font1), TextAlign::TOP_RIGHT , "%.1f°C", id(office_temperature).state);
      }
      
font:
  - file: 'OpenSans-Regular.ttf'
    id: font1
    size: 28

I use this one

thanks myle, however I’m not using a MQTT broker, but ESPhome instead

I do it like this:

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    reset_pin: D0
    address: 0x3C
    lambda: |-
     // Print inside temperature (from homeassistant sensor)
      if (id(office_temperature).state == id(office_temperature).state) {
        it.printf(0, 8, id(font1), TextAlign::TOP_RIGHT , "%.1f°C", id(office_temperature).state);
      } else {
        it.print(0, 8, id(font1), TextAlign::TOP_RIGHT , "waiting");
      }

I’ve never used has_state(). But comparing a float to itself will also return false if the float is NaN. And I print something on screen in the else as well, so that I always have something on screen even if the rest isn’t working.

1 Like

swiftlyfalling, I used your code, it compiled and uploaded but the screen is still blank. I’ve double checked my entity_id to make sure it is correct and it is. I know the display works because I can use the “hello world” code and it shows on the screen.

Here’s what I put, in case I’m missing something…

sensor:
  - platform: homeassistant
    id: office_temperature
    entity_id: sensor.office_temperature
    internal: true
    
i2c:
  sda: D1
  scl: D2
  scan: False

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    reset_pin: D0
    address: 0x3C
    lambda: |-
     // Print inside temperature (from homeassistant sensor)
      if (id(office_temperature).state == id(office_temperature).state) {
        it.printf(0, 8, id(font1), TextAlign::TOP_RIGHT , "%.1f°C", id(office_temperature).state);
      } else {
        it.print(0, 8, id(font1), TextAlign::TOP_RIGHT , "waiting");
      }
      
font:
  - file: 'OpenSans-Regular.ttf'
    id: font1
    size: 28

Here’s the log output:
[19:13:36][C][i2c:035]: I2C Bus:
[19:13:36][C][i2c:036]: SDA Pin: GPIO5
[19:13:36][C][i2c:037]: SCL Pin: GPIO4
[19:13:36][C][i2c:038]: Frequency: 50000 Hz
[19:13:36][C][logger:142]: Logger:
[19:13:36][C][logger:143]: Level: DEBUG
[19:13:36][C][logger:144]: Log Baud Rate: 115200
[19:13:36][C][logger:145]: Hardware UART: UART0
[19:13:36][C][display.ssd1306:312]: I2C SSD1306
[19:13:36][C][display.ssd1306:312]: Rotations: 0 °
[19:13:36][C][display.ssd1306:312]: Dimensions: 128px x 64px
[19:13:36][C][display.ssd1306:313]: Address: 0x3C
[19:13:36][C][display.ssd1306:314]: Model: SSD1306 128x64
[19:13:36][C][display.ssd1306:315]: Reset Pin: GPIO16 (Mode: OUTPUT)
[19:13:36][C][display.ssd1306:316]: External VCC: NO
[19:13:36][C][display.ssd1306:317]: Update Interval: 5000 ms
[19:13:36][C][sensor.homeassistant:031]: Homeassistant Sensor ‘office_temperature’
[19:13:36][C][sensor.homeassistant:031]: Unit of Measurement: ‘’
[19:13:36][C][sensor.homeassistant:031]: Accuracy Decimals: 0
[19:13:36][C][sensor.homeassistant:032]: Entity ID: ‘sensor.office_temperature’
[19:13:36][C][api:101]: API Server:
[19:13:36][C][api:102]: Address: test_nodemcu.local:6053
[19:13:36][C][ota:127]: Over-The-Air Updates:
[19:13:36][C][ota:128]: Address: test_nodemcu.local:8266

Does it matter if the sensor is connected to the same NodeMCU as the display? Eventually, I want to add the display to a nodemcu that also has a DHT22, PIR, and light sensor, and I want the display to show temp and humidity and when there’s motion

It’s still not clear to me how this should function. Is the nodemcu firmware going to send the temp and humidity data to homeassistant and then the same nodemcu will pull it back down to display it on the OLED? Is that what will happen?

Can you show the Hello World code you are using that does work?

Sure, i pulled it straight from the SSD1306 OLED Display ESPHome website.

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    reset_pin: D0
    address: 0x3C
    lambda: |-
      it.print(0, 0, id(font), "Hello World!");

When I use this, Hello World shows on the display. I think my problem is related to obtaining the entity ID…maybe?

Take the TextAlign bits out, I think maybe it’s printing so close to the corner that you’re missing it.

The code you’re using should print SOMETHING if that hello world works. Because, even if the sensor data isn’t present, you should still see the °C.

You may also try taking the ° out, and just use the “C” to make sure there’s not an encoding issue with that special character.

Ok, I tried your suggestion and I do have the word “waiting” now. I took out the textalign and the degree symbol. I waited for several minutes but nothing appears. I don’t think it’s pulling down the entity_id data.

Here’s my code, I now see waiting:

lambda: |-
 // Print office temperature (from homeassistant sensor)
  if (id(office_temperature).state == id(office_temperature).state) {
    it.printf(0, 8, id(font1), "%.1fC", id(office_temperature).state);
  } else {
    it.print(0, 8, id(font1), "waiting");
  }

swiftlyfalling, thanks so much for hanging in there and helping me. I figured it out, it’s working now. I had been using a test nodemcu with the display attached because I didn’t want to fiddle with my sensor nodemcu. The problem was I had failed to register the test nodemcu in homeassistant. Once I registered it, it pulled the temp in immediately. Sorry, I’m a complete noob.

2 Likes

Sorry to hijack this thread but, I’m using same oled and have it displaying temperature and time and now want to display on or off for a switch connected to the same node mcu what’s the easiest way to get state of switch and display thanks

Sorry to hijack this thread but, I’m using same oled and have it displaying temperature and time and now want to display on or off for a switch connected to the same node mcu what’s the easiest way to get state of switch and display thanks

if (id(switch).state) {
   ... true code 
} else {
   ... false code
}
2 Likes

also hijacking this, can anyone help?

2 Likes

I had exact same problem and used your exact solution - so thanks!

Thanks for sharing the solution, it worked for me!!

1 Like