Skip to content

Instantly share code, notes, and snippets.

@kdorff
Last active December 28, 2024 17:35
Show Gist options
  • Save kdorff/396a11b5bb29142f09c1f52242c546e5 to your computer and use it in GitHub Desktop.
Save kdorff/396a11b5bb29142f09c1f52242c546e5 to your computer and use it in GitHub Desktop.

Revisions

  1. kdorff revised this gist Oct 18, 2022. 1 changed file with 66 additions and 6 deletions.
    72 changes: 66 additions & 6 deletions led-and-key1-esp32.yaml
    Original file line number Diff line number Diff line change
    @@ -13,12 +13,11 @@
    ## if (ledkey1_display == "") the current time and temperature
    ## will be displayed, instead (obtained from HA).
    ##
    ## Wiring of the TM1638 to an ESP32 D1 Mini
    ## VCC to 3.3v
    ## GND to GND
    ## DIO to D23
    ## CLK to D19
    ## STB to D18
    ## Wiring of the TM1638
    ## 3.3v and GND from 3.3V and GND from the microcontroller (may not be ideal?)
    ## DIO on D23, GPIO34
    ## CLK on D19, GPIO19
    ## STB on D18, GPIO18
    ##

    esphome:
    @@ -161,12 +160,73 @@ binary_sensor:
    - if:
    condition:
    - binary_sensor.is_on: front_door_autolock
    then:
    - logger.log: "LED2 on"
    - output.turn_on: ledkey1_led_2
    else:
    - logger.log: "LED2 off"
    - output.turn_off: ledkey1_led_2
    - platform: homeassistant
    id: back_door_sensor
    entity_id: binary_sensor.back_door_sensor
    publish_initial_state: true
    on_state:
    - if:
    condition:
    - binary_sensor.is_on: back_door_sensor
    then:
    - logger.log: "LED1 on"
    - output.turn_on: ledkey1_led_1
    - lambda: |-
    id(ledkey1_display) = "Back dr";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Opened";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display).clear();
    else:
    - logger.log: "LED1 off"
    - output.turn_off: ledkey1_led_1
    - lambda: |-
    id(ledkey1_display) = "Back dr";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Closed";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display).clear();
    - platform: homeassistant
    id: front_door_sensor
    entity_id: binary_sensor.front_door_sensor
    publish_initial_state: true
    on_state:
    - if:
    condition:
    - binary_sensor.is_on: front_door_sensor
    then:
    - logger.log: "LED3 on"
    - output.turn_on: ledkey1_led_3
    - lambda: |-
    id(ledkey1_display) = "Front dr";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Opened";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display).clear();
    else:
    - logger.log: "LED3 off"
    - output.turn_off: ledkey1_led_3
    - lambda: |-
    id(ledkey1_display) = "Front dr";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Closed";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display).clear();
    ##
    ## The eight buttons on the TM1638.
  2. kdorff created this gist Oct 17, 2022.
    354 changes: 354 additions & 0 deletions led-and-key1-esp32.yaml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,354 @@
    ##
    ## ESPHome configuration for led-and-key1-esp32
    ## * LED0 is lit if the back door autolock is enabled
    ## * LED1 is lit if the front door autolock is enabled
    ## * ..
    ## * BUTTON0 toggles the back door autolock enable
    ## * BUTTON1 toggles the front door autolock enable
    ## * ...
    ## * BUTTON6 will decrease the intensity
    ## * BUTTON7 will decrease the intensity
    ##
    ## The global ledkey1_display controls what is being displayed.
    ## if (ledkey1_display == "") the current time and temperature
    ## will be displayed, instead (obtained from HA).
    ##
    ## Wiring of the TM1638 to an ESP32 D1 Mini
    ## VCC to 3.3v
    ## GND to GND
    ## DIO to D23
    ## CLK to D19
    ## STB to D18
    ##

    esphome:
    name: led-and-key1-esp32

    esp32:
    board: esp32dev
    framework:
    type: arduino

    # Enable logging
    logger:

    # Enable Home Assistant API
    api:
    encryption:
    key: !secret ledkey1_esp32_api_encryption

    ota:
    password: !secret ota_password

    wifi:
    ssid: !secret wifi_ssid
    password: !secret wifi_password
    fast_connect: true
    domain: .mylocal

    # Enable fallback hotspot (captive portal) in case wifi connection fails
    ap:
    ssid: "led-and-key-1 Fallback Hotspot"
    password: !secret fallback_wifi_password

    captive_portal:

    ##
    ## Use HA as a source of time.
    ##
    time:
    - platform: homeassistant
    id: esptime

    globals:
    ##
    ## The value to be displayed
    ##
    - id: ledkey1_display
    type: std::string
    restore_value: no
    initial_value: ""
    ##
    ## The value currently on the display
    ##
    - id: ledkey1_display_current
    type: std::string
    restore_value: no
    initial_value: ""
    ##
    ## The intensity of the display (0-7).
    ##
    - id: intensity
    type: int
    restore_value: yes
    initial_value: "1"

    ##
    ## Display the value in the global ledkey1_display
    ## or display the time and temperature if
    ## (ledkey1_display == "").
    ##
    display:
    platform: tm1638
    id: ledkey1_display_actual
    dio_pin: 23
    clk_pin: 19
    stb_pin: 18
    intensity: 1
    update_interval: 0.5s
    lambda: |-
    it.set_intensity(id(intensity));
    if (strlen(id(ledkey1_display).c_str()) == 0) {
    //
    // Nothing being displayed. Show the time and temp.
    //
    id(ledkey1_display_current).clear();
    it.print(" ");
    it.printf("%s %.0fF", id(esptime).now().strftime("%I.%M").c_str(), id(back_yard_temperature).state);
    }
    else {
    if (strcmp(id(ledkey1_display).c_str(), id(ledkey1_display_current).c_str()) != 0) {
    //
    // New text to display. Show it and remember it
    // so we don't have to show it again in the next iteration
    // of the lambda.
    //
    id(ledkey1_display_current).clear();
    id(ledkey1_display_current).append(id(ledkey1_display));
    // Clear the display and then print.
    it.print(" ");
    it.print(id(ledkey1_display).c_str());
    }
    }
    sensor:
    ##
    ## HA sensors we need to do our work.
    ##
    - platform: homeassistant
    id: back_yard_temperature
    entity_id: sensor.back_yard_sensor_temperature

    binary_sensor:
    ##
    ## More HA sensors we need to do our work.
    ##
    - platform: homeassistant
    id: back_door_autolock
    entity_id: input_boolean.keymaster_back_door_autolock
    publish_initial_state: true
    on_state:
    ##
    ## Observe initial state and state changes within HA.
    ##
    - if:
    condition:
    - binary_sensor.is_on: back_door_autolock
    then:
    - logger.log: "LED0 on"
    - output.turn_on: ledkey1_led_0
    else:
    - logger.log: "LED0 off"
    - output.turn_off: ledkey1_led_0
    - platform: homeassistant
    id: front_door_autolock
    entity_id: input_boolean.keymaster_front_door_autolock
    publish_initial_state: true
    on_state:
    ##
    ## Observe initial state and state changes within HA.
    ##
    - if:
    condition:
    - binary_sensor.is_on: front_door_autolock
    then:
    - logger.log: "LED1 on"
    - output.turn_on: ledkey1_led_1
    else:
    - logger.log: "LED1 off"
    - output.turn_off: ledkey1_led_1

    ##
    ## The eight buttons on the TM1638.
    ##
    - platform: tm1638
    name: "ledkey1 button 0"
    id: ledkey1_button_0
    key: 0
    on_click:
    ##
    ## Button 0 is pressed. Toggle back door autolock.
    ##
    - if:
    condition:
    - binary_sensor.is_on: back_door_autolock
    then:
    - logger.log: "Back door autolock disabled"
    - homeassistant.service:
    ##
    ## I thought changing ESPHome's binary_sensor.back_door_autolock
    ## should have changed the associated input_boolean.keymaster_back_door_autolock
    ## but it didn't seem to. Calling the HA service to change the value worked.
    ##
    service: "input_boolean.turn_off"
    data:
    entity_id: "input_boolean.keymaster_back_door_autolock"
    - lambda: |-
    id(ledkey1_display) = "Back dr";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Autolock";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Disabled";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display).clear();
    else:
    - logger.log: "Back door autolock enabled"
    - homeassistant.service:
    service: "input_boolean.turn_on"
    data:
    entity_id: "input_boolean.keymaster_back_door_autolock"
    - lambda: |-
    id(ledkey1_display) = "Back dr";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Autolock";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Enabled";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display).clear();
    - platform: tm1638
    name: "ledkey1 button 1"
    id: ledkey1_button_1
    key: 1
    on_click:
    ##
    ## Button 1 is pressed. Toggle front door autolock.
    ##
    - if:
    condition:
    - binary_sensor.is_on: front_door_autolock
    then:
    - logger.log: "Front door autolock disabled"
    - homeassistant.service:
    service: "input_boolean.turn_off"
    data:
    entity_id: "input_boolean.keymaster_front_door_autolock"
    - lambda: |-
    id(ledkey1_display) = "Front dr";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Autolock";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Disabled";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display).clear();
    else:
    - logger.log: "Front door autolock enabled"
    - homeassistant.service:
    service: "input_boolean.turn_on"
    data:
    entity_id: "input_boolean.keymaster_front_door_autolock"
    - lambda: |-
    id(ledkey1_display) = "Front dr";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Autolock";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display) = "Enabled";
    - delay: 1s
    - lambda: |-
    id(ledkey1_display).clear();
    - platform: tm1638
    name: "ledkey1 button 2"
    id: ledkey1_button_2
    key: 2
    - platform: tm1638
    name: "ledkey1 button 3"
    id: ledkey1_button_3
    key: 3
    - platform: tm1638
    name: "ledkey1 button 4"
    id: ledkey1_button_4
    key: 4
    - platform: tm1638
    name: "ledkey1 button 5"
    id: ledkey1_button_5
    key: 5
    - platform: tm1638
    name: "ledkey1 button 6"
    id: ledkey1_button_6
    key: 6
    on_click:
    ##
    ## Button 6: Decrease display intensity
    ##
    - lambda: |-
    if (id(intensity) > 0) {
    id(intensity)--;
    }
    id(ledkey1_display) = "Inten " + esphome::to_string(id(intensity));
    - delay: 1s
    - lambda: |-
    id(ledkey1_display).clear();
    - platform: tm1638
    name: "ledkey1 button 7"
    id: ledkey1_button_7
    key: 7
    on_click:
    ##
    ## Button 7: Increase display intensity
    ##
    - lambda: |-
    if (id(intensity) < 7) {
    id(intensity)++;
    }
    id(ledkey1_display) = "Inten " + esphome::to_string(id(intensity));
    - delay: 1s
    - lambda: |-
    id(ledkey1_display).clear();
    ##
    ## The LEDs. We control these elsewhere, as necessary.
    ##
    output:
    - platform: tm1638
    # name: "ledkey1 led 0"
    id: ledkey1_led_0
    led: 0
    - platform: tm1638
    # name: "ledkey1 led 1"
    id: ledkey1_led_1
    led: 1
    - platform: tm1638
    # name: "ledkey1 led 2"
    id: ledkey1_led_2
    led: 2
    - platform: tm1638
    # name: "ledkey1 led 3"
    id: ledkey1_led_3
    led: 3
    - platform: tm1638
    # name: "ledkey1 led 4"
    id: ledkey1_led_4
    led: 4
    - platform: tm1638
    # name: "ledkey1 led 5"
    id: ledkey1_led_5
    led: 5
    - platform: tm1638
    # name: "ledkey1 led 6"
    id: ledkey1_led_6
    led: 6
    - platform: tm1638
    # name: "ledkey1 led 7"
    id: ledkey1_led_7
    led: 7