ESPHome show time

Just leaving this here for anybody who wants to know the current time of your ESPHome device. This text-sensor template updates the time into the text sensor every 60 seconds. I could not find the time anywhere so i created this solution. It takes 60 seconds after the reboot before the time is set correctly, probably the time is not up to date directly after reboot.

time:
  - 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
13 Likes

Really? It’s in the docs Time Component — ESPHome and Time & Temperature on OLED Display — ESPHome

1 Like

Well yes, that is time explained, the latter one even for an oled. But I was looking for displaying time on the web page of my esp to check if the time is correct. And in the docs there is no copy paste solution, or maybe I am missing something?
From the time docs it requires some knowledge regarding programming to get it done.

Edit: @nickrout focussing on that sentence alone makes me look like that meme with john travolta from pulp fiction. The sentence should have been “I could not find the time displayed anywhere on my ESP so I created this solution”

2 Likes

The reference here doesn’t use strftime the same way, so i found @EnsconcE’s post very useful.

In case someone wants just to log the time, something like that can be used:

time:
  - platform: homeassistant
    id: homeassistant_time
    on_time_sync:
      then:
        - logger.log: "Synchronized system clock"

interval:
  - interval: 1min
    then:
      - logger.log: "1min passed"
      - lambda: |-          
          char str[30];
          time_t currTime = id(homeassistant_time).now().timestamp;
          strftime(str, sizeof(str), "%Y-%m-%d %H:%M", localtime(&currTime));
          ESP_LOGD("custom", "curTime: %s", str);
      - delay: 20s
      - logger.log: "1min and 20s passed"
1 Like