ESPHome show light brightness on OLED Display

hello,
well i’ll have a issue…
i have a OLED display SSD1306 connected to my ESP32 to show Temp /humid from the bmp280.
have also a 4 x relay for light and a PWM channel for LED light dimming…
all works well and i’ll can display the states of the relay and all other info on my display…
the last info i’ll need is the PWM or lets say the light brightness value
Bildschirmfoto 2020-08-15 um 11.44.10
i’ll have done some different constellations… but could not find any info on the web how to get that value on my oled…

here is my code:

  - platform: homeassistant
    id: grow1led_pwmx
    entity_id: light.grow1_light
    internal: true
    
  - platform: template
    name: "${device_name} brightness"
    id: sensor_g_bright
    internal: true
    update_interval: 25ms
    # Ensure on_value only triggered when brightness (0-255) changes
    filters:
      delta: 0.8
    # Read brightness (0 - 1) from light , convert to (0-255) for MCU
    lambda: |-
      if (id(light_main).remote_values.is_on()) {
        return (int(id(light_main).remote_values.get_brightness() * 255));
      }
      else {
        return 0;
      }
    # On Change send to MCU via UART
    on_value:
      then:
        - logger.log:
            level: INFO
            format: "Sensor Value Change sent to UART %3.1f"
            args: ["id(sensor_g_bright).state"]
  
text_sensor:
  - platform: homeassistant
    id: grow1led_pwm
    entity_id: light.grow1_light
    internal: true
    
display:
  - platform: ssd1306_i2c
    i2c_id: bus_b
    model: "SSD1306 128x32"
    id: oled
    #reset_pin: D0
    address: 0x3D
    update_interval: 5s
    lambda: |-
      // Print time in HH:MM format
      //it.strftime(0, 6, id(font7), TextAlign::BASELINE_LEFT, "GROW1: %H:%M", id(esptime).now());
      it.printf(0, 6, id(font3),  TextAlign::BASELINE_LEFT, "LED: %s", id(grow1_led).state ? "ON" : "OFF");
      it.printf(0, 12, id(font3), TextAlign::BASELINE_LEFT, "PWM: %.1%%", id(sensor_g_bright).state);
      it.printf(0, 18, id(font3), TextAlign::BASELINE_LEFT, "UV:  %s", id(grow1_uv).state ? "ON" : "OFF");
      it.printf(0, 24, id(font3), TextAlign::BASELINE_LEFT, "IR:  %s", id(grow1_ir).state ? "ON" : "OFF");
      it.printf(0, 30, id(font3), TextAlign::BASELINE_LEFT, "VEN: %s", id(grow1_vent).state ? "ON" : "OFF");

      
      
      // Print outside temperature (from homeassistant sensor)
      if (id(grow1_temp).state == id(grow1_temp).state) {
        it.printf(127, 10, id(font1), TextAlign::BASELINE_RIGHT , "%.1f °C", id(grow1_temp).state);
      } else {
        it.print(127, 10, id(font3), TextAlign::BASELINE_RIGHT , "...waiting...");
      }
      
      // Print outside humidity (from homeassistant sensor)
      if (id(grow1_hum).state == id(grow1_hum).state) {
        it.printf(127, 23, id(font1), TextAlign::BASELINE_RIGHT , "%.0f %%", id(grow1_hum).state);
      } else {
        it.print(127, 23, id(font3), TextAlign::BASELINE_RIGHT , "...waiting...");
      }
      
      // Print outside pressure (from homeassistant sensor)
      if (id(wifi_sig).state == id(wifi_sig).state) {
        it.printf(127, 30, id(font3), TextAlign::BASELINE_RIGHT , "%.0f", id(wifi_sig).state);
      } else {
        it.print(127, 30, id(font3), TextAlign::BASELINE_RIGHT , "...waiting...");
      }

font:
  - id: font1
    file: "fonts/open-sans/OpenSans-Semibold.ttf"
    size: 14
    
  - file: 'fonts/slkscr.ttf'
    id: font3
    size: 8
    
  - file: 'fonts/slkscr.ttf'
    id: font7
    size: 10
    
  - file: "fonts/open-sans/OpenSans-Semibold.ttf"
    id: font2
    size: 16

  - file: 'fonts/Saira-Bold.ttf'
    id: font4
    size: 10


    
    
binary_sensor:
  - platform: status
    name: "ESP32 Status"    


switch:
  - platform: restart
    name: "${device_name} REBOOT"
    
  - platform: gpio
    inverted: true
    pin: 25
    name: "LED"
    id: grow1_led
    restore_mode: RESTORE_DEFAULT_OFF
    interlock_wait_time: 0ms
  - platform: gpio
    inverted: true
    pin: 26
    name: "UV"
    id: grow1_uv
    restore_mode: RESTORE_DEFAULT_OFF
    interlock_wait_time: 0ms
  - platform: gpio
    inverted: true
    pin: 27
    name: "IR"
    id: grow1_ir
    restore_mode: RESTORE_DEFAULT_OFF
    interlock_wait_time: 0ms
  - platform: gpio
    inverted: true
    pin: 14
    name: "Vent"
    id: grow1_vent
    restore_mode: RESTORE_DEFAULT_OFF
    interlock_wait_time: 0ms


# Example configuration entry
output:
  - platform: ledc
    pin: 19
    frequency: 5000
    id: ledc_pmw

# Example usage in a light
light:
  - platform: monochromatic
    output: ledc_pmw
    name: "Grow1 Light"
    id: light_main
    restore_mode: RESTORE_DEFAULT_OFF
1 Like

hello,
just figured it out…
you have to grap the value from light output and convert it…
than read it for the display … works like charm :slight_smile:

#Readings calculated from light output
  - platform: template
    name: "Grow1 LED PWM"
    id: "led1_pwm"
    update_interval: 10s
    accuracy_decimals: 0
    lambda: |-
      return (int(id(light_main).remote_values.get_brightness() * 255));

...
# light output pin 19 is PMW pin important is frequency: 5000
output:
  - platform: ledc
    pin: 19
    frequency: 5000
    id: ledc_pmw

# LED PMW light -> from here grab the id for reading in template sensors
light:
  - platform: monochromatic
    output: ledc_pmw
    name: "Grow1 Light"
    id: light_main
    restore_mode: RESTORE_DEFAULT_OFF

hope this help’s other wo struggle with this…
all the best
M.

4 Likes

Great job, and I am looking for the same thing, but how did you figure it out and do we have to use 'lambda" for that?

https://esphome.io/api/classesphome_1_1light_1_1_light_color_values.html#a0a534f360202c502f9c237d4228610a9