ESPHome Time Display Solution: A Simple Yet Effective Hack

Hey everyone, I just wanted to share a little hack I came up with for my ESPHome setup. I was trying to figure out how to display the current time in my home automation setup, and after some digging, I realized that there wasn’t a straightforward way to do it out of the box. So, I decided to create my own solution using a text-sensor template. Here’s how I did it:

yaml
platform: homeassistant
id: homeassistant_time
timezone: Europe/Amsterdam
text_sensor:

  • platform: template
    name: “Current time”
    lambda: |
    char str[17];
    time_t currTime = id(homeassistant_time).now().timestamp;
    strftime(str, sizeof(str), “%Y-%m-%d %H:%M”, localtime(&currTime));
    return { str };
    update_interval: 60s

This setup updates the time every 60 seconds, which is perfect for my needs. I noticed that after a reboot, it takes about a minute for the time to set correctly, which I assume is because the system needs a bit of time to sync up after starting. It’s a simple fix, but it makes a world of difference in terms of functionality.

I thought this might be helpful for others who are looking to add a time display to their ESPHome setup. It’s a great way to keep track of the time without needing additional hardware or complicated integrations. Plus, it’s completely customizable, so you can adjust the update interval or the time format to suit your preferences.

If anyone has any questions or suggestions on how to improve this further, I’d love to hear them! Happy automating! :rocket: