Cant figure how to shut off OLED say from a button press

so i trying to 2 things… as a Temperature sensor…
1… id like to have mini D1 with the temperature sensor, and a 2nd one with a screen that reads that mini D1… i have been able to connect the 2 together using the ap: wifi option so that part works… but thats as far as i have gotten…

2… is i working on the orginal setup i have a OLED screen it displays the temperature… but i been trying to incorporate a Button… right now i dont have one… but i wanted to use a gpio in its high or low state at boot jsut to simulate a button is pressed

i want to use those touch pads or a mometary switch… i dont have either at this moment will in a day or so but i wanted to get the code working

and id like to do a timer say after 10min it shuts screen off…

here is my messy code i been trying google with the esp home to try to find what would work
at this moment i dont have a mini d1 i using a 12e board at the moment
you will see about a deck… i was jst copying code from someone else to try to work it in my code

esphome:
  name: car-temp
  platform: ESP8266
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "cdcbe64916c6e40cc4b331f94096eb68"

wifi:
 # ssid: "test"
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Car-Temp Fallback Hotspot"
    password: "zXXAgyYrjW9h"

captive_portal:


sensor:
  - platform: dht
    model: am2302
    pin: GPIO4
    temperature:
      name: "Car Outside Temperature"
      id: outside_temperature
#      filters:
#        offset: 2
    humidity:
      name: "Car Outside Humidity"
    update_interval: 6s



switch:
  - platform: gpio  # or any other platform
    pin: D2
    name: Touch_Switch
    # ...
    on_turn_on:
    - logger.log: "Switch Turned On!"
    - lambda: id(oled).turn_off();
    on_turn_off:
    - logger.log: "Switch Turned Off!"
    - lambda: id(oled).turn_on();
    


  # deck lights off
  - platform: gpio
    pin: GPIO1
    id: decklightsoff

  - platform: template
    name: "Deck Lights OFF"
    lambda: 'return false;'
    turn_on_action:
      - switch.turn_on: decklightsoff
      - delay: 10ms
      - switch.turn_off: decklightsoff  
      - lambda: id(oled).turn_off();

binary_sensor:
  - platform: gpio
    name: "Button"
    internal: true
    pin:
      number: D6
      mode: INPUT_PULLUP
    on_press:
      - lambda: id(oled).turn_off();
#      - delay: 5s
#      - lambda: id(oled).turn_on();    
    on_state:
      then:
        - lambda: id(oled).turn_off();
    
    
    
# Example configuration entry
i2c:
  sda: D3
  scl: D4

font:
  - file: "fonts/nasalization.ttf"
    id: font1
    size: 23
  - file: "fonts/refsan.ttf"
    id: font2
    size: 35    
    
display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    id: oled
    reset_pin: D0
    address: 0x3C
    lambda: |-
        it.printf(0, 0, id(font1), "Outside");
        it.printf(0, 22, id(font2), TextAlign::TOP_LEFT , "%.1f°c", id(outside_temperature).state);
      #  it.printf(0, 8, id(font1), id(office_temperature).state);
        
#     // Print inside temperature (from homeassistant sensor)
#      if (id(office_temperature).state == id(office_temperature).state) {
#        it.printf(0, 8, id(font1), TextAlign::TOP_RIGHT , "%.1f°C", id(office_temperature).state);
#      } else {
#        it.print(0, 8, id(font1), TextAlign::TOP_RIGHT , "waiting");
#      }
#// Within lambda, get the switch state and conditionally do something
#         if (id(my_switch).state) {
#  // Switch is ON, do something here
#         } else {
#           // Switch is OFF, do something else here
#         };
1 Like

so i got me a touch sensor… but i having an awful time getting it to work right i stuck

here is my code…

esphome:
  name: car-temp
  platform: ESP8266
  board: nodemcuv2
  
  on_boot:
    priority: -100
    then:
      - switch.turn_on: Touch_Switch
      - lambda: id(oled).turn_on();  

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "cdcbe64916c6e40cc4b331f94096eb68"

wifi:
 # ssid: "test"
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Car-Temp Fallback Hotspot"
    password: "zXXAgyYrjW9h"

captive_portal:


# Turn off Flashing Blue Led
#status_led:
#  pin:
#    number: GPIO0
#    inverted: true


# Temperature Settings    
sensor:
  - platform: dht
    model: am2302
    pin: GPIO4
    temperature:
      name: "Car Outside Temperature"
      id: outside_temperature
#      filters:
#        offset: 2
    humidity:
      name: "Car Outside Humidity"
    update_interval: 6s

# Physical Touch Switch

  - platform: adc
    pin: A0
    name: "Touch Switch ADC"
    id: touch_switch_adc
 #   update_interval: 100ms
    filters:
    - multiply: 100
 
switch:
#  - platform: template
#    name: "Touch Switch"
#    id: touch_switch
#    optimistic: true
#    turn_on_action:
#      - lambda: id(oled).turn_on();
#    turn_off_action:
#      - lambda: id(oled).turn_off();

  - platform: template
##    pin: 2
    name: Touch_Switch
    id: Touch_Switch
    optimistic: true

#    # ...
    turn_on_action:
#    - logger.log: "Switch Turned On!"
    - lambda: id(oled).turn_on();
    - delay: 5s

    turn_off_action:
#    - logger.log: "Switch Turned Off!"
    - lambda: id(oled).turn_off();
    - delay: 5s

 
globals:
 - id: t_sw_bool
   type: bool
 
binary_sensor:
  - platform: template
    id: touch_switch_pressed
    name: "touch switch pressed"
    on_press:
      - switch.toggle: Touch_Switch
    #  - delay: 10s
    filters:
      - delayed_on: 5s
    lambda: |-
      if (id(touch_switch_adc).state > 1) {
        if (id(t_sw_bool) == false){
          id(Touch_Switch).toggle();
                 }
        id(t_sw_bool) = true;
        //  id(Touch_Switch).toggle();
        return true;
      } else {
        id(t_sw_bool) = false;
        // id(Touch_Switch).toggle();
        return false;
      }



#Virtual Touch Button  inside Home Assistant To activate screen
#switch:
#  - platform: gpio  # or any other platform
#  - platform: template
##    pin: 2
#    name: Touch_Switch
#    id: Touch_Switch
##    optimistic: true
#    # ...
#    turn_on_action:
#    - logger.log: "Switch Turned On!"
#    - lambda: id(oled).turn_on();
#    turn_off_action:
#    - logger.log: "Switch Turned Off!"
#    - lambda: id(oled).turn_off();





# Physical Button  Toggle The Screen 
#binary_sensor:
#  - platform: gpio
#    name: "Button"
#    internal: true
#    pin:
#      number: A0
#      mode: INPUT_PULLUP
#    on_press:
#    - switch.toggle: Touch_Switch
#      - lambda: id(oled).turn_off();
#      - delay: 5s
#      - lambda: id(oled).turn_on();    
#    on_state:
#      then:
#        - lambda: id(oled).turn_off();
    

    
# Example configuration entry
i2c:
  sda: D3
  scl: D4

font:
  - file: "fonts/nasalization.ttf"
    id: font1
    size: 23
  - file: "fonts/refsan.ttf"
    id: font2
    size: 35    
    
display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    id: oled
    reset_pin: D0
    address: 0x3C
    lambda: |-
        it.printf(0, 0, id(font1), "Outside");
        it.printf(0, 22, id(font2), TextAlign::TOP_LEFT , "%.1f°c", id(outside_temperature).state);
      #  it.printf(0, 8, id(font1), id(office_temperature).state);

do u need that line update_interval: 100ms
i find its the only way it works but it goes crazy in the logs

The interval can be set higher 500ms, 1sec, etc. it will effect how responsive it will be. 100ms seems to be excessive. Start with 1s and adjust from there.

i did play with it… if i icreased things it didnt work right

i tried 5 seconds 10 seconds

i did find some issues… 1 i didnt need the touch pad just the wire on the AOC pin

2nd… when i got it to knda work with the 100ms logging that was only way i could get the screen to turn off

but it would shut off on its own or like the air caused the touch pad to shut it off… is there a way to do it…
for now i scrapped the idea i think the touch pad is too sensitiive?? that u blowing on it can act like your finger touched it