ESPHome binary_sensor: display:

Hello,
I still have some problem to understand the ESPHome language:
I have the following code :

# Les switchs et boutons
binary_sensor:
  - platform: gpio
    pin:
      number: D3
      mode: INPUT_PULLUP
    name: "door_state"
    id: door_state
    on_state:
      - if:
          condition: binary_sensor.is_on
          then:
            - lambda: id(door_state).text_state("Door is closed")
          else:
            - lambda: |-
                id(door_state).text_state("Door is open")

# L'affichage sur le LCD2004
display:
  - platform: lcd_pcf8574
    dimensions: 20x4
    address: 0x27
    id: my_display
    update_interval: 1s
    lambda: |-    
      it.print("Page 1");
      it.printf(0, 1, id(door_state).text_state.c_str());
      it.strftime(0, 2, "%H:%M:%S", id(my_time).now())

Why do I get the following error message:

INFO ESPHome 2023.6.3
INFO Reading configuration /config/esphome/garagetoor.yaml...
INFO Detected timezone 'Europe/Zurich'
Failed config

binary_sensor.gpio: [source /config/esphome/garagetoor.yaml:45]
  platform: gpio
  pin: 
    number: D3
    mode: INPUT_PULLUP
  name: door_state
  id: door_state
  on_state: 
    - if: 
        
        'id' is a required option for [binary_sensor.is_on].
        condition: binary_sensor.is_on
        then: 
          - lambda: id(door_state).text_state("Door is closed")
        else: 
          - lambda: id(door_state).text_state("Door is open")

Which binary sensor is on?

You have not supplied an id.

If you mean the door sensor then use the on_press and on_release triggers instead of the on_state trigger: Binary Sensor Component — ESPHome

binary_sensor:
  - platform: gpio
    pin:
      number: D3
      mode: INPUT_PULLUP
    name: "door_state"
    id: door_state
    on_press:
          then:
            - lambda: id(door_state).text_state("Door is closed")
    on_release:
          then:
            - lambda: |-
                id(door_state).text_state("Door is open")

What do you think text_state does?

A binary sensor is on or off. If you want to display text depending on the state, build that logic into your display section.

1 Like

I just deleted that part of my reply about the action, as I was not sure if it was an attribute that could be used. I don’t do displays on my esp devices. Thanks for confirming.

1 Like

Instead of this delete all your on_state code from the sensor, and use:

      it.printf(0, 1, "Door is %s", id(door_state) ? "open" : "closed");

Thanks for your help. It’s working better already. But I’m having a bit of trouble understanding how a yaml program works with ESPHome. I’ll have to reread some examples.