Displaying an image using Lambda from a binary sensor

Stuck novice here. I would like to display an image on an esp32 with an lcd when a door is open. This is what I have: an ESP32 with reed switch → Home Assistant → 2nd ESP32 with an LCD. I can see the state for the reed switch change in Home Assistant (open or closed). I can also display the correct values of other sensors on the LCD screen coming from Home Assistant. I am just having problems with the binary sensor. Here is the relevant info for the binary sensor in Home Assistant.

Here is the relevant portion of the code for the ESP 32 with the LCD:

binary_sensor:
  - platform: homeassistant
    id: shed_door
    entity_id: binary_sensor.esphome_web_854c70_upper_shed_door
    internal: true

color:
  - id: my_black
    red: 100%
    green: 100%
    blue: 100%

spi:
  clk_pin: GPIO18
  mosi_pin: GPIO23

image:
  - file: "chicken.jpg"
    id: chicken
    resize: 200x200
    type: RGB24

  - file: "barn.jpg"
    id: barn
    resize: 200x200
    type: RGB24

display:
  - platform: ili9xxx
    model: ST7789V
    dc_pin: GPIO2
    reset_pin: GPIO4
    cs_pin: GPIO15
    color_order: rgb
    transform:
      swap_xy: true
      mirror_y: true
    lambda: |-
      it.fill(my_black);
      if(id(shed_door).state) {
        it.image(130, 80, id(barn));
      } else {
        it.image(40, 80, id(chicken))
      }

Right now I only get the test chicken picture. The picture doesn’t change if the door is opened or closed. I can get the picture to change if I include id(shed_door).publish_state(true); before the if statement. I know what I am trying to do is similar to the 3rd post in the following thread, but I cannot find my mistake. Thanks.

SOLVED: Trying to display the state of a switch in ESPHOME display - ESPHome - Home Assistant Community (home-assistant.io)

Positions are not the same. Off screen?

I can see both images. Eventually I planned to use a different image for the shed door and a chicken coop door (not in the current code) and I have them in different places so that I can see if both doors are open/closed.

Just to be clear

I can test to see if I can see the other picture if I change the code to

    lambda: |-
      it.fill(my_black);
      id(shed_door).publish_state(true);
      if(id(shed_door).state) {
        it.image(130, 80, id(barn));
      } else {
        it.image(40, 80, id(chicken));
      }

And I see the picture change.

I think I have something wrong with the logic from the binary sensor from home assistant.

I found a note in my code to import binary sensors under text_sensor:

text_sensor:
  - platform: homeassistant
    name: "motion_basement"
    id: motion_basement
    entity_id: binary_sensor.motion_basement
    on_value:
      then:

Evaluation in the display:

          if (id(motion_basement).state == "on") {
          it.printf(5, 65, id(helvetica_12), id(my_green), TextAlign::TOP_LEFT, "Motion basement");
          }

It works for me.

Thanks for the suggestion. It didn’t work, but It got me thinking about other possibilities. I have discover the ESP32 is going into safe mode. Once I try a few other fixes I will post an update.

I now have it working as I would expect. However, I believe I might have a defective ESP32, because it is very hard to upload a sketch. Let me explain in case it helps someone or if I can get an opinion that yes this is a bad part.

There were three things I should have given more thought to when the different code approaches weren’t working.

  1. I couldn’t visit the device from the ESPHome page.
  2. The device would blink when booting and would occasionally do so every once in a while.
  3. Pictures would only display when called in the else loop.

Once I bothered to hook it up to a serial monitor I realized it was failing to boot and entering into safe mode and the above three made sense:

  1. Limited wifi connectivity
  2. The blinking was the 10 boot attempts before entering safe mode and a timeout of 300 seconds before the next attemps.
  3. There was no connection to Home assistant so the binary or text sensor had no state so it entered the else loop.

On to the failing to boot and the serial monitor

The relevant portions are

e[0;36m[D][esp32_ble:279]: Enabling BLE...e[0m
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

and

I was getting a

E (462) psram: PSRAM ID read error: 0xffffffff

as well. I still get that error. There is some discussion of this error on github:
E (243) psram: PSRAM ID read error: 0xffffffff E (243) cpu_start: Failed to init external RAM! (IDFGH-4457) · Issue #6288 · espressif/esp-idf (github.com)

I tried a few things from there i.e setting this

psram:
  mode: quad

didn’t work. But finally I just tried disabling BLE since that was were it was crashing. That caused it to stop entering into safe mode.

esp32_ble:
  enable_on_boot: false

I thought maybe I was just running out of RAM. However, it now is a chore to load new code. 9/10 times the code will fail to load. Was there something to do with safemode that made it easier to load code with flakey PSRAM?

Here is a representative sample of when it fails to load:

e[0;36m[D][esphome.ota:293]: Progress: 27.3%e[0m
e[0;36m[D][esphome.ota:293]: Progress: 29.3%e[0m
e[0;36m[D][esphome.ota:293]: Progress: 31.5%e[0m
E (54050) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (54050) task_wdt:  - loopTask (CPU 1)
E (54050) task_wdt: Tasks currently running:
E (54050) task_wdt: CPU 0: IDLE
E (54050) task_wdt: CPU 1: IDLE
E (54050) task_wdt: Aborting.

abort() was called at PC 0x400fd84c on core 0


Backtrace:0x400839c1:0x3ffbe9fc |<-CORRUPTED




ELF file SHA256: 0000000000000000

Rebooting...
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode: DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13132
load:0x40080400,len:3036
entry 0x400805e4
E (462) psram: PSRAM ID read error: 0xffffffff
e[0;32m[I][logger:156]: Log initializede[0m
entry 0x400805e4
E (462) psram: PSRAM ID read error: 0xffffffff
e[0;32m[I][logger:156]: Log initializede[0m

It is working exactly as I would expect right now, but I was going to use this device to track several doors, so I will still update the code and don’t want to have to fight loading the code to debug. Is it defective or am I missing something else? I should mention this is a ESP-wroom-32 from ideaspark with the 1.14 inch screen using the ST7789 driver.

Thanks.

PS the final code:

text_sensor:
  - platform: homeassistant
    id: shed_door
    entity_id: binary_sensor.esphome_web_854c70_upper_shed_door
    internal: true
    on_value:
      then:

color:
  - id: my_black
    red: 100%
    green: 100%
    blue: 100%

  - id: my_red
    red: 10%
    green: 100%
    blue: 10%

spi:
  clk_pin: GPIO18
  mosi_pin: GPIO23

image:
  - file: "chicken.jpg"
    id: chicken
    resize: 200x200
    type: RGB24

  - file: "barn.jpg"
    id: barn
    resize: 200x200
    type: RGB24


font:
  - file: 'silkscreen.ttf'
    id: font1
    size: 8

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

display:
  - platform: ili9xxx
    model: ST7789V
    dc_pin: GPIO2
    reset_pin: GPIO4
    cs_pin: GPIO15
    color_order: rgb
    dimensions:
      height: 250
      width: 200
      #offset_width: 40
      offset_height: 40
    #transform:
    #  swap_xy: true
    #  mirror_y: true
    rotation: 270°

    lambda: |-
      it.fill(my_black);
      if (id(shed_door).has_state()) {
        if(id(shed_door).state == "on") {
          it.image(130, 70, id(barn));
        }  
        if(id(shed_door).state == "off") {
          it.printf(130, 70, id(font1), id(my_red), "shed closed");
        }
      }      
      else {
        it.printf(130, 70, id(font2), id(my_red), "ERROR");
      }