How to get the time in an ESPHome configuration?

Can someone pull me from this rabbit hole? I have been trying for two days to make this work, but getting the current time in ESPHome should be relatively simple.

Let me start with the end point. I have an ESP device connected to two Adafruit ht16k33 backpacks with 14-segment LED displays. That much is working. I can display text with it.print:

display:
  - platform: ht16k33_alpha
    address: 0x71
    scroll: false
    lambda: |-
      it.set_brightness(0.1);
      it.print("1234");      

  - platform: ht16k33_alpha
    address: 0x70
    scroll: false
    lambda: |-
      it.set_brightness(0.1);
      it.print("5678");

But what I want is the time in the display at 0x71 in “HHMM” format.

Here is the device YAML configuration:

external_components:
  - source:
      type: git
      url: https://github.com/ssieb/esphome_components
    components: [ ht16k33_alpha ]

substitutions:
  device_name: adafruit-test
  friendly_name: adafruit_test

packages:
  wifi: !include common/wifi.yaml
  device_base: !include common/esp8266.yaml

# -------------------------- Overrides --------------------------
esp8266:
  board: d1_mini

logger:
  level: DEBUG

binary_sensor:
  - platform: status
    name: ${device_name} status

i2c:
  scl: D1 #GPIO3
  sda: D2 #GPIO5
  frequency: 50000
  scan: true

time:
  - platform: homeassistant
    id: homeassistant_time

text_sensor:
  - platform: template
    name: "Current Time"
    id: current_time
    lambda: |- 
      char formatted_time[6];  // Space for "HHMM\0"
      snprintf(formatted_time, sizeof(formatted_time), "%02d%02d", id(homeassistant_time).now().hour, id(homeassistant_time).now().minute);
      return esphome::optional<std::string>(formatted_time);


interval:
  - interval: 1sec
    then:
      - logger.log:
          level: DEBUG
          format: "Current time: %s"
          args: id(current_time).state

display:
  - platform: ht16k33_alpha
    address: 0x71
    scroll: false
    lambda: |-
      it.set_brightness(0.1);
      it.print("1234");      

  - platform: ht16k33_alpha
    address: 0x70
    scroll: false
    lambda: |-
      it.set_brightness(0.1);
      it.print("5678");      

Where I am at now:

I tried to see sensor.current_time using the logger tool, but all I get is [16:02:42][D][main:349]: Current time: \xe8\xfe\xff?

I defined homeassistant_time in sensors.yaml:

# Makes:
# sensor.homeassistant_time
# 2024-03-10 14:46:11.452756-04:00
  sensors:
    homeassistant_time:
      friendly_name: "Home Assistant Time"
      value_template: "{{ now() }}"

Please help me Obi-Wan.

Check " Converting time to text" - may be this is solution.

Thanks for the tip. I tried something similar, but…

 error: 'class esphome::ht16k33_alpha::HT16K33AlphaDisplay' has no member named 'set_component_text';

I am just baffled that getting the time into ESPHome is so difficult.

Just tried following. Work perfect - text sensor contains hours & minutes.

time:
  - platform: homeassistant
    id: homeassistant_time

text_sensor:
  - platform: template
    name: "Current Time"
    id: current_time
    update_interval: 10s
    lambda: return  id(homeassistant_time).now().strftime("%H%M");

Then no issues to put it onto display by it.print(id(current_time).state); most probably.
Or may be:

      it.printf("%s", id(current_time).state);
1 Like

Works great. Thanks

The esphome documentation for thermocouple…no, wait! The Time documentation has lots examples and it verbatim actually tells you how to do this.


%c

Date and time representation

Sat Aug 18 16:31:42 2018

%x

Date representation

08/18/18

%X

Time representation

16:31:42

%%

A literal % character

%
1 Like

I did see this but the display component is a 14-segment LED, thus no font.

I have decided to shelve this project since the time display is unreliable. It starts off by being 30 seconds slow, then a few hours later it just stops updating the time.

Background:
I have built some LED displays that have two 4-character 14-segment LEDs that display the current time and my glucose levels. The code is written in Arduino C++ and gets data from Node-Red every ten seconds. It’s been a desire of mine to move it to ESPHome. I thought I was on a final stretch when I was referred to the sseib ht16k33 component, which provides a component for the 14-segment display.

But I do sincerely appreciate the feedback I have received.

Thus remove the configuration for “font” and problem solved… You have to keep an open mind and dont forget these are just examples. You arent always going to be able to copy/paste some code you find online.

You also need to be prepared to visit the Docs pages frequently. Looking at the docs for a 7-segment display as an example. See the “Configuration Variables”? A quick check here will tell you that ‘font’ isnt something you can configure for these types of displays. Font is only for LCD type displays.

Now just use the example the docs give you minus the ‘form’ configuration variable. There’s even a ‘Time’ example on the 7-segment docs page…

Wow, 30 seconds off? Well, if you wanted help, no one can offer any if you dont provide your config.

Post number 1.

oops, I missed that somehow.

This works for me. There’s no delays in time keeping.


time:
  - platform: homeassistant
    id: homeassistant_time

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

Interesting. The main difference from my code is that I used the strftime() function.
At any rate, your code is still working after two hours, but it’s the overnight timing that I want to see before proceeding with my project.

Is there anything else going on in your config at night? No deep_sleep or anything?

I did see some older posts and people were having time sync problems and for whatever reason they fixed it by either using a static IP or not using a static IP. I cant remember which it was now but, it was one or the other.

I rarely use static IP. I can only guess that the strftime() function takes too long to run.

I doubt it. It just reformats a date/time to a string, its pretty basic. I was looking earlier to see how often the time_sync happens and if you can force an update in the event of a growing time difference. I didnt see a way to do it or the default update frequency.

Your code syncs the time pretty well. The display shows HHMM, and the minutes updates within a second of the PC clock.

But, as before, the time display just stops updating after a few hours. Restarting the device does not get the time back, restarting HomeAssistant does.

The time display?

See post #1

I see it but post 1 doesn’t print the time on either display and you’ve got code chunks in every other post from editing and changing your config. I don’t remember getting a paycheck from you in the mail Friday so post your most recent config so I don’t have to work so hard! You trying to make my brain hurt?

what is confusing is the time in esphome is staying in sync but, the time printed on your display isn’t? The only way to sync the displayed time is to restart HA?

Do you use deep_sleep on this esp board?