Display an icon on OLED screen depending on the status of a sensor light

Good morning

On a small DIY project, I put 2 relays, 2 door sensors, 1 Dallas DS18B20 probe and an OLED screen on an ESP32.

On the OLED screen, I currently display

  • an mdi:fan icon if the relay is ON
  • an mdi:fan-off icon if the relay is OFF

There I now want to retrieve the information from HA of a light.ecl_baie_led_haut to do a bit of the same thing, namely display:

  • an mdi:lightbulb-outline icon if the relay is ON
  • an mdi:lightbulb-off-outline icon if the relay is OFF

here is some of my code to do it:

sensor:
  - platform: homeassistant
    id: led
    entity_id: light.ecl_baie_led_haut 
    internal: true

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    update_interval: 0.2s
    lambda: |-
      # Works welln       
      if (id(ventillateur_1).state) {
        it.print(0, 0, id(font5), "\U000F0210");
      } else {
        it.print(0, 0, id(font5), "\U000F081D");
      }

      # Works well 
      if (id(ventillateur_2).state) {
        it.print(0, 32, id(font5), "\U000F0210");
      } else {
        it.print(0, 32, id(font5), "\U000F081D");
      }

      # doesn't work at all     
      if (id(led).state == true) {
        it.print(40, 0, id(font5), "\U000F0336");
      } else {
        it.print(40, 0, id(font5), "\U000F0E50");
      }

Error message in the logs:

[20:43:13][W][homeassistant.sensor:015]: 'light.ecl_baie_led_haut': Can't convert 'on' to number!
[20:43:13][D][sensor:094]: 'led': Sending state nan  with 1 decimals of accuracy
[20:43:26][W][homeassistant.sensor:015]: 'light.ecl_baie_led_haut': Can't convert 'off' to number!
[20:43:26][D][sensor:094]: 'led': Sending state nan  with 1 decimals of accuracy

Thank you for your help on the subject

Try it as a text sensor. As the docs say the sensor component is for numeric components.

So I created two templates to transform my light.ecl_baie_led_haut for example into binary_sensor

- platform: template
  sensors:
    led_baie_haut:
      friendly_name: "LED Baie Haut Binary Sensor"
      value_template: "{{ is_state('light.ecl_baie_led_haut', 'on') }}"

- platform: template
  sensors:
    led_baie_bas:
      friendly_name: "LED Baie Bas Binary Sensor"
      value_template: "{{ is_state('light.ecl_baie_led_bas', 'on') }}"

In esphome I put the binary_sensor like this

binary_sensor:
  - platform: homeassistant
    id: led_baie_bas
    entity_id: binary_sensor.led_baie_bas
  - platform: homeassistant
    id: led_baie_haut
    entity_id: binary_sensor.led_baie_haut

On the Oled display I put this

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    update_interval: 0.2s
    lambda: |-
      
      // Print image
      //it.image(0, 0, id(fan));
      //it.image(0, 32, id(fan));

      if (id(ventillateur_1).state) {
        it.print(0, 0, id(font5), "\U000F0210");
      } else {
        it.print(0, 0, id(font5), "\U000F081D");
      }

      if (id(ventillateur_2).state) {
        it.print(0, 32, id(font5), "\U000F0210");
      } else {
        it.print(0, 32, id(font5), "\U000F081D");
      }

      if (id(led_baie_haut).state) {
      it.print(40, 0, id(font5), "\U000F0336");
      } else {
          it.print(40, 0, id(font5), "\U000F0E50");
      }

      if (id(led_baie_bas).state) {
      it.print(40, 32, id(font5), "\U000F0336");
      } else {
          it.print(40, 32, id(font5), "\U000F0E50");
      }

and here is the result for

  • 1 relay ON
  • 1 relay OFF
  • 1 light ON
  • 1 light OFF

I think you just needed to change the esphome yaml to the above, although on my reading you don’t need the internal: true, as it is the default.

I’m going to test this solution to see if it works because it can simplify things by not making the template