How do you get Time saved in Nextion Screen and into text fields properly

hi i having issues… i posted in another forum for HA when i was working on weather station project…

but figured id ask here in Esphome section

so i have the 2.4 enhanced screen
id like Home assistant to sync the time to the screen when i get a battery for it… so when its connected to the network it will sync the time… rest of the time… it uses the battery to keep the time…

my template.yaml i have

    - name: "Display Time"
      state: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%I:%M %p') }}"
      icon: "mdi:calendar-clock"

    - name: "Display Hour"
      state: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%I') }}"
      icon: "mdi:calendar-clock"

    - name: "Display Minutes"
      state: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%M') }}"
      icon: "mdi:calendar-clock"

my esphome yaml i have some coding

time:
  - platform: sntp
    id: sntp_time

sensor:
  - platform: homeassistant
    id: sun_elevation
    entity_id: sensor.sun_elevation

  - platform: homeassistant   # Inside temperature
    id: display_time
    entity_id: sensor.display_time

i have 3 text boxes in nexiton
dtime <=== to show full time 12 hour with AM/PM
hour <======= hour to show 12 hours
minutes <===== show minutes

i been playing with different ways… home assistant doesnt like my time display says its cant convert time to a number… i dont want it to convert it i just want to print it…

and the lamda

    lambda: |-     
     // Turn display on when somebody is home. Only update when display on.
      float br;
                auto time_text = id(sntp_time).now().strftime("%I:%M%P");
                auto hour_text = id(sntp_time).now().strftime("%I");
                auto minute_text = id(sntp_time).now().strftime("%M");
                it.set_component_text("dtime",time_text.c_str());
               // it.set_component_text("dtime",time_text.c_str());
             //  it.set_component_text_printf("dtime","%I", id(display_time).c_str);
           //    it.set_component_text("dtime", id(display_time).state);
          //     it.set_component_text_printf("dtime","%I", id(display_time).state);
                              
          //      it.set_component_text("hour", hour_text.c_str());
        //        it.set_component_text("hour", hour_text.c_str());
        //        it.set_component_text("minute", id(sntp_time).now().strftime("%M"));
          
        //  it.set_component_text("hour", hour_text.c_str());
      //    it.set_component_text("minute", minute_text.c_str());

i not getting the outcome i was hoping
for 0 errors
time to display from Home Assitant
display in 12 hour format
save it to the the screen… (when battery is there )
and sync it whenever home assistant syncs instead of me trying to pole time on the module
as i also wanted a incar display for time… so each time it connects to my network it will re sync the time.

figured id ask in here as its more narrowed for screen and esphome… as im stuck spinning my wheels googling etc

Where is your log?

well one log is
|-- 1.2
Compiling /data/temperature-display-2/.pioenvs/temperature-display-2/src/main.cpp.o
src/main.cpp:451:3: warning: multi-line comment [-Wcomment]
// \ \n it.set_component_text(“hour”, hour_text.c_str());\n
^
/config/esphome/temperature-display-2.yaml:121:35: warning: multi-character character constant [-Wmultichar]
if (id(somebody_home).state !=‘off’) {
^
/config/esphome/temperature-display-2.yaml: In lambda function:
/config/esphome/temperature-display-2.yaml:119:61: error: request for member ‘hour’ in ‘time2’, which is of pointer type ‘esphome::homeassistant::HomeassistantSensor*’ (maybe you meant to use ‘->’ ?)
it.set_component_text_printf(“hour”,"%02d",time2.hour);
^
/config/esphome/temperature-display-2.yaml:120:64: error: request for member ‘minute’ in ‘time2’, which is of pointer type ‘esphome::homeassistant::HomeassistantSensor*’ (maybe you meant to use ‘->’ ?)
it.set_component_text_printf(“minutes”,"%02d",time2.minute);
^
*** [/data/temperature-display-2/.pioenvs/temperature-display-2/src/main.cpp.o] Error 1
========================== [FAILED] Took 1.59 seconds ==========================

ill have to re plug it in for the logs for the conversion issue

and logs change due to the above i keep changing as i dont know the correct format for the 3 fields i wanna fill in

time2 error is cuz i cant use time

  • platform: homeassistant # Inside temperature
    id: time2
    entity_id: sensor.time

so when i rem out the 2 lines for the time2

the logs of esp is

[16:01:54][C][homeassistant.sensor:031]: Entity ID: ‘sensor.today_icon’
[16:02:00][W][homeassistant.sensor:015]: Can’t convert ‘16:02’ to number!
[16:02:00][D][sensor:113]: ‘time2’: Sending state nan with 1 decimals of accuracy
[16:02:00][W][homeassistant.sensor:015]: Can’t convert ‘04:02 PM’ to number!
[16:02:00][D][sensor:113]: ‘display_time’: Sending state nan with 1 decimals of accuracy
[16:02:08][D][sntp:075]: Synchronized time: 2022-01-03 16:02:08

so what i want is

display_time = 04:40 pm

dtime = 04:40 pm
hours = 04
minutes = 40

and then it saves it in the nextion screen so it can fill those fields in when you plug it in and when its not connected to wifi
and then when connect to wifi is re syncs itself

I presume you have googled that and fixed your commenting.

I don’t see time2 in your code, so where is this coming from?

Frankly your post is barely comprehensible.

well like i mention i been modifying the code.
so the code above doesnt match what i have id have 20 postings of 20 different codes i working with

no idea what you talking about multiline… error havent googled nothign

i only googled like i mentioned to get the Time

well with dislexia and learning disability… people usually need to read between the lines

so ill try to re explain…

  1. Read Home Assisant time
    2… Save Home assistant Time into the Nextion screen when using a battery
    3… put the time in fields “dtime” “hour” and “mintues”
  2. fix the error
    [12:30:59][W][homeassistant.sensor:015]: Can’t convert ‘12:31’ to number!
    [12:31:00][D][sensor:113]: ‘time2’: Sending state nan with 1 decimals of accuracy
    [12:31:00][W][homeassistant.sensor:015]: Can’t convert ‘12:31 PM’ to number!

5… everytime you turn on the esp device it re syncs the time…

so current code is and the current error logs are below… hope i explain it better … as i not sure how to explain it better as to me it make sense

and when i said time2

is you cant use the word “time” as its already used by esphome built and gives you error so to get “time” from home assistant i had to call it “time2”

i working on 2 different display projects 1 that uses dtime… and the other uses dtime and hours and minutes… as field names… but easier to give the simplest code and ill fiddle with it to work in the other project

esphome:
  name: moms-car-temp-2
# This is ESPHome configuration file. This goes to the esphome folder
# You need to provide your WiFi ssis/password and passwords for API and OTA (or put them in your esphome/secrets.yaml)

  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true
  
  manual_ip:
    static_ip: 192.168.0.227
    gateway: 192.168.0.1
    subnet: 255.255.255.0  

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Temperature-Display-2"
    password: "sWdknJXvOodX"
    
api:
  password: !secret api_password

ota:
  password: !secret ota_password




logger:
  baud_rate: 0     # Disable UART logging (pins GPIO1/3 are used for Nextion communication)

uart:
#  uart_id: uart_2
  rx_pin: GPIO3 #D1
  tx_pin: GPIO1 #D2
#  baud_rate: 115200
  baud_rate: 9600

time:
  - platform: sntp
    id: sntp_time



# Temperature Settings    

sensor:
  - platform: dht
    model: am2302
    pin: D7
    temperature:
      name: "Mom's Car Outside Temperature"
      id: outside_temperature
#      filters:
#        offset: 2
    humidity:
      name: "Mom's Car Outside Humidity"
      id: outside_humidity
    update_interval: 6s
    
 
  - platform: homeassistant   # Inside temperature
    id: display_time
    entity_id: sensor.display_time
    
  - platform: homeassistant   # Inside temperature
    id: time2
    entity_id: sensor.time

 




#globals:
# - id: first_page             # First page of the display?#
## - id: mom2
#   type: bool
#   restore_value: no
# - id: current_brightness             # Is display on?
#   type: float
#   restore_value: no
##   initial_value: '-1.0'
#   initial_value: '1.0'
 
#display:
#  - platform: nextion
#    id: teplomer
#    update_interval: 10s

display:
  - platform: nextion
    id: nextion1
#    uart_id: uart_2
    update_interval: 10s
    
    # ...
    lambda: |-
      auto time_text = id(sntp_time).now().strftime("%I:%M%P");
      auto hour_text = id(sntp_time).now().strftime("%I");
      auto minute_text = id(sntp_time).now().strftime("%M");
      //  it.set_component_text("dtime",time_text.c_str());
      it.set_component_text("dtime",id(display_time).state);
      it.set_component_text_printf("temperature", "%.1fc", id(outside_temperature).state);
INFO Reading configuration /config/esphome/moms-car-temp-2.yaml...
INFO Detected timezone 'America/Toronto'
INFO Generating C++ source...
INFO Compiling app...
Processing moms-car-temp-2 (board: d1_mini; framework: arduino; platform: platformio/espressif8266 @ 2.6.3)
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
Dependency Graph
|-- <ESP8266WiFi> 1.0
|-- <ESP8266mDNS> 1.2
Compiling /data/moms-car-temp-2/.pioenvs/moms-car-temp-2/src/main.cpp.o
/config/esphome/moms-car-temp-2.yaml: In lambda function:
/config/esphome/moms-car-temp-2.yaml:107:56: error: no matching function for call to 'esphome::nextion::Nextion::set_component_text(const char [6], float&)'
       it.set_component_text("dtime",id(display_time).state);
                                                        ^
/config/esphome/moms-car-temp-2.yaml:107:56: note: candidate is:
In file included from src/esphome/components/nextion/automation.h:3:0,
                 from src/esphome.h:29,
                 from src/main.cpp:3:
src/esphome/components/nextion/nextion.h:48:8: note: void esphome::nextion::Nextion::set_component_text(const char*, const char*)
   void set_component_text(const char *component, const char *text);
        ^
src/esphome/components/nextion/nextion.h:48:8: note:   no known conversion for argument 2 from 'float' to 'const char*'
*** [/data/moms-car-temp-2/.pioenvs/moms-car-temp-2/src/main.cpp.o] Error 1
========================== [FAILED] Took 1.42 seconds ==========================

and the code
auto time_text = id(sntp_time).now().strftime("%I:%M%P");
auto hour_text = id(sntp_time).now().strftime("%I");
auto minute_text = id(sntp_time).now().strftime("%M");

only works when Internet works… but since i dont live line a city like most people my internet is no where as good every one else… so i need the time to work from home assistant and sync to it everytime or as often as it needs i guess

my template.yaml in home assistant is


    - name: "Date and time"
      state: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%A %B %-d, %I:%M %p') }}"
      icon: "mdi:calendar-clock"

    - name: "Display Time"
      state: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%I:%M %p') }}"
      icon: "mdi:calendar-clock"

    - name: "Display Hour"
      state: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%I') }}"
      icon: "mdi:calendar-clock"

    - name: "Display Minutes"
      state: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%M') }}"
      icon: "mdi:calendar-clock"

so i wanna put

dtime = Display Time
hours = Dislplay Hours
minutes = Display Minutes

the 3 fields in nextion screen is
dtime,minutes, hours

but currently having issues with the dtime one as esphome cant convert sensor from text to text talks about it cant convert it to a number … which i dont want i just wanna display the state like 04:04pm i dont want to convert it

so i did find something i found it deep in someone else post

change sensor to
text_sensor:
text_sensor:

  • platform: homeassistant # Inside temperature
    id: display_time
    entity_id: sensor.display_time

so that part works i just cant get
this to work
it.set_component_text(“dtime”,id(display_time).state);

i cant get teh display_time state to send to dtime field

and save the dtime minutes and hours in the real time clock

but i gonna google more i think u gotta do the real clock stuff in the nextion screen program i appreciate the help so far.

Please post your code properly. You do so sometimes, but not others.

If you don’t have internet access don’t use sntp for time, use the home assistant time. Time — ESPHome

The error message tells you exactly where the error occurs - line 107 char 56.

ok so i wasnt able to figure how to take text_sensor info and send text using it.set_component_text
so for now i scarpped it…

so i got it coded in… and i changed the nextion screen… and i read you can upload it using home assistant… but its not working… i made a directory nextion in the www directory on home assistant and put car.tft in there…

i added tft_url: to the display section and the api section but it wont complie

so the code i have is

esphome:
  name: moms-car-temp-2
# This is ESPHome configuration file. This goes to the esphome folder
# You need to provide your WiFi ssis/password and passwords for API and OTA (or put them in your esphome/secrets.yaml)

  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: true


  manual_ip:
    static_ip: 192.168.0.227
    gateway: 192.168.0.1
    subnet: 255.255.255.0  

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Temperature-Display-2"
    password: "sWdknJXvOodX"
    
api:
  password: !secret api_password
  services:
    - service: update_nextion
      then:
        - lambda: 'id(nextion1)->upload_tft();'

ota:
  password: !secret ota_password




logger:
  baud_rate: 0     # Disable UART logging (pins GPIO1/3 are used for Nextion communication)

uart:
#  uart_id: uart_2
  rx_pin: GPIO3 #D1
  tx_pin: GPIO1 #D2
#  baud_rate: 115200
  baud_rate: 9600

time:
#  - platform: sntp
#    id: sntp_time
  - platform: homeassistant
    id: homeassistant_time


# Temperature Settings    

sensor:
  - platform: dht
    model: am2302
    pin: D7
    temperature:
      name: "Mom's Car Outside Temperature"
      id: outside_temperature
#      filters:
#        offset: 2
    humidity:
      name: "Mom's Car Outside Humidity"
      id: outside_humidity
    update_interval: 6s
    
 

    




display:
  - platform: nextion
    id: nextion1
    tft_url: http://192.168.0.12:80/local/nextion/car.tft  
#    uart_id: uart_2
    update_interval: 10s
    
    # ...
    lambda: |-
  

      auto time_text = id(homeassistant_time).now().strftime("%I:%M%P");
      auto hour_text = id(homeassistant_time).now().strftime("%I");
      auto minute_text = id(homeassistant_time).now().strftime("%I");


      it.set_component_text("dtime",time_text.c_str());
      it.set_component_text("minutes",minute_text.c_str());
      it.set_component_text("hour",hour_text.c_str());

      it.set_component_text_printf("temperature", "%.1fc", id(outside_temperature).state);
 

and error i getting is

Processing moms-car-temp-2 (board: d1_mini; framework: arduino; platform: platformio/espressif8266 @ 2.6.3)
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
Dependency Graph
|-- <ESP8266WiFi> 1.0
|-- <ESP8266mDNS> 1.2
Compiling /data/moms-car-temp-2/.pioenvs/moms-car-temp-2/src/esphome/components/nextion/nextion.cpp.o
Compiling /data/moms-car-temp-2/.pioenvs/moms-car-temp-2/src/esphome/components/nextion/nextion_commands.cpp.o
In file included from src/esphome/components/nextion/nextion.cpp:1:0:
src/esphome/components/nextion/nextion.h:15:31: fatal error: ESP8266HTTPClient.h: No such file or directory

***************************************************************************
* Looking for ESP8266HTTPClient.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:ESP8266HTTPClient.h"
* Web  > https://platformio.org/lib/search?query=header:ESP8266HTTPClient.h
*
***************************************************************************

 #include <ESP8266HTTPClient.h>
                               ^
compilation terminated.
*** [/data/moms-car-temp-2/.pioenvs/moms-car-temp-2/src/esphome/components/nextion/nextion.cpp.o] Error 1
In file included from src/esphome/components/nextion/nextion_commands.cpp:1:0:
src/esphome/components/nextion/nextion.h:15:31: fatal error: ESP8266HTTPClient.h: No such file or directory

***************************************************************************
* Looking for ESP8266HTTPClient.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:ESP8266HTTPClient.h"
* Web  > https://platformio.org/lib/search?query=header:ESP8266HTTPClient.h
*
***************************************************************************

 #include <ESP8266HTTPClient.h>
                               ^
compilation terminated.
*** [/data/moms-car-temp-2/.pioenvs/moms-car-temp-2/src/esphome/components/nextion/nextion_commands.cpp.o] Error 1
========================== [FAILED] Took 0.58 seconds ==========================

do i need to install something else? the esphome page didnt really say… and i guessed the port address of 80 i did try 8123 didnt work either

i tried googling but found 0 info on it
there is the ngnix underneath

do i need to install this ngnix to get this tft upload to work…

A header files is missing. Try a clean build.

Or upload your tft the normal way (via a sd card)

The port is 8123.

ok thanks… ya i ended up just using the sd card on the 4 wires
got the time to work

do you happen to know how to save now this time in the Real Time Clock i google and it talks about doing in the nextion but not from home assistant… but if you dont thats ok…
gonna google more least its working the way i wanted

This compile error can be solved by manually adding http_request: to your YAML file. See also: fatal error: ESP8266HTTPClient.h: No such file or directory · Issue #2721 · esphome/issues · GitHub

You are not making sense. You get the time from home assistant, why do you need to send it back to home assistant?