This ESPHome recipe is for a clock using the MAX7219 LED display driver without leading zeros in the time display.
ESPHome does not have the %-I format specifier available in strftime() to display the hour without leading zeros. Therefore, the code provided on the wiki page uses an if statement to check the hour and adjust it accordingly. This is achieved by using individual time components and displaying them as numerical values without leading zeros.
For example, if the hour is greater than or equal to 13, it subtracts 12 from the hour and displays it as “1” instead of “01”. Similarly, if the hour is less than 10, it simply displays it as “9” instead of “09”. This allows the clock to display the time without leading zeros, as desired.
Connect the CLK pin of the MAX7219 to GPIO 18 of the ESP32, and connect the MOSI pin of the MAX7219 to GPIO 23 of the ESP32. Cable select CS goes to GPIO5.
time:
- platform: homeassistant
id: homeassistant_time
font:
- file: "Thintel.ttf"
id: digit_font
size: 16
spi:
clk_pin: 18
mosi_pin: 23
display:
- platform: max7219digit
scroll_enable: false
cs_pin: 5
num_chips: 4
intensity: 5
lambda: |-
char buffer[9];
struct tm timeinfo;
::time_t now;
::time(&now);
localtime_r(&now, &timeinfo);
sprintf(buffer, "%d:%d:%d", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
if (timeinfo.tm_hour >= 13) {
timeinfo.tm_hour -= 12;
}
sprintf(buffer, "%d:%0d.%d", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
it.printf(0, -1, id(digit_font), "%s", buffer);