ESPhome reformat text for date

Hi,

I want to change the date on my display of esphome.

I have this date now: 16.08.2024
I want to have: Friday, 16.08.2024

I have this code, what do I need to change. I tried with AI tools, but failed.

  - platform: homeassistant
    id: ha_date
    entity_id: sensor.date
    on_value:
      then:
        - lvgl.label.update:
            id: display_date
            text: !lambda |-
              std::string date_str = id(ha_date).state.c_str();
              std::string year = date_str.substr(0, 4);
              std::string month = date_str.substr(5, 2);
              std::string day = date_str.substr(8, 2);
              return (day + "." + month + "." + year).c_str();

Check this one: ESPHome Formatting HA Time Entity with strftime
As well: Converting time to text · Issue #469 · esphome/issues · GitHub

Tried AI and failed…not really surprising. Next time I’d start by using real intelligence and just go search the esphome documentation because, it specifically addresses this and has examples. Not only that but, you’ll learn new things and increase your intelligence instead of using AI and learning nothing at all.

I checked, but I only get errors.
I have this

  - platform: homeassistant
    id: ha_date
    entity_id: sensor.time_date
    on_value:
      then:
        - lvgl.label.update:
            id: display_date
            text: !lambda |-
              auto time = id(ha_date).now();
              return time.strftime("%A %d %B %Y").c_str();

I got the error has no member named now?

ha-panel.yaml: In lambda function:
ha-panel.yaml:534:30: error: ‘class esphome::homeassistant::HomeassistantTextSensor’ has no member named ‘now’
*** [.pioenvs/ha-panel-1/src/main.o] Error 1

Just added following to my test D1 mini:

time:
  - platform: homeassistant
    id: ha_date

text_sensor:
  - platform: template 
    id: display_date
    name: ha2esp_date
    update_interval: 10s
    lambda: |-
      return id(ha_date).now().strftime("%A %d %B %Y");

See in log:

[22:02:34][D][text_sensor:064]: 'ha2esp_date': Sending state 'Saturday 17 August 2024'

As well in HA:
image

IMHO so far it work perfect - text sensor every 10s populated with formatted date/time. When by automation it can be pubished whatevere else.

tnx, that helped a lot.

I now have

  - platform: template 
    id: display_date_sensor
    name: ha2esp_date
    update_interval: 10s
    lambda: |-
      return id(ha_date).now().strftime("%A, %d.%m");
    on_value:
      then:
        - lambda: |-
            lv_label_set_text(id(display_date), x.c_str());

I got this as result

D][text_sensor:064]: ‘ha2esp_date’: Sending state ‘Sunday, 18.08’

I have a question:

Is it possible to use localiziation, so the language of the weekdayname is translated in right language?

Thats a good question and unfortunately I have no idea about alternate languages. An alternative that would work is using substitutions just like the documentation explains and you may want to visit there.

So, create a list of weekdays in English and then map them to the translation you prefer.