Skip to content

Instantly share code, notes, and snippets.

@dacoffey
Created September 20, 2022 20:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacoffey/787522faed2448757a9e48f56ff5200d to your computer and use it in GitHub Desktop.
Save dacoffey/787522faed2448757a9e48f56ff5200d to your computer and use it in GitHub Desktop.
ESPHOME: AdaFruit LED Backpack Integration: ht16k33
#include <Adafruit_LEDBackpack.h>
Adafruit_7segment LEDSEG;
esphome::time::ESPTime DTNOW;
char DTSTR[17];
time_t DT;
logger:
level: WARN
esphome:
name: test
platform: ESP32
board: esp32dev
includes: CLOCK.h
libraries:
- "SPI"
- "Wire"
- "Adafruit BusIO"
- "Adafruit GFX Library"
- "Adafruit LED Backpack Library"
on_boot:
then:
- lambda: !lambda |-
DTNOW = id(DTHA).now();
id(LEDSEGCOLON) = true;
LEDSEG = Adafruit_7segment();
LEDSEG.begin(0x70);
LEDSEG.setBrightness(15);
LEDSEG.blinkRate(HT16K33_BLINK_OFF);
globals:
- id: LEDSEGCOLON
type: bool
restore_value: no
<<: !include CONFIG_WIFI.yaml
time:
- platform: homeassistant
id: DTHA
i2c:
scl: 22
sda: 21
frequency: 50000
scan: true
sensor:
- platform: template
name: "T_DT"
update_interval: 100ms
lambda: |-
DTNOW = id(DTHA).now();
DT = DTNOW.timestamp;
strftime(DTSTR, sizeof(DTSTR), "%I%M", localtime(&DT));
return DT;
- platform: template
name: "T_LEDSEG"
update_interval: 750ms
lambda: |-
LEDSEG.print(strtoul(DTSTR,NULL,10));
LEDSEG.drawColon(id(LEDSEGCOLON) = !id(LEDSEGCOLON));
LEDSEG.writeDisplay();
return id(LEDSEGCOLON);
@dacoffey
Copy link
Author

@malo6008
Copy link

Hi,
I'm using your code for a simple clock based on an Adafruit 7 segment display. I've changed the strftime format to "%H%M", as used as a normal way in Europe (24h clock). However, as from midnight until 1:00 am, the clock shows only the minutes (e.g.: ":45" for 0:45). I'd like to change it to 0:45 or even 00:45. How can I achieve this? I think I have to replace "LEDSEG.print(strtoul(DTSTR,NULL,10));" somehow, but unfortunately, I'm not a C++ (or Lambda?) programmer , sorry...

Anyway, thanks in advance for your kind help!

@ropalo77
Copy link

ropalo77 commented Apr 1, 2023

substitutions:
plug_name: ESP32 DEVKITV1

esphome:
name: esp-clock
comment: ${plug_name}
includes: include/Adafruit_LEDBackpack.h
libraries:
- "SPI"
- "Wire"
- "Adafruit BusIO"
- "Adafruit GFX Library"
- "Adafruit LED Backpack Library"

esp32:
board: nodemcu-32s

wifi:
ssid: "yourssid_wifi"
password: "yourpass_wifi"
power_save_mode: none

api:

web_server:
port: 80

logger:
baud_rate: 0

ota:

i2c:
scl: 22
sda: 21
scan: true

text_sensor:

  • platform: template
    name: "LED time"
    id: led_time
    icon: mdi:web-clock
    update_interval: 1s
    lambda: |-
    Adafruit_7segment display;
    if (!display.begin()) {
    display = Adafruit_7segment();
    }
    if (id(ha_sun).elevation() < 0) {
    display.setBrightness(1);
    } else {
    display.setBrightness(10);
    }
    auto now = id(ha_time).now();
    char text[6];
    if (now.hour == 0) {
    snprintf(text, sizeof(text), "0%02d", now.minute);
    } else if (now.hour < 10) {
    snprintf(text, sizeof(text), " %d%02d", now.hour, now.minute);
    } else {
    snprintf(text, sizeof(text), "%d%02d", now.hour, now.minute);
    }
    display.print(text);
    static int i = 0;
    if (i == 1) {
    display.drawColon(true);
    } else {
    display.drawColon(false);
    }
    i++;
    if (i == 2) {
    i = 0;
    }
    display.writeDisplay();
    std::string str(text);
    str.insert(str.length() - 2, ":");
    return str;

binary_sensor:

  • platform: status
    name: "${plug_name} Status"

switch:

  • platform: restart
    name: "${plug_name} Restart"

status_led:
pin:
number: GPIO2
inverted: false

time:

  • platform: homeassistant
    id: ha_time

sun:
latitude: 88.88°
longitude: 88.88°
id: ha_sun

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