Turn ON / OFF OLED with Switch

Hey guys in my new project I’m trying to set up a switch to turn the OLED backlight on and off from Home Assistant.
It kindof works, but when I turn on the switch, the screen turns on but the switch switches back after 1s. i.e. to disable I have to enable switch again and disable instantly.
pls Help find the error

switch:
  - platform: template
    name: "MagicDeckOled"
    turn_on_action:
      - lambda: id(oled).turn_on();
    turn_off_action:
      - lambda: id(oled).turn_off();

Full Config

esphome:
  name: magicdeck
  on_boot:
    priority: -100
    then:
      - lambda: id(oled).turn_off();

esp32:
  board: esp32dev
  framework:
    type: arduino


# Enable logging
logger:

# Enable Home Assistant API
api:
  services:
    - service: play_rtttl
      variables:
        song_str: string
      then:
        - rtttl.play:
            rtttl: !lambda 'return song_str;'
            
            # https://esphome.io/components/rtttl.html
            # In the developer tools. E.g. for calling rtttl.play select the service droplet_play_rtttl and in service data enter    siren:d=8,o=5,b=100:d,e,d,e,d,e,d,e   or  scale_up:d=32,o=5,b=100:c,c#,d#,e,f#,g#,a#,b  


# Integrated BUZZER 
rtttl:
  output: rtttl_out
  on_finished_playback:
    - logger.log: 'Song ended!'


ota:
  password: "4a1deb370b158c5f26170f72593448ef"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  domain: .lab.lan

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

captive_portal:
    

i2c:
  sda: 21
  scl: 22
  scan: true

font: # Fonts for Display
  - file: 'arial.ttf'
    id: font1
    size: 10
  - file: 'arial.ttf'
    id: font2
    size: 10
  - file: 'arial.ttf'
    id: font3
    size: 19
  - file: 'BebasNeue-Regular.ttf'
    id: font4
    size: 20
    

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x32"
    id: oled
    address: 0x3C
    lambda: |-
      it.printf(2, 0, id(font2), TextAlign::TOP_LEFT, "MagicDeck");
      it.printf(61, 0, id(font1) ,"%.1f", id(dbm).state);
      it.line(0, 12, 87, 12);
      it.printf(0, 32, id(font4), TextAlign::BASELINE_LEFT, "%.1f°", id(bedroom1_temperature).state);
      it.printf(127, 32, id(font4), TextAlign::BASELINE_RIGHT,"%s", id(hamessage).state.c_str());


text_sensor:
  - platform: homeassistant
    id: hamessage
    entity_id: input_text.magicdeckmessage


# Example configuration entry
apds9960:
  address: 0x39
  update_interval: 60s

sensor:

  - platform: apds9960
    type: PROXIMITY
    name: "APDS9960 Proximity Sensor"


  - platform: homeassistant
    id: bedroom1_temperature
    entity_id: sensor.bedroom1_temperature



  # WIFI #
  - platform: wifi_signal
    name: "Droplet_WiFi_Signal_Sensor"
    id: "dbm"
    update_interval: 5s

switch:
  - platform: template
    name: "MagicDeckOled"
    turn_on_action:
      - lambda: id(oled).turn_on();
    turn_off_action:
      - lambda: id(oled).turn_off();


binary_sensor:

  - platform: apds9960
    direction: UP
    name: "APDS960 Left Movement"
    id: left
    
  - platform: apds9960
    direction: DOWN
    name: "APDS960 Right Movement"
    id: right
  # Repeat for each direction




  - platform: gpio
    pin: 32
    name: "Button 1"
    filters:
      - delayed_on: 100ms
    on_press:
      then:
        - light.turn_off: backlit
        - light.turn_on: led1
        - lambda: id(oled).turn_on();
        - delay: 30s
        - lambda: id(oled).turn_off();
    on_release:
      then:
        - light.turn_off: led1
        - light.turn_on: 
            id: backlit
            effect: Rainbow

  - platform: gpio
    pin: 35
    name: "Button 2"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - light.turn_off: backlit
        - light.turn_on: led2
        - lambda: id(oled).turn_on();
        - delay: 30s
        - lambda: id(oled).turn_off();
    on_release:
      then:
        - light.turn_off: led2
        - light.turn_on: 
            id: backlit
            effect: Rainbow

  - platform: gpio
    pin: 34
    name: "Button 3"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - light.turn_off: backlit
        - light.turn_on: led3
        - lambda: id(oled).turn_on();
        - delay: 30s
        - lambda: id(oled).turn_off();
    on_release:
      then:
        - light.turn_off: led3
        - light.turn_on: 
            id: backlit
            effect: Rainbow

  - platform: gpio
    pin: 39
    name: "Button 4"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - light.turn_off: backlit
        - light.turn_on: led4
        - lambda: id(oled).turn_on();
        - delay: 30s
        - lambda: id(oled).turn_off();
    on_release:
      then:
        - light.turn_off: led4
        - light.turn_on: 
            id: backlit
            effect: Rainbow

  - platform: gpio
    pin: 36
    name: "Button 5"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - light.turn_off: backlit
        - light.turn_on: led5
        - lambda: id(oled).turn_on();
        - delay: 30s
        - lambda: id(oled).turn_off();
    on_release:
      then:
        - light.turn_off: led5
        - light.turn_on: 
            id: backlit
            effect: Rainbow


  - platform: gpio
    pin: 33
    name: "Button 6"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - light.turn_off: backlit
        - light.turn_on: led6
        - lambda: id(oled).turn_on();
        - delay: 30s
        - lambda: id(oled).turn_off();
    on_release:
      then:
        - light.turn_off: led6
        - light.turn_on: 
            id: backlit
            effect: Rainbow

  - platform: gpio
    pin: 25
    name: "Button 7"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - light.turn_off: backlit
        - light.turn_on: led7
        - lambda: id(oled).turn_on();
        - delay: 30s
        - lambda: id(oled).turn_off();
    on_release:
      then:
        - light.turn_off: led7
        - light.turn_on: 
            id: backlit
            effect: Rainbow

  - platform: gpio
    pin: 26
    name: "Button 8"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - light.turn_off: backlit
        - light.turn_on: led8
        - lambda: id(oled).turn_on();
        - delay: 30s
        - lambda: id(oled).turn_off();
    on_release:
      then:
        - light.turn_off: led8
        - light.turn_on: 
            id: backlit
            effect: Rainbow

  - platform: gpio
    pin: 27
    name: "Button 9"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - light.turn_off: backlit
        - light.turn_on: led9
        - lambda: id(oled).turn_on();
        - delay: 30s
        - lambda: id(oled).turn_off();
    on_release:
      then:
        - light.turn_off: led9
        - light.turn_on: 
            id: backlit
            effect: Rainbow

  - platform: gpio
    pin: 14
    name: "Button 10"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - light.turn_off: backlit
        - light.turn_on: led10
        - lambda: id(oled).turn_on();
        - delay: 30s
        - lambda: id(oled).turn_off();
    on_release:
      then:
        - light.turn_off: led10
        - light.turn_on: 
            id: backlit
            effect: Rainbow
          
          

  - platform: status
    name: "MagicDeck Status"

light:
  - platform: fastled_clockless
    chipset: WS2812
    id: backlit
    pin: GPIO15
    num_leds: 10
    rgb_order: GRB
    name: "MagicDeckLeds"
    default_transition_length: 0.1s
    effects:
      - strobe:
      - random:
      
      - addressable_rainbow:
      - addressable_rainbow:
          name: Rainbow Effect With Custom Values
          speed: 10
          width: 50
          
      - addressable_color_wipe:
      - addressable_color_wipe:
          name: Color Wipe Effect With Custom Values
          colors:
            - red: 100%
              green: 100%
              blue: 100%
              num_leds: 1
            - red: 0%
              green: 0%
              blue: 0%
              num_leds: 1
          add_led_interval: 100ms
          reverse: false
          
      - addressable_scan:
      - addressable_scan:
          name: Scan Effect With Custom Values
          move_interval: 100ms
          scan_width: 1

      - addressable_twinkle:
      - addressable_twinkle:
          name: Twinkle Effect With Custom Values
          twinkle_probability: 5%
          progress_interval: 4ms






  - platform: partition
    name: "MagicDeckLed 1"
    id: led1
    segments:
      - id: backlit
        from: 0
        to: 0
    effects:
      - strobe:
      
  - platform: partition
    name: "MagicDeckLed 2"
    id: led2
    segments:
      - id: backlit
        from: 1
        to: 1
    effects:
      - strobe:
      
  - platform: partition
    name: "MagicDeckLed 3"
    id: led3
    segments:
      - id: backlit
        from: 2
        to: 2
    effects:
      - strobe:
      
  - platform: partition
    name: "MagicDeckLed 4"
    id: led4
    segments:
      - id: backlit
        from: 3
        to: 3
    effects:
      - strobe:
      
  - platform: partition
    name: "MagicDeckLed 5"
    id: led5
    segments:
      - id: backlit
        from: 4
        to: 4
    effects:
      - strobe:
      
  - platform: partition
    name: "MagicDeckLed 6"
    id: led6
    segments:
      - id: backlit
        from: 5
        to: 5
    effects:
      - strobe:
      
  - platform: partition
    name: "MagicDeckLed 7"
    id: led7
    segments:
      - id: backlit
        from: 6
        to: 6
    effects:
      - strobe:
      
  - platform: partition
    name: "MagicDeckLed 8"
    id: led8
    segments:
      - id: backlit
        from: 7
        to: 7
    effects:
      - strobe:
      
  - platform: partition
    name: "MagicDeckLed 9"
    id: led9
    segments:
      - id: backlit
        from: 8
        to: 8
    effects:
      - strobe:
      
  - platform: partition
    name: "MagicDeckLed 10"
    id: led10
    segments:
      - id: backlit
        from: 9
        to: 9
    effects:
      - strobe:




output:

  # Integrated BUZZER # https://esphome.io/components/output/ledc.html?highlight=buzzer
  - platform: ledc
    pin: GPIO19
    id: rtttl_out
1 Like

Try optimistic in the template switch Template Switch — ESPHome

1 Like

Thanks!
It works :smiley:

switch:
  - platform: template
    name: "MagicDeckOled"
    optimistic: true
    turn_on_action:
      - lambda: id(oled).turn_on();
    turn_off_action:
      - lambda: id(oled).turn_off();

1 Like