Esphome config to turn off ssd1306 OLED display with a trigger

Hi.

I have configured a ssd1306 OLED display to display some sensor values. I want to turn on(loop through 3 pages) the display when the occupancy sensor is on and turn off(reduce the brightness to 0 or show a blank page) when the occupancy sensor is off. Need some help with writing the yaml logic. Here is my current config.

    
display:
  - platform: ssd1306_i2c
    i2c_id: bus_b  
    model: "SSD1306 128x64"
    id: my_display
    # reset_pin: D0
    address: 0x3C
    update_interval: 1s
    pages:
      - id: page1    
        lambda: |-
          //it.strftime(0, 15, id(font2), "%I:%M %p", id(esptime).now());
          it.strftime(5, 0, id(font1), "%Y-%m-%d", id(esptime).now());
          it.strftime(0, 20, id(font2), "%I:%M %p", id(esptime).now());
      - id: page2    
        lambda: |-          
          it.printf(6, 3, id(font3), "T: %.0f°C", id(temperature_sensor).state);
          it.printf(6, 27, id(font3), "H: %.0f%%", id(humidity_sensor).state);
      - id: page3    
        lambda: |-          
          it.printf(0, 3, id(font1), "IAQ: %s", id(IAQ_Classification).state.c_str());
          it.printf(0, 27, id(font1), "CO2: %.0f ppm%", id(co2).state);

interval:
  - interval: 5s
    then:        
      - display.page.show_next: my_display
      - component.update: my_display


binary_sensor:
  - platform: gpio
    ..

  - platform: gpio
    name: ${room} mmWave
    id: mmWave
    ...

  - platform: template
    name: ${room} Occupancy
    id: occupancy
    device_class: occupancy
    filters:
      - delayed_off: 15s
    lambda: |-
      if ( id(mmWave).state or id(pir_motion_sensor).state) {
        return true;
      } 
      else if (id(mmWave).state == 0 and id(pir_motion_sensor).state == 0) {
        return false;
      } 
      else {
        return id(occupancy).state;
      } 

The SSD1306 is an OLED display, thus you can’t control the brightness and there is no backlight you can turn off. You can only display a blank page.

You will need to do something like:

      - id: page4   
        lambda: |-   
          it.print(0, 0, id(my_font), "");

interval:
  - interval: 5s
    then:        
      - if:
          condition:
            binary_sensor.is_on: occupancy
          then:
            if: 
              condition:
                display.is_displaying_page: page1
              then:
                display.page.show: page2
              else:
                if:
                  condition:
                    display.is_displaying_page: page2
                  then:
                    display.page.show: page3
                  else:
                    display.page.show: page1
          else:
            - display.page.show: page4
      - component.update: my_display 

Messy I know and not tested…

1 Like

Yeh! It worked! Thanks a lot! I though of something similar but I was not able to get the if condition working correctly!

1 Like

Hello!

Is it possible to create a switch to homeassistant to turn off the OLED Screen?
I know that it is possible to switch it off via trigger in esphome.

Thanks in advance!

Sure. Create a light entity and use different display pages for the on and off state. It’s even possible to change contrast (which has more or less the same effect of brightness in a ssd1306)…

light:
  - platform: monochromatic
    name: "OLED "
    output: oled_bc
    id: oled
    on_turn_on:
      then: 
      - display.page.show: page1  
      - component.update: ssd1306   #the component id which need to be updated
    on_turn_off:
      then:
      - display.page.show: page2   # a empty page....
      - component.update: ssd1306  
output:
  - platform: template
    id: oled_bc
    type: float
    write_action:
      then:
      - lambda: id(ssd1306).set_contrast(state);
3 Likes

Thank you!

I actually solved it a bit differently, but your code led me to right direction.

Instead of using different pages, I just used lambda to turn on and off the display.

light:
  - platform: monochromatic
    name: "OLED "
    output: oled_bc
    on_turn_on:
      then: 
      - lambda: id(oled_roosa).turn_on();
    on_turn_off:
      then:
      - lambda: id(oled_roosa).turn_off();
output:
  - platform: template
    id: oled_bc
    type: float
    write_action:
      then:
      - lambda: id(oled_roosa).set_contrast(state);

3 Likes