How can I print the state of a Binary Sensor?

I am reading a binary_sensor into Home Assistant

Installation method: Home Assistant OS
Core: 2025.5.3
Supervisor: 2025.05.3
Operating System: 15.2
Frontend: 20250516.0

via this code:

binary_sensor:
  - platform: gpio
    name: "Waste Valve"
    pin: 
      number: GPIO16
      mode:
        input: true
        pullup: true

Then, later down the path, displaying the value on an LCD display.

display:
  - platform: ili9xxx
    model: ili9341
    id: tft_display
    invert_colors: false

    cs_pin:    GPIO12 #TFT_CS (Pin 3)    
    reset_pin: GPIO35 #TFT_RST (Pin 4)
    dc_pin:    GPIO37 #TFT_D/C (Pin 5)

    rotation: 0
    lambda: |-
      it.printf(id(line8x), id(line8y), id(my_font), "Waste Valve");
      it.printf(it.get_width(), id(line8y), id(my_font), TextAlign::TOP_RIGHT, "%s", id(waste_valve) );

So…
What is the data type of a binary_sensor?
And what format specifier should I use in the it.printf() command?

Regards, Martin

Boolean, either true or false.

The syntax to print it is like this:
it.printf(0, 0, id(my_font), "State: %s", id(waste_valve).state ? "ON" : "OFF");

You need to give
id: waste_valve
for your binary sensor.

There are macros for that:

There is also a YESNO macro.

2 Likes

and TRUEFALSE I expect…
I wonder where you find these…

They should generally be automatically available in lambdas due to the inclusion of header files in the main.cpp file, but the source is here:

1 Like

Dho… But not OPENCLOSE?

So, I’m sticking with:
it.printf(it.get_width(), id(line8y), id(my_font), TextAlign::TOP_RIGHT, "%s", id(waste_valve).state ? "OPEN" : "CLOSED" );

Thanks for all the ideas, M.

1 Like