Approach if statement

I have succesfully created ESP device with a BME280 sensor and a SSD1306 display.
Connected on a SonOff basic module.
On the display i have all sensor information I need to readout locally.

This works great so far.

Only thing I want to do is show the state of the Relay on my display.

But I simply cant find the right approach how to get a usable variable/state to work with to show the state on the display.

In my project there is a push button that turns the relay on or off.
So reading out the button I think is not the right approach. It can be true or false, but doesnt say anything about the state of the relay.

Only good way to make sure if the relay is realy on or off is to read out the state of the relay itself…
But How do I read out it’s state? the button is just toggling the relay on or off.

esphome:
  name: dryer_1
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "SSID"
  password: "PASSWORD"

  # Optional manual IP
  manual_ip:
    static_ip: 192.168.1.80
    gateway: 192.168.1.1
    subnet: 255.255.255.0
  
#  use_address: 192.168.1.xxx

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Dryer1 Fallback Hotspot"
    password: "mf39hVlmsrj4"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

font:
  - file: 'fonts/boulder.ttf'
    id: font1
    size: 22

  - file: 'fonts/arlrbdb.ttf'
    id: font2
    size: 16
    
  - file: 'fonts/arial.ttf'
    id: font3
    size: 14  

i2c:
  sda: 3
  scl: 1
  scan: False

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "FilaDry_1_Button"
    on_press:
      - switch.toggle: relay

switch:
  - platform: gpio
    name: "FilaDry_1_Relay"
    pin: GPIO12
    id: relay

status_led:
  pin:
    number: GPIO13
    inverted: yes
    
    
sensor:
  - platform: bme280
    temperature:
      name: "FilaDry_1_Temperature"
      oversampling: 16x
      id: temp
    pressure:
      name: "FilaDry_1_Pressure"
    humidity:
      name: "FilaDry_1_Humidity"
      id: hum
    address: 0x76
    update_interval: 5s

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    reset_pin: 0
    address: 0x3C
    rotation: 0
    lambda: |-

      it.printf(0, 0, id(font1), TextAlign::TOP_LEFT, "FilaDry 1");

      it.printf(0, 40, id(font3), TextAlign::BASELINE_LEFT, "Temp:");
      it.printf(0, 60, id(font3), TextAlign::BASELINE_LEFT, "Humidity:");

      if (id(hum).has_state()) {
        it.printf(127, 42, id(font2), TextAlign::BASELINE_RIGHT , "%.1f°C", id(temp).state);
        it.printf(127, 62, id(font2), TextAlign::BASELINE_RIGHT , "%.0f%%", id(hum).state);
      }

Fixed it right after posting the question :slight_smile:

      if (id(relay).state) {
        it.printf(128, 0, id(font2), TextAlign::TOP_RIGHT, "ON");
      } else {
        it.printf(128, 0, id(font2), TextAlign::TOP_RIGHT, "OFF");
      }
      
1 Like