Esphome gpio.switch status on OLED display

Hello Everyone,

I would like to ask some help from you because I cant make a valid config for this use case.

I need to print every gpio switch status on my oled screen, but every type of get this class I dont have success.

This is my conf:

switch:
  - platform: gpio
    pin: GPIO32
    inverted: yes
    restore_mode: RESTORE_DEFAULT_OFF
    name: "CH-1"
    id: ch_1

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    id: my_display
    address: 0x3C
    update_interval: 2s
    pages:
      - id: page1
        lambda: |-
          it.printf(10, 10, id(roboto), "CH1 status:", id(ch_1).state ? "on" : "off");

Thanks in advance for any kind of help!

BR
megnetx

Did you see/try this from the examples?

binary_sensor:
  - platform: ...
    # ...
    id: my_binary_sensor

display:
  - platform: ...
    # ...
    lambda: |-
      if (id(my_binary_sensor).state) {
        it.print(0, 0, id(my_font), "state: ON");
      } else {
        it.print(0, 0, id(my_font), "state: OFF");
      }
      // Shorthand:
      it.printf(0, 0, id(my_font), "State: %s", id(my_binary_sensor).state ? "ON" : "OFF");

It’s work~ish but wont update the status, so when start the esp the display shows State: OFF and dont refresh when I chage the status.

@Mahko_Mahko

Thanks for your help, if you have some time for me please help me a little… How can make a trigger or automation or update interval for this binary sensor to reach the status change because this boolean never be change after creation.

Thank you so much

BR
Megyo

Are you able to display and refresh something else simple?

Like uptime?

Try it.

binary_sensor:
  - platform: .....
    on_state:
      - component.update: display_lcd

display:
  - platform: .....
    id: display_lcd

@Mahko_Mahko, @Miki_Spetik

I tried the uptime sensor and the on_state as well.
The uptime sensor dont shown on the screen but every other thing are work perfectly.
The on_state dont work neither when configured, the switch doesnt react to the switching, but the display shown the state ON :smiley:

I insert my config maybe you see some big rookie mistake

esphome:
    name: 8ch-din-relay
  
  esp32:
    board: esp32dev
    framework:
      type: arduino
  
  # Enable logging
  logger:
  
  # Enable Home Assistant API
  api:
    encryption:
      key: "abcd1234"
  
  ota:
    password: "abcd1234"
  
  wifi:
    ssid: !secret wifi_ssid
    password: !secret wifi_password
  
    # Enable fallback hotspot (captive portal) in case wifi connection fails
    ap:
      ssid: "fallbackssid"
      password: "abcd1234"
  
  captive_portal:
  
  binary_sensor:
    - platform: gpio
      pin: GPIO32
      id: ch1_st
      on_state:
         - component.update: my_display
    - platform: gpio
      pin: GPIO33
      id: ch2_st
    - platform: gpio
      pin: GPIO25
      id: ch3_st
    - platform: gpio
      pin: GPIO26
      id: ch4_st
    - platform: gpio
      pin: GPIO27
      id: ch5_st
    - platform: gpio
      pin: GPIO14
      id: ch6_st
    - platform: gpio
      pin: GPIO13
      id: ch7_st
    - platform: gpio
      pin: GPIO22
      id: ch8_st
  
  switch:
    - platform: gpio
      pin: GPIO32
      inverted: yes
      restore_mode: RESTORE_DEFAULT_OFF
      name: "CH-1"
      id: ch_1
    - platform: gpio
      pin: GPIO33
      inverted: yes
      restore_mode: RESTORE_DEFAULT_OFF
      name: "CH-2"
      id: ch_2
    - platform: gpio
      pin: GPIO25
      inverted: yes
      restore_mode: RESTORE_DEFAULT_OFF
      name: "CH-3"    
      id: ch_3
    - platform: gpio
      pin: GPIO26
      inverted: yes
      restore_mode: RESTORE_DEFAULT_OFF
      name: "CH-4"
      id: ch_4
    - platform: gpio
      pin: GPIO27
      inverted: yes
      restore_mode: RESTORE_DEFAULT_OFF
      name: "CH-5"
      id: ch_5
    - platform: gpio
      pin: GPIO14
      inverted: yes
      restore_mode: RESTORE_DEFAULT_OFF
      name: "CH-6"
      id: ch_6
    - platform: gpio
      pin: GPIO13
      inverted: yes
      restore_mode: RESTORE_DEFAULT_OFF
      name: "CH-7"    
      id: ch_7
    - platform: gpio
      pin: GPIO22
      inverted: yes
      restore_mode: RESTORE_DEFAULT_OFF
      name: "CH-8"
      id: ch_8    
  
  sensor:
    - platform: uptime
      name: Uptime Sensor
      id: uptimes  
  
  i2c:
    sda: GPIO4
    scl: GPIO5
  
  display:
    - platform: ssd1306_i2c
      model: "SSD1306 128x64"
      id: my_display
      address: 0x3C
      update_interval: 1s
      pages:
        - id: page1
          lambda: |-
            it.rectangle(0, 0, 128, 64);
            it.printf(30, 0, id(roboto), "Relay status:");
            it.line(0, 15, 128, 15);
            it.line(0, 28, 128, 28);
            it.line(0, 40, 128, 40);
            it.line(0, 52, 128, 52);
            it.line(64, 15, 64, 64);
            it.printf(3, 16, id(roboto), "CH1:%s", id(ch1_st).state ? "ON" : "OFF");
            it.printf(3, 28, id(roboto), "CH2:%s", id(ch2_st).state ? "ON" : "OFF");
            it.printf(3, 40, id(roboto), "CH3:%s", id(ch3_st).state ? "ON" : "OFF");
            it.printf(3, 51, id(roboto), "CH4:%s", id(ch4_st).state ? "ON" : "OFF");
            it.printf(66, 16, id(roboto), "CH5:%s", id(ch5_st).state ? "ON" : "OFF");
            it.printf(66, 28, id(roboto), "CH6:%s", id(ch6_st).state ? "ON" : "OFF");
            it.printf(66, 40, id(roboto), "CH7:%s", id(ch7_st).state ? "ON" : "OFF");
            it.printf(66, 51, id(roboto), "CH8:%s", id(ch8_st).state ? "ON" : "OFF");
        - id: page2
          lambda: |-
            it.printf(2, 0, id(roboto),"Controller info:");
            it.printf(2, 10, id(roboto), "Uptime: ", id(uptimes));
  
        - id: page3
          lambda: |-
            it.printf(2, 15, id(roboto), "Dashboard");
            it.printf(20, 25, id(roboto), "Link");
            it.qr_code(65, 5, id(dashboard), Color(255,255,255), 2);
  
  interval:
    - interval: 10s
      then:
        - display.page.show_next: my_display
        - component.update: my_display      
  
  font:
    - file: "gfonts://Roboto"
      id: roboto
      size: 11      
  
  web_server:
    js_include: "./v2/www.js"
    js_url: ""
    version: 2          
  
  qr_code:
    - id: dashboard
      value: "something_not_interest"

Thanks for the help!

BR
megnetx

  binary_sensor:
    - platform: gpio
      pin: GPIO32
      id: ch1_st
      on_press:
        - lambda: |-
            if(id(ch_1).state) id(ch_1).turn_off();
            else id(ch_1).turn_on();
        - component.update: my_display

or:

  binary_sensor:
    - platform: gpio
      pin: GPIO32
      id: ch1_st
      on_press:
        - switch.toggle: ch_1
        - component.update: my_display

and of course edit

it.printf(3, 16, id(roboto), "CH1:%s", id(ch_1).state ? "ON" : "OFF");

Sorry for three quick edits.

And uptime missing %i

it.printf(2, 10, id(roboto), "Uptime:  %i", id(uptimes));

@Miki_Spetik

I tried both of your option and the displayed status change is ok now, but the GPIO pin dont make change that is, the relay doesn’t switch.

I can’t imagine why the binary sensor affects the switching in this way.

Thanks you for your support and don’t worry about how many times you edit the post :smiley:

P.S.: The uptime sensor is OK right now. Thanks.

BR
megnetx

Now I tried esp32 and relay and lcd - ok.
The button changes the relay status and the LCD shows the relay status.
Is there a fault in the wiring?

Ooooh. You have the same pins for binary_sensor and switch ! This is wrong. GPIO always use one pin.
:slight_smile:

Sorry but i dont catch this :smile:
The binary sensor represent the relay outputs gpio’s and the gpio config just plachold to one non used pin?

GPIO binary_sensor is input (button), GPIO switch is output (relay). You can’t have both on one pin.
If you don’t have a physical button, try it.
Delete binary_sensor and:

switch:
  - platform: gpio
    pin: GPIO32
    inverted: yes
    restore_mode: RESTORE_DEFAULT_OFF
    name: Relay 01
    id: relay_01
    on_turn_on:
    - logger.log: "Switch Turned On!"
    - component.update: display_lcd
    on_turn_off:
    - logger.log: "Switch Turned Off!"
    - component.update: display_lcd

display:
    - platform: .....
      pages:
        - id: page1
          lambda: |-
            //State gpio output
            it.printf(3, 16, id(roboto), "Relay 01:%s", id(relay_01).state ? "ON" : "OFF");      

Oh ok I see.
I’ll try in the morning and get back to you!

I think now understand, many thanks for the help and the teaching!

1 Like

Hello @Miki_Spetik

I tried like this:

switch:

  - platform: gpio

    pin: GPIO32

    inverted: yes

    restore_mode: RESTORE_DEFAULT_OFF

    name: "CH-1"

    id: ch_1

    on_turn_on:

      - component.update: my_display

    on_turn_off:

      - component.update: my_display

...........

it.printf(3, 16, id(roboto), "CH1:%s", id(ch_1).state ? "ON" : "OFF");

And get from esp this:

[I][app:029]: Running through setup()...
[I][i2c.arduino:175]: Performing I2C bus recovery
[C][switch.gpio:011]: Setting up GPIO Switch 'CH-1'...
[D][switch:017]: 'CH-1' Turning OFF.
[D][switch:056]: 'CH-1': Sending state OFF
Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x400dbe83  PS      : 0x00060c30  A0      : 0x800dbe9b  A1      : 0x3ffb2430  
A2      : 0x3ffb336c  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x3f40293d  
A6      : 0x3ffb2a00  A7      : 0x3f402927  A8      : 0x00000000  A9      : 0x3ffb2410  
A10     : 0x00000400  A11     : 0x00000080  A12     : 0x00000038  A13     : 0x3f40293d  
A14     : 0x00000001  A15     : 0x00000000  SAR     : 0x00000004  EXCCAUSE: 0x0000001d  
EXCVADDR: 0x00000000  LBEG    : 0x40089e69  LEND    : 0x40089e79  LCOUNT  : 0xfffffff2  


Backtrace:0x400dbe80:0x3ffb24300x400dbe98:0x3ffb2450 0x400d80fe:0x3ffb2470 0x400d86be:0x3ffb2490 0x400dbdaa:0x3ffb24b0 0x400e2e09:0x3ffb24d0 0x4017647f:0x3ffb24f0 0x401764ad:0x3ffb2510 0x400e302f:0x3ffb2530 0x400dc735:0x3ffb2550 0x400d9271:0x3ffb2580 0x400dc5bb:0x3ffb25d0 0x400d90c4:0x3ffb25f0 0x400d90dc:0x3ffb2620 0x40176209:0x3ffb2640 0x401762b9:0x3ffb2660 0x400e1b68:0x3ffb2680 0x400e48c2:0x3ffb26b0 0x400f2e36:0x3ffb2820 




ELF file SHA256: 0000000000000000

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x17 (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

What have I done wrong?

Can you show the full yaml?

of course:

esphome:
  name: 8ch-din-relay

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "abcd1234"

ota:
  password: "abcd1234"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "8Ch-Din-Relay"
    password: "abcd1234"

captive_portal:


switch:
  - platform: gpio
    pin: GPIO32
    inverted: yes
    restore_mode: RESTORE_DEFAULT_OFF
    name: "CH-1"
    id: ch_1
    on_turn_on:
      - component.update: my_display
    on_turn_off:
      - component.update: my_display
  - platform: gpio
    pin: GPIO33
    inverted: yes
    restore_mode: RESTORE_DEFAULT_OFF
    name: "CH-2"
    id: ch_2
    on_turn_on:
      - component.update: my_display
    on_turn_off:
      - component.update: my_display    
  - platform: gpio
    pin: GPIO25
    inverted: yes
    restore_mode: RESTORE_DEFAULT_OFF
    name: "CH-3"    
    id: ch_3
    on_turn_on:
      - component.update: my_display
    on_turn_off:
      - component.update: my_display    
  - platform: gpio
    pin: GPIO26
    inverted: yes
    restore_mode: RESTORE_DEFAULT_OFF
    name: "CH-4"
    id: ch_4
    on_turn_on:
      - component.update: my_display
    on_turn_off:
      - component.update: my_display    
  - platform: gpio
    pin: GPIO27
    inverted: yes
    restore_mode: RESTORE_DEFAULT_OFF
    name: "CH-5"
    id: ch_5
    on_turn_on:
      - component.update: my_display
    on_turn_off:
      - component.update: my_display    
  - platform: gpio
    pin: GPIO14
    inverted: yes
    restore_mode: RESTORE_DEFAULT_OFF
    name: "CH-6"
    id: ch_6
    on_turn_on:
      - component.update: my_display
    on_turn_off:
      - component.update: my_display    
  - platform: gpio
    pin: GPIO13
    inverted: yes
    restore_mode: RESTORE_DEFAULT_OFF
    name: "CH-7"    
    id: ch_7
    on_turn_on:
      - component.update: my_display
    on_turn_off:
      - component.update: my_display    
  - platform: gpio
    pin: GPIO22
    inverted: yes
    restore_mode: RESTORE_DEFAULT_OFF
    name: "CH-8"
    id: ch_8    
    on_turn_on:
      - component.update: my_display
    on_turn_off:
      - component.update: my_display    

# text_sensor:
#   - platform: wifi_info
#     ip_address:
#       id: ip_addr
#     ssid:
#       id: ssid
#     bssid:
#       id: bssid
#     mac_address:
#       id: mac_address
#     scan_results:
#       id: scan_res

sensor:
  - platform: uptime
    name: Uptime Sensor
    id: uptimes  
#   - platform: wifi_signal
#     id: signyal
#     update_interval: 5s      

i2c:
  sda: GPIO4
  scl: GPIO5

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    id: my_display
    address: 0x3C
    update_interval: 1s
    pages:
      - id: page1
        lambda: |-
          it.rectangle(0, 0, 128, 64);
          it.printf(30, 0, id(roboto), "Relay status:");
          it.line(0, 15, 128, 15);
          it.line(0, 28, 128, 28);
          it.line(0, 40, 128, 40);
          it.line(0, 52, 128, 52);
          it.line(64, 15, 64, 64);
          it.printf(3, 16, id(roboto), "CH1:%s", id(ch_1).state ? "ON" : "OFF");
          it.printf(3, 28, id(roboto), "CH2:%s", id(ch_2).state ? "ON" : "OFF");
          it.printf(3, 40, id(roboto), "CH3:%s", id(ch_3).state ? "ON" : "OFF");
          it.printf(3, 51, id(roboto), "CH4:%s", id(ch_4).state ? "ON" : "OFF");
          it.printf(66, 16, id(roboto), "CH5:%s", id(ch_5).state ? "ON" : "OFF");
          it.printf(66, 28, id(roboto), "CH6:%s", id(ch_6).state ? "ON" : "OFF");
          it.printf(66, 40, id(roboto), "CH7:%s", id(ch_7).state ? "ON" : "OFF");
          it.printf(66, 51, id(roboto), "CH8:%s", id(ch_8).state ? "ON" : "OFF");
      - id: page2
        lambda: |-
          it.printf(2, 0, id(roboto),"Controller info:");
          it.printf(2, 10, id(roboto), "Uptime: %i", id(uptimes));

      - id: page3
        lambda: |-
          it.printf(2, 15, id(roboto), "Dashboard");
          it.printf(20, 25, id(roboto), "Link");
          it.qr_code(65, 5, id(dashboard), Color(255,255,255), 2);

interval:
  - interval: 10s
    then:
      - display.page.show_next: my_display
      - component.update: my_display      

font:
  - file: "gfonts://Roboto"
    id: roboto
    size: 11      

web_server:
  js_include: "./v2/www.js"
  js_url: ""
  version: 2          

qr_code:
  - id: dashboard
    value: "something_not_interest"


      # - id: page2
      #   lambda: |-
          # it.printf(2, 10, id(roboto), id(ip_addr->toString()));
          # it.printf(2, 20, id(roboto), id(ssid->toString()));
          # it.printf(2, 30, id(roboto), id(mac_address->toString()));
          # it.printf(2, 40, id(roboto), "Signal DB", id(signyal->toString()));


   

The Wifi sensors are commented because the value displaying faulty as well

Remove all

on_turn_on:
      - component.update: my_display
    on_turn_off:
      - component.update: my_display

Probably many request component.update.
I will try to analyze later.

Hi @Miki_Spetik

When I deleted the mentioned lines the code start to work, and the status change on the display as well!
Thank you very much for your help and @Mahko_Mahko thank you too!

I have only one question or topic in this use case.
I would like to display all wifi sensor as well like:
Used SSID:
MAC:
IP:
Signal strenght:

But with this config doesnt ok:

# text_sensor:
#   - platform: wifi_info
#     ip_address:
#       id: ip_addr
#     ssid:
#       id: ssid
#     bssid:
#       id: bssid
#     mac_address:
#       id: mac_address
#     scan_results:
#       id: scan_res

# sensor:
#   - platform: wifi_signal
#     id: signyal
#     update_interval: 5s    

# display:
.
.
      # - id: page2
      #   lambda: |-
          # it.printf(2, 10, id(roboto), id(ip_addr->toString()));
          # it.printf(2, 20, id(roboto), id(ssid->toString()));
          # it.printf(2, 30, id(roboto), id(mac_address->toString()));
          # it.printf(2, 40, id(roboto), "Signal DB", id(signyal->toString()));

Thats why I commented the lines. What I’m doing wrong what is the good approach.

BR
megnetx