Esphome Nextion NSPanel Time update

Hello Every One and best wishes for 2022
I try to upadte my time on my NSPAnel But if the time and date is correct when the Nspanel boot
The values (Time and date) don’t update
The values of CO2 sensor and temperature from home assistant update
Can you check my Code and help me ?
Thanks

# Note: this may not be needed if the pull request has been merged.
# Check https://github.com/esphome/esphome/pull/2956 for current status.
external_components:
  - source: github://pr#2956
    components: [nextion]
    refresh: 1h

substitutions:
  node_name: nspanel-sdj
  device_name: NSPanel SdJ

esphome:
  name: $node_name
  comment: $device_name

esp32:
  board: esp32dev

# Enable logging
logger:

# Enable Home Assistant API

ota:
  password: "xxxxxxxxxxxxxxxx"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Nspanel-Sdj Fallback Hotspot"
    password: "xxxxxx"

captive_portal:



# Enable Home Assistant API
api:
  services:
    # Service to play a song
    - service: play_rtttl
      variables:
        song_str: string
      then:
        - rtttl.play:
            rtttl: !lambda 'return song_str;'
    - service: upload_tft
      then:
        - lambda: 'id(disp1)->upload_tft();'

# A reboot button is always useful
button:
  - platform: restart
    name: Restart $device_name

# Define some inputs
binary_sensor:
  - platform: gpio
    name: $device_name Left Button
    pin:
      number: 14
      inverted: true
    on_click:
      - switch.toggle: relay_1

  - platform: gpio
    name: $device_name Right Button
    pin:
      number: 27
      inverted: true
    on_click:
      - switch.toggle: relay_2
      
  - platform: nextion
    name: $device_name work Button
    page_id: 0
    component_id: 3

  - platform: nextion
    name: $device_name B1 Button
    page_id: 0
    component_id: 4

  - platform: nextion
    name: $device_name B2 Button
    page_id: 0
    component_id: 5      

sensor:
  - platform: wifi_signal
    name: $device_name WiFi Signal
    update_interval: 60s

  - platform: ntc
    id: temperature
    sensor: resistance_sensor
    calibration:
      b_constant: 3950
      reference_temperature: 25°C
      reference_resistance: 10kOhm
    name: $device_name Temperature

  - platform: resistance
    id: resistance_sensor
    sensor: ntc_source
    configuration: DOWNSTREAM
    resistor: 11.2kOhm

  - platform: adc
    id: ntc_source
    pin: 38
    update_interval: 10s
    attenuation: 11db

  # Grab current temperature from Home Assistant
  - platform: homeassistant
    id: current_temperature
    entity_id: sensor.sation_sdj_temperature_ambiante
    on_value:
      # Push it to the display
      then:
        - lambda: 'id(disp1).set_component_text_printf("temp", "%.1f ""\xb0""c", x);'

  - platform: homeassistant
    id: current_co2
    entity_id: sensor.station_sdj_co2_ambient
    on_value:
      # Push it to the display
      then:
         - lambda: 'id(disp1).set_component_text_printf("co2", "%2.0f ppm", x);'

# Define some outputs
switch:
  # The two relays
  - platform: gpio
    name: $device_name Relay 1
    id: relay_1
    pin:
      number: 22
  - platform: gpio
    name: $device_name Relay 2
    id: relay_2
    pin:
      number: 19

  # Pin 4 always needs to be on to power up the display
  - platform: gpio
    id: screen_power
    entity_category: config
    pin:
      number: 4
      inverted: true
    restore_mode: ALWAYS_ON

number:
  platform: template
  name: $device_name Brightness
  id: brightness
  entity_category: config
  unit_of_measurement: '%'
  min_value: 0
  max_value: 100
  step: 1
  initial_value: 30
  set_action:
    then:
      - lambda: 'id(disp1).set_backlight_brightness(x/100);'

# Configure the internal bleeper
output:
  - platform: ledc
    id: buzzer_out
    pin:
      number: 21

# Enable ringtone music support
rtttl:
  id: buzzer
  output: buzzer_out

# Configure UART for communicating with the screen
uart:
  id: tf_uart
  tx_pin: 16
  rx_pin: 17
  baud_rate: 115200
  
time:
  - platform: sntp
    id: sntp_time  
 

# Configure the screen itself
display:
  - platform: nextion
    id: disp1
    uart_id: tf_uart
    tft_url: http://10.0.0.40:8123/local/tft/nspanelsdj.tft
    update_interval: 5s
    # A little fun...
    on_setup:
      then:
        - number.set:
            id: brightness
            value: 40
        - lambda: |-
            id(disp1).set_component_text_printf(
              "temp", "%.1f ""\xb0""c", id(current_temperature).state
            );
            id(disp1).set_component_text_printf(
              "co2", "%2.0f ppm", id(current_co2).state
            );
            auto time_text = id(sntp_time).now().strftime("%H:%M");
            auto date_text = id(sntp_time).now().strftime("%d %b.");
            id(disp1).set_component_text("time", time_text.c_str());
            id(disp1).set_component_text("date", date_text.c_str());
            auto time = id(sntp_time).now();
        - rtttl.play: "twobits:d=4,o=5,b=220:c6,8g,8g,a,g,p,b,c6"

In the same scope i try to print the sunset time and sunrise time
I add

sun:
  latitude: 48.8584°
  longitude: 2.2945°
text_sensor:
  - platform: sun
    name: sunrise
    id: sunrise
    type: sunrise
    format: ("%H:%M")
  - platform: sun
    name: sunset
    type: sunset
    id: sunset 
    format: ("%H:%M")

In the code but i cant find how to print it !!!

1 Like

For the sunset and sunrise i find this solution

text_sensor:

  - platform: homeassistant
    id: sunrise
    entity_id: sensor.sunrise_time 
    on_value:
       then:
         - lambda: 'id(disp1).set_component_text_printf("sunrise","%s",id(sunrise).state.c_str());'    
  - platform: homeassistant
    id: sunset
    entity_id: sensor.sunset_time 
    on_value:
       then:
         - lambda: 'id(disp1).set_component_text_printf("sunset","%s",id(sunset).state.c_str());'  

and in display i have add after lambda

            id(disp1).set_component_text_printf("sunrise","%s",id(sunrise).state.c_str());
            id(disp1).set_component_text_printf("sunset","%s",id(sunset).state.c_str());

Hello!

I don’t use the same approach as you do, I use HomeAssistant time, not SNTP time, but for what its worth, here is “my” config, which (finally works). I need to credit @masto for 98% of the config here…

substitutions:
  node_name: entrance-nspanel-dev
  device_name: Entrance Wall Panel - Sonoff NSPanel

external_components:
  - source: github://pr#2956
    components: [nextion]
    refresh: 1h

esphome:
  name: $node_name
  comment: $device_name

esp32:
  board: esp32dev

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

logger:
ota:
time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      - seconds: 0
        minutes: '*'
        then:
          - lambda: |-
              auto time_text = id(homeassistant_time).now().strftime("%H:%M");
              id(disp1).set_component_text("time", time_text.c_str());


##################################################################################################################

api:
  services:
    - service: upload_tft
      then:
        - lambda: 'id(disp1)->upload_tft();'


binary_sensor:
  - platform: nextion
    name: $device_name Load UI Button
    page_id: 0
    component_id: 7
    on_click:
      - lambda: 'id(disp1)->upload_tft();'

  - platform: nextion
    name: $device_name God natt
    page_id: 0
    component_id: 10

sensor:
  - platform: wifi_signal
    name: $device_name WiFi Signal
    id: wifi_rssi
    update_interval: 30s
    on_value:
      then:
        - lambda: |-
            if (id(wifi_rssi).state >= -45)  {
              id(disp1).set_component_pic("wlan", 5);
              }
            if ((id(wifi_rssi).state < -46) && (id(wifi_rssi).state >= -50)) {
              id(disp1).set_component_pic("wlan", 4);
              }
            if ((id(wifi_rssi).state < -51) && (id(wifi_rssi).state >= -60)) {
              id(disp1).set_component_pic("wlan", 3);
              }
            if ((id(wifi_rssi).state < -61) && (id(wifi_rssi).state >= -70)) {
              id(disp1).set_component_pic("wlan", 2);
              }
            if (id(wifi_rssi).state < -71) {
              id(disp1).set_component_pic("wlan", 1);
              }

  - platform: ntc
    id: temperature
    sensor: resistance_sensor
    calibration:
      b_constant: 3950
      reference_temperature: 25°C
      reference_resistance: 10kOhm
    name: $device_name Temperature

  - platform: resistance
    id: resistance_sensor
    sensor: ntc_source
    configuration: DOWNSTREAM
    resistor: 11.2kOhm

  - platform: adc
    id: ntc_source
    pin: 38
    update_interval: 10s
    attenuation: 11db

  - platform: homeassistant
    id: current_temperature
    entity_id: sensor.outside_temperature
    on_value:
      then:
        - lambda: 'id(disp1).set_component_text_printf("temp", "%.1f ""\xb0""C", x);'



switch:
  # Pin 4 always needs to be on to power up the display
  - platform: gpio
    id: screen_power
    entity_category: config
    pin:
      number: 4
      inverted: true
    restore_mode: ALWAYS_ON

number:
  platform: template
  name: $device_name Brightness
  id: brightness
  entity_category: config
  unit_of_measurement: '%'
  min_value: 0
  max_value: 100
  step: 1
  initial_value: 100
  set_action:
    then:
      - lambda: 'id(disp1).set_backlight_brightness(x/100);'

uart:
  id: tf_uart
  tx_pin: 16
  rx_pin: 17
  baud_rate: 115200

display:
  - platform: nextion
    id: disp1
    uart_id: tf_uart
    tft_url: http://192.168.5.36:8123/local/nextion/entrance_nspanel_esphome_dev.tft
    on_setup:
      then:
        - number.set:
            id: brightness
            value: 100
        - lambda: |-
            id(disp1).set_component_text_printf(
              "temp", "%.1f ""\xb0""C", id(current_temperature).state
            );

Hi, on the last one with HA time, this makes my device reconnect every 60 seconds (on-the-dot). I raised this with masto but I think it is more esphome thing.

Hello, guys, please tell me how to display the ‘%’ character on the screen after displaying the value, maybe someone knows how to do this by some method, such as a screen for a character or creating an additional variable.
It should work, but unfortunately I either have an error, or “%” does not appear at all.
“Humidity: 53.4%”

My codes:

  • platform: homeassistant
    id: humi1
    entity_id: sensor.aq_thp_bedroom_kv_humidity
    on_value:
    then:
    - wait_until:
    switch.is_on: nextion_init
    - lambda: id(disp1).set_component_text_printf(“Home.text2”, “%.1f%”, id(humi1).state);

PS… Initially not finding a solution to my problem, I used the “Text” toolbox from the Nextion Editor.
But later I found a way out, you need to use ‘%%’:
- lambda: id(disp1).set_component_text_printf(“Home.text2”, “%.1f%%”, id(humi1).state);