Skip to content

Instantly share code, notes, and snippets.

@kdorff
Last active September 29, 2023 17:46
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.
##
## 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
## 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:
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: "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.
##
- 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
@bjackerman
Copy link

bjackerman commented Sep 29, 2023

To clarify, it sounds like you want to show the value of a Home Assistant (HA) sensor, some temperature value, which we will call temp1 for our discussion. If you want to grab any sensor value that is defined in HA, refer to Kevin's example starting on line 123 (minus comments:

sensor:
  - platform: homeassistant
    id: back_yard_temperature
    entity_id: sensor.back_yard_sensor_temperature

You define a new ESPHome sensor stating it is based on one from HA. You can give it whatever ID you want to refer to in your YAML, but remember to match the HA id exactly in the entity_id field.

Let's assume you did that and your sensor has the id: "temp1".

To display that in a lambda expression, you need to get the value from the sensor and then knowing it is most likely an integer, you need to cast that to a string to display. So in your line above that goes like this:

- lambda: |-
          id(ledkey1_display) = "temp1" + esphome::to_string(id((temp_1));

You are setting the value of the variable ledkey1_display, which is a string. You are trying to assign the concatenation of the literal string "temp1" which gets displayed and what looks like the correct way to refer to the value of temp1 (assuming the underscore is a typo) and properly cast that value to a string.

My best guess, is that you just didn't properly define the value of temp1 as a sensor from HA. Feel free to share where you tried to define that if you need help,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment