Help needed with it.print to lcd - SOLVED

Long story short, if a switch is “on” I want to to print “On” at row 1 column 18 on an LCD and if the switch is “off” I want to print “off” instead. Which sounds simple enough but I’m having hell’s own job formatting the lambda correctly.

Here is what I have…

.........................
switch:
  - platform: gpio
    pin: GPIO16
    name: "Booth Fan Relay"  
    inverted: true
    id: fan_relay
    
display:
  - platform: lcd_pcf8574
    id: mydisplay
    dimensions: 20x4
    address: 0x27
    lambda: |-
      it.print(0,0,"     Temp   RH  Fans" );
      it.printf(0,1,"Now: %.1fc %.1f%%", id(booth_temp).state, id(booth_rh).state);
      it.printf(0,2,"Min: %.1fc %.1f%%", id(min_temp).state, id(min_rh).state);    
      it.printf(0,3,"Max: %.1fc %.1f%%", id(max_temp).state, id(max_rh).state); 

Looking at “states” in Home assistant, Booth Fan Relay is either “On” or “Off” but I guess it’s either true or false. So essentially if the state of (id) fan_relay is “on” then I want to show “On” at row 1, column 18 and if it’s “off” then I want to show “Off” at that position. I’ve tried every which way I can think of but not being a programmer, I haven’t managed to stumble across the correct way to do this. Any help you can give a 69 year old non-writer of code, would be much appreciated.

Edit. column 17, not 18

Solved it! The bit of the jigsaw that I was missing is how to evaluate the state of an entity when it’s “on” or “off” and essentially, it’s much easier than everything else I tried. I just needed to use “if (id(fan_relay).state {” which evaluates to true of false - no need to compare the state with a value which is what I was trying to do.

So for anyone stumbling across this, I ended with this at the end of my lambda…

if (id(fan_relay).state){
        it.print(17,1,"On");
      } else {
        it.print(17,1, "Off");
      }
2 Likes