ESPHome Formatting HA Time Entity with strftime

Hi all,

Can someone please help me work out why this is not working as expected? Instead of a properly formatted date string like “Thursday 11 January 2024” what I actually get is a random set of characters, currently “LA”.

- lambda: id(disp1).set_component_text_printf("Home.date", "%s", id(homeassistant_time).now().strftime("%A %d %B %Y"));
time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      - seconds: 0
        minutes: /1
        then:
          - lambda: id(disp1).set_component_text_printf("Home.time", "%02i:%02i", id(homeassistant_time).now().hour, id(homeassistant_time).now().minute);
          - lambda: id(disp1).set_component_text_printf("Home.date", "%s", id(homeassistant_time).now().strftime("%A %d %B %Y"));
    on_time_sync:
        then:
          - wait_until:
              switch.is_on: nextion_init
          - lambda: id(disp1).set_component_text_printf("Home.time", "%02i:%02i", id(homeassistant_time).now().hour, id(homeassistant_time).now().minute);
          - lambda: id(disp1).set_component_text_printf("Home.date", "%s", id(homeassistant_time).now().strftime("%A %d %B %Y"));

Thank you!

Your function is fine. Maybe the Nextion component expects a C style string. Have you tried:

- lambda: id(disp1).set_component_text_printf("Home.date", "%s", id(homeassistant_time).now().strftime("%A %d %B %Y").c_str());

Clutching at straws I know but worth a try.

That was it! Thank you sir.

1 Like