I’ve been diving into the ESPHome ecosystem lately and it’s been a fascinating journey. One of my recent projects involved setting up an ESP32 with an ILI9341 display to show real-time information. While the standard time display functionality works seamlessly, I wanted to take it a step further by customizing the time display to suit my specific needs.
The goal was to extract the tens and units digits of the hour separately so I could position them independently on the display. For example, if the time is 13:45, I wanted to display the ‘1’ and ‘3’ of the hour in two different locations. This would allow for a more visually appealing and customizable display.
I started by exploring the strftime function, which is commonly used for formatting time strings. However, this approach only gives me the entire hour value as a string, not the individual digits. To achieve my desired outcome, I realized I needed to break down the hour value into its constituent digits.
Here’s how I approached it:
- Extracting the Hour Value: First, I needed to get the current hour value. This can be done using
id(esptime).now().hour(). - Separating the Digits: Once I had the hour value, I could separate the tens and units digits using simple arithmetic operations. For example, if the hour is 13:
- Tens digit = 13 // 10 = 1
- Units digit = 13 % 10 = 3
- Displaying the Digits: Finally, I used two separate
it.printcommands to display each digit at the desired positions on the screen.
This approach not only gave me the flexibility to position the digits independently but also allowed for further customization, such as changing font sizes or colors for each digit.
While this was a fun and educational project, I’m curious to know if there are more efficient or elegant ways to achieve this in ESPHome. If anyone has alternative methods or suggestions, I’d love to hear them!
It’s amazing how a little creativity and problem-solving can transform a basic functionality into something truly personalized. Happy coding everyone! ![]()