Ssd1306 Screen ON/OFF time based

Hi I have a ssd1306 connected to a sensor and would like to now if the screen can be turned off say at 10pm to 6am the screen is going in a bedroom and I don’t want it going while trying to sleep ? if thats possibleThanks

So, you didn’t leave much information on your setup but this is how I would address your request. Assuming that you have a connection to HA, I would do the following.

Utilize the time: component to setup a 1 minute re-occurring event.

time:
  id: ha_time
  platform: homeassistant
  on_time:
    - seconds: 0
      then:
        lambda: |-
          static bool bDisplay = true;
          auto now = id(ha_time).now(); // local time
          if ((now.hour >= 22 || now.hour < 6) && bDisplay) {
            id(turn_display_off).execute();
            bDisplay = false;
          } else if ((now.hour >= 6 && now.hour < 22) && !bDisplay) {
            id(turn_display_on).execute();
            bDisplay = true;
         }

The above code Is psuedo code-ish. I’m not claiming it will compile but it’s close. It appears your display has on/off commands 0xAE, 0xAF. They would be sent in the two scripts turn_display_off and turn_display_on. You could of course utilize the api: component to pull times from HA to make it more configurable. I utilize Lamba coding almost exclusively so there may be a way of doing the same thing in the scripting language as well. Hope this helps.

1 Like

Hi thanks for your reply (sorry been late)
MY yaml file as below but i’m getting (below) while validating the code

INFO Reading configuration...
ERROR Error while reading config: Invalid YAML syntax. Please see YAML syntax reference or use an online YAML syntax validator:

mapping values are not allowed here
  in "/config/esphome/master_bedroom_sensor.yaml", line 143, column 10:
         then:
             ^
esphome:
  name: master_bedroom_sensor
  platform: ESP32
  board: esp-wrover-kit

wifi:
  ssid: "----"
  password: "-----"
  power_save_mode: none 
  manual_ip:
#    # Set this to the IP of the ESP
    static_ip: 192.168.1.33
#    # Set this to the IP address of the router. Often ends with .1
    gateway: 192.168.1.1
#    # The subnet of the network. 255.255.255.0 works for most home networks.
    subnet: 255.255.255.0
    dns1: 192.168.1.12

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "----"

ota:
  password: "----"
  
esp32_ble_tracker:
  scan_interval: 60s 
  
web_server:
  port: 80 
text_sensor:
  - platform: wifi_info
    ip_address:
      name: "Master Bedroom Sensor IP address"
    ssid:
      name: "Master Bedroom Sensor SSID"
  - platform: version
    name: "Master Bedroom Sensor ESPHome Version"
    
apds9960:
  address: 0x39
  update_interval: 30s    
  
  
switch:
  - platform: restart
    name: "Master Bedroom Sensor Restart"

  - platform: shutdown
    name: "Master Bedroom Sensor Shutdown" 

binary_sensor:
  - platform: status
    name: "Master Bedroom Sensor Status"   

  - platform: gpio
    pin: 34
    name: "Master Bedroom Motion"
    device_class: motion
    
  - platform: gpio
    pin: 33
    name: "Sound level"
    filters:
      - delayed_off: 6s  
      
  - platform: ble_presence
    mac_address: ----
    name: "----"   
    
  - platform: ble_presence
    mac_address: "----"
    name: ---- ---- 
    
  - platform: ble_presence
    mac_address: ----
    name: "----"  
    
  - platform: apds9960
    name: "APDS960 Up Movement"
    direction: UP
    
  - platform: apds9960
    name: "APDS960 Down Movement"
    direction: DOWN
    
  - platform: apds9960
    name: "APDS960 Left Movement"
    direction: LEFT
    
  - platform: apds9960
    name: "APDS960 Right Movement"
    direction: RIGHT
    
sensor:
  - platform: wifi_signal
    name: "Master Bedroom Sensor WiFi Signal"
    update_interval: 60s 

  - platform: bmp280
    temperature:
      name: "BME280 Temperature"
      id: mb_temp
      oversampling: 16x
    pressure:
      name: "BME280 Pressure"
      id: mb_press
    address: 0x76
    update_interval: 60s
    
  - platform: apds9960
    name: "APDS9960 Clear Channel"
    type: CLEAR
    
  - platform: apds9960
    name: "APDS9960 Red Channel"
    type: RED
    
  - platform: apds9960
    name: "APDS9960 Green Channel"
    type: GREEN
    
  - platform: apds9960
    name: "APDS9960 Blue Channel"
    type: BLUE
    
  - platform: apds9960
    name: "APDS9960 Proximity Channel"
    type: PROXIMITY
   
  - platform: uptime
    name: Master Bedroom Uptime 
    update_interval: 60s

time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
  - seconds: 0
     then:
      lambda: |-
          static bool bDisplay = true;
          auto now = id(ha_time).now(); // local time
          if ((now.hour >= 22 || now.hour < 6) && bDisplay) {
            id(turn_display_off).execute();
            bDisplay = false;
          } else if ((now.hour >= 6 && now.hour < 22) && !bDisplay) {
            id(turn_display_on).execute();
            bDisplay = true;};

display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    address: 0x3C
    lambda: |-
      // Print "Bedroom Sensor" in top center.
      it.printf(64, 0, id(font1), TextAlign::TOP_CENTER, "Bedroom Sensor");

      // Print Master Bedroom Temperature (from master_bedroom_sensor)
      if (id(mb_temp).has_state()) {
        it.printf(3, 60, id(font2), TextAlign::BASELINE_LEFT , "%.1f°", id(mb_temp).state);
      }

      // Print Master Bedroom Pressure (from master_bedroom_sensor)
      if (id(mb_press).has_state()) {
        it.printf(127, 60, id(font3), TextAlign::BASELINE_RIGHT , "%.1f°", id(mb_press).state);
      }

font:
  - file: 'slkscr.ttf'
    id: font1
    size: 8

  - file: 'BebasNeue-Regular.ttf'
    id: font2
    size: 36

  - file: 'arial.ttf'
    id: font3
    size: 14


i2c:
  sda: 21
  scl: 22
  scan: True

Yaml is very picky about indents and spaces.

time:
  - platform: sntp
    # ...
    on_time:
      # Every 5 minutes
      - seconds: 0
        minutes: /5
        then:
          - switch.toggle: my_switch

I took this out of the documentation. Your time: code is not indented properly.