Sonoff NSPanel by ITead - Smart Scene Wall Switch based on ESP32 and custom Nextion Touch Screen Panel Display (non-Pro variant)

Hi, I hope I have not made a mistake in posting here, before installing the NSpanel on the wall, I wanted to try it by attaching it to the socket, my doubt however is this, I live in Italy and the sockets are not polarized, so phase and neutral can be reversed, do i have to check the wires first before connecting it to the socket via a plug? Thanks in advance

yes i would check the polarity 1st.

Thanks, I did as you said everything ok

I have not checked polarity once.
I assumed it would work either way.
So either I have been lucky or it works both ways.

1 Like

with AC power it doesnā€™t really matter on the workings as the ac changes polarity each cycle anyway. but if the device has any ac protection this is normally on the positive terminal before sensitive components

Same here. Live/neutral are almost never relevant (other than to differentiate the line that can kill you from the one that canā€™t!).

Iā€™m sorry I may not have followed what youā€™re trying to do so apologies if this isnā€™t relevant. I have several pages that contain different information. This information doesnā€™t always seem to update properly so I update the information every 1s. However, I only do it for the information for a page when itā€™s is on display. Moreover, when my display dims to nothing it goes to an ā€œemptyā€ page so there are no updates happening.

Iā€™m using the following to differentiate my pages (you can see itā€™s under development):

# Get the current page and update it
  - platform: nextion
    id: disp1_current_page
    variable_name: dp
    update_interval: 1s
    on_value:
      if:
        condition: 
          lambda: 'return x == 4;'
        then:
#          - logger.log: "**** GAPS **** Sensor current page 4"
          - script.execute: refresh
        else:
          if:
            condition: 
              lambda: 'return x == 0;'
            then:
#              - logger.log: "**** GAPS **** Sensor current page 0"

so Iā€™m interested, because Iā€™m having problems updating information. already being able to update will help me because I didnā€™t know how to do it.

and i am working on a similar problem.

When I put a switch from on to off or vice versa from home assistant, everything works, the screen is awake, but the relay-1 is activated well, but the touch button which must be in the on position does not update .

the command:
update_interval: 1s
solved the problem =)

would it be possible to share your yalm configuration as well as the HMI?

I would like to understand how you store the current page in memory.

Here it is. Itā€™s a work in progress but does do the main things I need for now. I might try and put together a short video showing how my nspanel works.

# Set some variables for convenience
substitutions:
  node_name: nspanel
  device_name: NSPanel
  my_thermostat: climate.nnn
  outside_thermometer: sensor.thermometer_1_temperature
  TV_on_icon: '0'
  TV_off_icon: '1'
  FloorLight_on_icon: '12'
  FloorLight_off_icon: '11'
  Heating_on_icon: '13'
  Heating_off_icon: '14'
  HallLight_page_id: '3'
  HallLight_entity_id: light.tz3000_middle_light_color_on_off
  PorchLight_page_id: '5'
  PorchLight_entity_id: light.porch_light_middle_light_color_on_off
  Boiler_entity_id: switch.tz3000_middle_on_off
  Boiler_on_icon: '13'
  Boiler_off_icon: '14'

# 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

esphome:
  name: $node_name
  comment: $device_name

esp32:
  board: esp32dev
#  framework:
#    type: arduino

# Enable logging
logger:
  baud_rate: 0
  level: DEBUG
  logs:
    sensor: WARN
    resistance: WARN
    text_sensor: WARN
    ntc: WARN

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    password: !secret wifi_password

captive_portal:

# Enable wireless updates
ota:

# 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 to upload the tft file
    - service: upload_tft
      then:
        - lambda: 'id(disp1)->upload_tft();'
    # Send an arbitrary command
    - service: send_command
      variables:
        cmd: string
      then:
         - lambda: 'id(disp1).send_command_printf("%s", cmd.c_str());'

### Time Configuration
time:
  ## Time component that fetches time from Home Assistant and updates the display once a second and when the display is ready at bootup (and time is available).
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      - seconds: /1 #0
#        minutes: /1
        then:
          - lambda: 'id(disp1).set_component_text_printf("time", "%02i:%02i", id(homeassistant_time).now().hour, id(homeassistant_time).now().minute);'
          - lambda: 'id(disp1).set_component_text_printf("date", "%i/%02i/%02i", id(homeassistant_time).now().day_of_month, id(homeassistant_time).now().month, id(homeassistant_time).now().year);'
#          - script.execute: refresh
    on_time_sync:
        then:
          - wait_until:
              switch.is_on: nextion_init
#          - script.execute: refresh
#          - logger.log: "**** GAPS **** Time sync run"
          - lambda: 'id(disp1).set_component_text_printf("time", "%02i:%02i", id(homeassistant_time).now().hour, id(homeassistant_time).now().minute);'
          - lambda: 'id(disp1).set_component_text_printf("date", "%i/%02i/%02i", id(homeassistant_time).now().day_of_month, id(homeassistant_time).now().month, id(homeassistant_time).now().year);'#

# A reboot button is always useful
button:
  - platform: restart
    name: Restart $device_name
    device_class: restart
  # easy tft update
  - platform: template
    name: $device_name TFT upload
    device_class: update
    on_press:
      - lambda: 'id(disp1)->upload_tft();'

# Define some inputs
binary_sensor:
  - platform: gpio
    name: $device_name Left Button
    pin:
      number: 14
      inverted: true
    on_press:
      - lambda: 'id(disp1).send_command_printf("page Home");'
      - lambda: 'id(disp1).send_command_printf("Sleep.en=1");'
      - lambda: 'id(disp1).send_command_printf("dim=100");'
    on_click:
      then:
        - homeassistant.service:
            service: light.toggle
            data:
              entity_id: light.porch_light_middle_light_color_on_off
    on_multi_click:
      - timing:
          - ON for at least 2s
        then:
          - lambda: 'id(disp1).send_command_printf("page PorchLight");'

  - platform: gpio
    name: $device_name Right Button
    pin:
      number: 27
      inverted: true
    on_press:
      then:
        - lambda: 'id(disp1).send_command_printf("page Home");'
        - lambda: 'id(disp1).send_command_printf("Sleep.en=1");'
        - lambda: 'id(disp1).send_command_printf("dim=100");'
    on_click:
      then:
        - homeassistant.service:
            service: light.toggle
            data:
              entity_id: light.tz3000_middle_light_color_on_off
    on_multi_click:
      - timing:
          - ON for at least 2s
        then:
          - lambda: 'id(disp1).send_command_printf("page lights");'

  - platform: nextion
    name: $device_name Hot water boost
    page_id: 0
    component_id: 2
    on_multi_click:
      - timing:
          - ON for at least 2s
        then:
          - rtttl.play: "twobits:d=4,o=0,b=6000:a0"
    on_double_click:
      then:
        - homeassistant.service:
            service: switch.turn_off
            data:
              entity_id: $Boiler_entity_id

  - platform: nextion
    name: $device_name TV OnOff
    page_id: 0
    component_id: 3
    on_release:
      then:
        - homeassistant.service:
            service: switch.toggle
            data:
              entity_id: switch.tz3000_middle_on_off

  - platform: nextion
    name: $device_name floor light OnOff
    page_id: 0
    component_id: 12
    on_release:
      then:
        - homeassistant.service:
            service: switch.toggle
            data:
              entity_id: switch.tz3000_middle_on_off

 # Thermostat page buttons
  - platform: nextion
    name: $device_name Minus Button
    page_id: 4
    component_id: 1
    internal: true
    on_release:
      then:
        - rtttl.play: "twobits:d=4,o=0,b=6000:a0"
        - homeassistant.service:
            service: climate.set_temperature
            data_template:
              entity_id: $my_thermostat
              temperature: '{{ (state_attr("$my_thermostat", "temperature")) - 0.5 }}'
    
  - platform: nextion
    name: $device_name Plus Button
    page_id: 4
    component_id: 2
    internal: true
    on_release:
      then:
        - rtttl.play: "twobits:d=4,o=0,b=6000:a0"
        - logger.log: "Plus button released"
        - homeassistant.service:
            service: climate.set_temperature
            data_template:
              entity_id: $my_thermostat
              temperature: '{{ (state_attr("$my_thermostat", "temperature")) + 0.5 }}'
    
  - platform: nextion
    name: $device_name Heat Button
    page_id: 4
    component_id: 6
    on_release:
      then:
        - if:
            condition:
              lambda: 'return id(current_mode).state == "0";'
            then:
              - rtttl.play: "twobits:d=4,o=0,b=6000:a0"
              - homeassistant.service:
                  service: climate.set_hvac_mode
                  data:
                    entity_id: $my_thermostat
                    hvac_mode: heat
            else:
              - rtttl.play: "twobits:d=4,o=0,b=6000:a0"
              - homeassistant.service:
                  service: climate.set_hvac_mode
                  data:
                    entity_id: $my_thermostat
                    hvac_mode: 'off'

  - platform: nextion
    name: $device_name Hall Light Brightness Slider
    page_id: $HallLight_page_id
    component_id: 3
    internal: true
    on_release:
      then:
        - lambda: !lambda |-
            id(HallLight_brightness_value).update();

  - platform: nextion
    name: $device_name Hall Light Color Temp Slider
    page_id: $HallLight_page_id
    component_id: 8
    internal: true
    on_release:
      then:
        - lambda: !lambda |-
            id(HallLight_color_temp_value).update();

  - platform: nextion
    name: $device_name Porch Light Brightness Slider
    page_id: $PorchLight_page_id
    component_id: 3
    internal: true
    on_release:
      then:
        - lambda: !lambda |-
            id(HallLight_brightness_value).update();

  - platform: nextion
    name: $device_name Hall Light Color Temp Slider
    page_id: $PorchLight_page_id
    component_id: 8
    internal: true
    on_release:
      then:
        - lambda: !lambda |-
            id(HallLight_color_temp_value).update();

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

# Get the current page and update it
  - platform: nextion
    id: disp1_current_page
    variable_name: dp
    update_interval: 1s
    on_value:
      if:
        condition: 
          lambda: 'return x == 4;'
        then:
#          - logger.log: "**** GAPS **** Sensor current page 4"
          - script.execute: refresh
        else:
          if:
            condition: 
              lambda: 'return x == 0;'
            then:
#              - logger.log: "**** GAPS **** Sensor current page 0"

  ## Temperature Sensor Components
  # Internal temperature sensor, adc value
  # Does not get exposed to HA
  - platform: adc
    id: ntc_source
    pin: 38
    update_interval: 10s
    attenuation: 11db

  # Internal temperature sensor, adc reading converted to resistance (calculation) 
  # Does not get exposed to HA
  - platform: resistance
    id: resistance_sensor
    sensor: ntc_source
    configuration: DOWNSTREAM
    resistor: 11.2kOhm

  # Internal temperature sensor, resistance to temperature (calculation)
  - platform: ntc
    id: temperature
    sensor: resistance_sensor
    calibration:
      b_constant: 3950
      reference_temperature: 25Ā°C
      reference_resistance: 10kOhm
    name: $device_name Temperature
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: id(disp1).set_component_text_printf("temp", "%.1f ""\xb0""C", id(outside_temperature).state);
    
  - platform: homeassistant
    id: target_temperature
    entity_id: $my_thermostat
    attribute: temperature
    on_value:
      then:
#        - logger.log: "**** GAPS **** Platform target_temperature setting"
        - if:
            condition:
              lambda: 'return id(current_mode).state == "0";'
            then:
              - lambda: 'id(disp1).set_component_text_printf("targetTemp", "Off");'
            else:
              - lambda: 'id(disp1).set_component_text_printf("targetTemp", "%.1f""\xb0""C", x);'
        
  - platform: homeassistant
    id: thermostat_temperature
    entity_id: $my_thermostat
    attribute: current_temperature
    on_value:
      then:
        - lambda: 'id(disp1).set_component_text_printf("ThermostatTemp", "%.1f""\xb0""C", x);'
        
  - platform: homeassistant
    id: outside_temperature
    entity_id: $outside_thermometer
    on_value:
      then:
        - lambda: 'id(disp1).set_component_text_printf("temp", "%.1f""\xb0""C", x);'

  - platform: homeassistant
    id: HallLight_current_brightness
    entity_id: $HallLight_entity_id
    attribute: brightness
    on_value:
      then:
        - lambda: 'id(disp1).send_command_printf("brightnessVal.val=%i", (int)x);'

  - platform: homeassistant
    id: HallLight_current_color_temp
    entity_id: $HallLight_entity_id
    attribute: color_temp
    on_value:
      then:
        - lambda: 'id(disp1).send_command_printf("tempSlider.val=%i", (int)x);'
        
  - platform: nextion
    id: HallLight_brightness_value
    component_name: brightnessVal
    on_value:
      then:
        - homeassistant.service:
            service: light.turn_on
            data:
              entity_id: $HallLight_entity_id
              brightness: !lambda 'return (int)x;'
            
  - platform: nextion
    id: HallLight_color_temp_value
    component_name: tempSlider
    on_value:
      then:
        - homeassistant.service:
            service: light.turn_on
            data:
              entity_id: $HallLight_entity_id
              color_temp: !lambda 'return (int)x;'

  - platform: homeassistant
    id: PorchLight_current_brightness
    entity_id: $PorchLight_entity_id
    attribute: brightness
    on_value:
      then:
        - lambda: 'id(disp1).send_command_printf("brightnessVal.val=%i", (int)x);'

  - platform: homeassistant
    id: PorchLight_current_color_temp
    entity_id: $PorchLight_entity_id
    attribute: color_temp
    on_value:
      then:
        - lambda: 'id(disp1).send_command_printf("tempSlider.val=%i", (int)x);'
        
  - platform: nextion
    id: PorchLight_brightness_value
    component_name: brightnessVal
    on_value:
      then:
        - homeassistant.service:
            service: light.turn_on
            data:
              entity_id: $PorchLight_entity_id
              brightness: !lambda 'return (int)x;'
            
  - platform: nextion
    id: PorchLight_color_temp_value
    component_name: tempSlider
    on_value:
      then:
        - homeassistant.service:
            service: light.turn_on
            data:
              entity_id: $PorchLight_entity_id
              color_temp: !lambda 'return (int)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
    
  ## Logical Switches
  # Basically a global variable to keep track on whether the Nextion display is ready or not. Delays initial info from HA to the display.
  # Does not get exposed to HA
  - platform: template
    id: nextion_init
    entity_category: config
    restore_state: false
    assumed_state: off
    optimistic: true
      
text_sensor:

  - platform: homeassistant
    id: current_mode
    entity_id: $my_thermostat
    filters:
      - map:
        - off -> 0
        - heat -> heatActive
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
#        - logger.log: "**** GAPS **** Homeassistant heatActive multiple"
        - if:
            condition:
              lambda: 'return x == "0";'
            then:
              - lambda: 'id(disp1).send_command_printf("thermostat.heat.pic=10");'
              - lambda: 'id(disp1).set_component_text_printf("targetTemp", "Off");'
            else:
              - lambda: 'id(disp1).send_command_printf("thermostat.heat.pic=9");'
              - lambda: 'id(disp1).set_component_text_printf("targetTemp", "%.1f""\xb0""C", id(target_temperature).state);'

# TV setup
  - platform: homeassistant
    id: TV
    entity_id: switch.tz3000_middle_on_off
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int BGimg = $TV_on_icon;
            if (id(TV).state == "off") {
              BGimg=$TV_off_icon;
            }
            id(disp1).send_command_printf("Home.TVOnOff.pic=%i", BGimg);
        # Show button indicator background colour from state of TV
# Floor setup
  - platform: homeassistant
    id: FloorLight
    entity_id: switch.tz3000_middle_on_off
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int BGimg = $FloorLight_on_icon;
            if (id(FloorLight).state == "off") {
              BGimg=$FloorLight_off_icon;
            }
            id(disp1).send_command_printf("Home.FloorLight.pic=%i", BGimg);
        # Show button indicator background colour from state of light
# Boiler setup
  - platform: homeassistant
    id: Boiler
    entity_id: $Boiler_entity_id
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int BGimg = $Boiler_on_icon;
            if (id(Boiler).state == "off") {
              BGimg=$Boiler_off_icon;
            }
            id(disp1).send_command_printf("Home.HWBoost.pic=%i", BGimg);
        # Show button indicator background colour from state of TV

  - platform: homeassistant
    id: HallLight
    entity_id: light.tz3000_middle_light_color_on_off
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int BGcol = 65504;
            if (id(HallLight).state == "off") {
              BGcol=46527;
            }
            id(disp1).send_command_printf("Home.RightButtonTxt.bco=%i", BGcol);
# Show button indicator background colour from state of light
  - platform: homeassistant
    id: PorchLight
    entity_id: light.porch_light_middle_light_color_on_off
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int BGcol = 65504;
            if (id(PorchLight).state == "off") {
              BGcol=46527;
            }
            id(disp1).send_command_printf("Home.LeftButtonTxt.bco=%i", BGcol);

script:
  - id: refresh
    then:
      - lambda: |-
          if (id(current_mode).state == "0") {
            id(disp1).set_component_text_printf("targetTemp", "Off");
            id(disp1).send_command_printf("thermostat.heat.pic=10");
          } else {
            id(disp1).set_component_text_printf("targetTemp", "%.1f""\xb0""C", id(target_temperature).state);
            id(disp1).send_command_printf("thermostat.heat.pic=9");
          }
          id(disp1).set_component_text_printf("currentTemp", "%.1f""\xb0""C", id(temperature).state);
          id(disp1).set_component_text_printf("ThermostatTemp", "%.1f""\xb0""C", id(thermostat_temperature).state);

# 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

# Configure the screen itself
display:
  - platform: nextion
    id: disp1
    uart_id: tf_uart
    tft_url: http://192.x.y.z:8123/local/tft/nspanel.tft
    auto_wake_on_touch: true
#    update_interval: 2s
#    on_page_change:
#      - from:
#        to: 4
#        then:
#          - logger.log: "**** GAPS **** On_page_change refresh"
#          - script.execute: refresh
    # A little fun...
    on_setup:
      then:
#        - number.set:
#            id: brightness
#            value: 30
        - wait_until:
            api.connected
        - switch.template.publish:
            id: nextion_init
            state: on
        - script.execute: refresh

#        - lambda: |-
#            id(disp1).set_component_text_printf(
#              "temp", "%.1f ""\xb0""F", id(current_temperature).state
#            );
#        - rtttl.play: "twobits:d=4,o=5,b=220:c6,8g,8g,a,g,p,b,c6"
        - rtttl.play: "twobits:d=4,o=5,b=220:c6,8g,8g,a,g"

Thanks very much ! I use your yalm configurations a lot and it really helps me a lot.

I have a question that your experience would have already answered. When changing an image for a text button, for example, I have a not pretty effect of a bad refresh as these screens do not have the possibility of taking images with a transparent background, what is the best way to change the image of a button in a fluid way?

your yalm file is incredibly clean and comprehensive, Iā€™m impressed!

1 Like

Blush!

My understanding is that transparent images arenā€™t supported in Nextion. Iā€™ve therefore been crude and used a fixed background colour and simple changes (eg I use the mdi icons for lights and exported two versions with the fixed background and black foreground for off and yellow for on).

# NSPANEL YAML BY LOVEJOY777
# Version 012-03-2022 github: https://github.com/lovejoy777
#
# CREDIT & THANKS TO THE FOLLOWING.
# INITIAL WORK: Masto github: https://github.com/
# FULLY CUSTOMIZED FIRMWARE: Marcfager github: https://github.com/marcfager/nspanel-mf.

# global varibles
globals:
  - id: current_target_temp_val
    type: float
    restore_value: true
    initial_value: '12'
    
  - id: current_lightslider_val
    type: int
    restore_value: true
    initial_value: '60'
    
  - id: brightness_lounge_val
    type: int
    restore_value: true
    initial_value: '100'
    
# SUBSTITUTIONS ##
substitutions:
  node_name: nspanel
  device_name: NSPanel
  target_lights_01: light.lounge_spots_val

# EXTERNAL COMPONENTS ##
# Functionality for the Nextion display
external_components:
  - source: github://pr#2956
    components: [nextion]
    refresh: 1h

## ESP HOME CONFIG ##
esphome:
  name: $node_name
  comment: $device_name

# TYPE OF BOARD
esp32:
  board: esp32dev

## WIFI SETUP ##
# Wifi settings. Add these to your secrets.yaml. fast_connect must be true for a hidden ssid.
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
#  fast_connect: !secret wifi_fast_connect

captive_portal:

# OTA (Over the air updates) password. Add to your secrets.yaml
ota:
  password: !secret ota_password

## LOGGER ##
# Logger. Disable the temperature sensor etc. to focus on the HMI development
logger:
  baud_rate: 0
  level: DEBUG
  logs:
    sensor: WARN
    resistance: WARN
    text_sensor: WARN
    ntc: WARN
    
## API SERVICES ##
# API. Add api_pwd to your secrets.yaml.
api:
  services:
      # SERVICE TO PLAY A RTTTL TONE.
    - service: play_rtttl
      variables:
        song_str: string
      then:
        - rtttl.play:
            rtttl: !lambda 'return song_str;'
      # SERVICE TO UPDATE THE HMI FILE.
    - service: upload_tft
      then:
        - lambda: 'id(disp1)->upload_tft();'

      # SERVICE TO WAKE THE SCREEN.
    - service: wakeup_screen
      then:
        - lambda: |-
            id(disp1).send_command_printf("page 0");
            
## BUZZER ##
# CONFIGURE INTERNAL BUZZER.
output:
    # BUZZER FOR PLAYING RINGTONES.
  - platform: ledc
    id: buzzer_out
    pin:
      number: 21

# ENABLE RINGTONE MUSIC SUPPORT.
rtttl:
  id: buzzer
  output: buzzer_out

## TIME ##
# Time component that fetches time from Home Assistant and updates the display once a minute and when the display is ready at bootup (and time is available).
time:
  - platform: homeassistant
    id: homeassistant_time
    on_time:
      - seconds: 0
        minutes: /1
        then:
          - lambda: id(disp1).set_component_text_printf("time", "%02i:%02i", id(homeassistant_time).now().hour, id(homeassistant_time).now().minute);
          - lambda: id(disp1).set_component_text_printf("date", "%i-%02i-%02i", id(homeassistant_time).now().day_of_month, id(homeassistant_time).now().month, id(homeassistant_time).now().year);
    on_time_sync:
        then:
          - wait_until:
              switch.is_on: nextion_init
          - lambda: id(disp1).set_component_text_printf("time", "%02i:%02i", id(homeassistant_time).now().hour, id(homeassistant_time).now().minute);
          - lambda: id(disp1).set_component_text_printf("date", "%i-%02i-%02i", id(homeassistant_time).now().day_of_month, id(homeassistant_time).now().month, id(homeassistant_time).now().year);


# BINARY SENSOR ################################################################
binary_sensor:

    #### SLIDER TESTING
#  - platform: nextion
#    name: $device_name Brightness Slider
#    page_id: 2
#    component_id: 24
#    internal: true
#    on_release:
#      then:
#        - lambda: !lambda |-
#            id(brightness_value).update();

    # LEFT BUTTON BELOW DISPLAY.
  - platform: gpio
    name: $device_name Left Button
    pin:
      number: 14
      inverted: true
    on_click:
      - switch.toggle: relay_1

    # RIGHT BUTTON BELOW DISPLAY.
  - platform: gpio
    name: $device_name Right Button
    pin:
      number: 27
      inverted: true
    on_click:
      - switch.toggle: relay_2

    # THERMOSTAT UP BUTTON INCREASES CLIMATE TARGET TEMPERATURE IN HA.
  - platform: nextion
    name: $device_name Target Up
    page_id: 7
    component_id: 12
    on_click:
#      - rtttl.play: "twobits:d=4,o=0,b=6000:a0"
      - homeassistant.service:
          service: climate.set_temperature
          data_template:
            entity_id: climate.lounge
            temperature: !lambda "return id(current_target_temp_val) + 1;"

    # THERMOSTAT DOWN BUTTON DECREASES CLIMATE TARGET TEMPERATURE IN HA. 
  - platform: nextion
    name: $device_name Target Dn
    page_id: 7
    component_id: 13
    on_click:
#      - rtttl.play: "twobits:d=4,o=0,b=6000:a0"
      - homeassistant.service:
          service: climate.set_temperature
          data_template:
            entity_id: climate.lounge
            temperature: !lambda "return id(current_target_temp_val) - 1;"
            
    # LIGHTS SETS LIGHTS BRIGHTNESS FROM GLOBAL VARIBLE.
  - platform: nextion
    name: $device_name light_slider
    page_id: 2
    component_id: 24
    on_release:
      - delay: 0.2s
      - homeassistant.service:
          service: light.turn_on
          data:
            entity_id: light.lounge_spots
            brightness: !lambda "return id(current_lightslider_val);"
      - globals.set:
            id: current_lightslider_val
            value: !lambda "return id(current_lightslider_val);" 
            
    # HEATING STATE BINARY FROM HASSIO
  - platform: homeassistant
    entity_id: binary_sensor.heating_state
    name: "Heating State Binary"
    id: heating_state_binary
   # internal: true
   
    # HEATING STATE BINARY FROM HASSIO
  - platform: homeassistant
    entity_id: binary_sensor.water_state
    name: "Water State Binary"
    id: water_state_binary

## SETTINGS PAGE BINARY SENSORS
    # SETTINGS AUTO HOME HOTSPOT BANARY SENSOR.
  - platform: nextion
    name: $device_name Auto Home
    page_id: 6
    component_id: 14
    on_click:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.autohome_light
            
    # SETTINGS AUTO HOME STATE FROM HASSIO
  - platform: homeassistant
    entity_id: binary_sensor.nspanel_auto_home
    name: "Auto Home State Binary"
    id: auto_home_state_binary
    # internal: true
            
    # SETTINGS AUTO DIM HOTSPOT BANARY SENSOR.
  - platform: nextion
    name: $device_name Auto Dim
    page_id: 6
    component_id: 15
    on_click:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.autodim_light
            
    # SETTINGS AUTO DIM STATE FROM HASSIO
  - platform: homeassistant
    entity_id: binary_sensor.nspanel_auto_dim
    name: "Auto Dim State Binary"
    id: auto_dim_state_binary
   # internal: true
            
    # SETTINGS AUTO SCREENSAVER HOTSPOT BANARY SENSOR.
  - platform: nextion
    name: $device_name Auto Screensaver
    page_id: 6
    component_id: 16
    on_click:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.autoscreensaver_light
            
    # SETTINGS AUTO SCREENSAVER STATE FROM HASSIO
  - platform: homeassistant
    entity_id: binary_sensor.nspanel_auto_screensaver
    name: "Auto Screensaver State Binary"
    id: auto_screensaver_state_binary
   # internal: true

## MUSIC PAGE BINARY SENSORS
    # MUSIC PLAY/PAUSE HOTSPOT
  - platform: nextion
    name: $device_name Music play pause
    page_id: 1
    component_id: 1
    on_click:
      - homeassistant.service:
          service: media_player.media_play_pause
          data:
            entity_id: media_player.kitchen_speaker
            
    # MUSIC VOLUME DOWN HOTSPOT
  - platform: nextion
    name: $device_name Music voldown
    page_id: 1
    component_id: 5
    on_click:
      - homeassistant.service:
          service: media_player.volume_down
          data:
            entity_id: media_player.kitchen_speaker
            
    # MUSIC VOLUME UP HOTSPOT
  - platform: nextion
    name: $device_name Music volup
    page_id: 1
    component_id: 6
    on_click:
      - homeassistant.service:
          service: media_player.volume_up
          data:
            entity_id: media_player.kitchen_speaker
            
#   MUSIC PLAYLIST 1 HOTSPOT
  - platform: nextion
    name: $device_name Music Playlist 1 button
    page_id: 1
    component_id: 7
    on_click:
      - homeassistant.service:
          service: homeassistant.turn_on
          data:
            entity_id: input_boolean.play_smoothfm
      - delay: 0.2s
      - homeassistant.service:
          service: homeassistant.turn_off
          data:
            entity_id: input_boolean.play_smoothfm
            
    # MUSIC PLAYLIST 2 HOTSPOT   
  - platform: nextion
    name: $device_name Music Playlist 2 button
    page_id: 1
    component_id: 8
    on_click:
      - homeassistant.service:
          service: homeassistant.turn_on
          data:
            entity_id: input_boolean.play_bbc1
      - delay: 0.2s
      - homeassistant.service:
          service: homeassistant.turn_off
          data:
            entity_id: input_boolean.play_bbc1
            
    # MUSIC PLAYLIST 3 HOTSPOT 
  - platform: nextion
    name: $device_name Music Playlist 3 button
    page_id: 1
    component_id: 9
    on_click:
      - homeassistant.service:
          service: homeassistant.turn_on
          data:
            entity_id: input_boolean.play_bbc2
      - delay: 0.2s
      - homeassistant.service:
          service: homeassistant.turn_off
          data:
            entity_id: input_boolean.play_bbc2
            
    # MUSIC PLAYLIST 4 HOTSPOT 
  - platform: nextion
    name: $device_name Music Playlist 4 button
    page_id: 1
    component_id: 10
    on_click:
      - homeassistant.service:
          service: homeassistant.turn_on
          data:
            entity_id: input_boolean.play_bbc4
      - delay: 0.2s
      - homeassistant.service:
          service: homeassistant.turn_off
          data:
            entity_id: input_boolean.play_bbc4

## LIGHTS PAGE BINARY SENSORS
    # LIGHT 1 HOTSPOT 
  - platform: nextion
    name: $device_name Light 0 button
    page_id: 2
    component_id: 13
    on_click:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.kitchen_spots

    # LIGHT 2 HOTSPOT
  - platform: nextion
    name: $device_name Light 1 button
    page_id: 2
    component_id: 14
    on_click:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.unit_lights
            
    # LIGHT 3 HOTSPOT
  - platform: nextion
    name: $device_name Light 2 button
    page_id: 2
    component_id: 19
    on_click:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.unit_lights_2

    # LIGHT 4 HOTSPOT
  - platform: nextion
    name: $device_name Light 3 button
    page_id: 2
    component_id: 20
    on_click:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.lounge_spots

    # LIGHT 5 HOTSPOT
  - platform: nextion
    name: $device_name Light 4 button
    page_id: 2
    component_id: 21
    on_click:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.lounge_lamp_1

    # LIGHT 6 HOTSPOT
  - platform: nextion
    name: $device_name Light 5 button
    page_id: 2
    component_id: 22
    on_click:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.lounge_lamp_2

## SENSORS #####################################################################
sensor:


    # INTERNAL TEMPERATUE SENSOR, ADC VALUE.
  - platform: adc
    id: ntc_source
    pin: 38
    update_interval: 10s
    attenuation: 11db

    # INTERNAL TEMPERATUE SENSOR, adc reading converted to resistance (calculation) 
  - platform: resistance
    id: resistance_sensor
    sensor: ntc_source
    configuration: DOWNSTREAM
    resistor: 11.2kOhm

    # INTERNAL TEMPERATUE SENSOR, resistance to temperature (calculation)
  - platform: ntc
    id: temperature
    sensor: resistance_sensor
    calibration:
      b_constant: 3950
      reference_temperature: 25Ā°C
      reference_resistance: 10kOhm
    name: $device_name Temperature

    # UPTIME FOR DISPLAY. Also updates a human readable one (further down)
  - platform: uptime
    name: $device_name Uptime Sensor
    id: uptime_sensor
    update_interval: 60s
    on_raw_value:
      then:
        - text_sensor.template.publish:
            id: uptime_human
            state: !lambda |-
                int seconds = round(id(uptime_sensor).raw_state);
                int days = seconds / (24 * 3600);
                seconds = seconds % (24 * 3600);
                int hours = seconds / 3600;
                seconds = seconds % 3600;
                int minutes = seconds /  60;
                seconds = seconds % 60;
                return (
                  (days ? to_string(days) + "d " : "") +
                  (hours ? to_string(hours) + "h " : "") +
                  (minutes ? to_string(minutes) + "m " : "") +
                  (to_string(seconds) + "s")
                ).c_str();

    # WIFI SIGNAL STRENGTH SENSOR.
  - platform: wifi_signal
    name: $device_name WiFi Signal Sensor
    update_interval: 60s
   
    # CURRENT PAGE DISPLAYED ON HMI.
  - platform: nextion
    name: $device_name Current display page
    id: disp1_current_page
    variable_name: dp
    update_interval: 1s

    # HOME TEMPERATURE NOW FROM HA.
  - platform: homeassistant
    id: current_temperature
    entity_id: weather.home
    attribute: temperature
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: id(disp1).set_component_text_printf("Home.temp", "%.1f", id(current_temperature).state);

    # HOME TEMPERATURE FEELS LIKE FROM HA.
  - platform: homeassistant
    id: temp_feelslike
    entity_id: sensor.home_temperature_feels_like
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: id(disp1).set_component_text_printf("Home.tempfl", "%.1f", id(temp_feelslike).state);
        
    # THERMOSTAT KITCHEN NSPANEL TEMPERATURE FROM HA.
  - platform: homeassistant
    id: kitchen_temperature
    entity_id: sensor.nspanel_temperature
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: id(disp1).set_component_text_printf("Thermostat.cctemp", "%.0f", id(kitchen_temperature).state);
        
    # THERMOSTAT HOT WATER TEMP FROM HA.
  - platform: homeassistant
    id: hotwater_temperature
    entity_id: sensor.water_temp
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: id(disp1).set_component_text_printf("Thermostat.hwtemp_txt", "%.0f", id(hotwater_temperature).state);
        
    # THERMOSTAT CLIMATE TARGET TEMPERATURE FROM HA.
  - platform: homeassistant
    id: target_temperature
    entity_id: sensor.target_temp_2
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
            
        - globals.set:
            id: current_target_temp_val
            value: !lambda 'return int(x);'    
            
        - lambda: id(disp1).set_component_text_printf("Thermostat.cttemp", "%.0f", id(current_target_temp_val));
        
  # LIGHTS GET VALUE FROM SLIDER AND SET GLOBAL VARIBLE.
  - platform: nextion
    id: lightslider
    name: $device_name lightslider
    variable_name: lightslider
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: id(disp1).set_component_text_printf("Lights.t1", "%i", id(current_lightslider_val));
        - globals.set:
            id: current_lightslider_val
            value: !lambda 'return int(x);'
            
 #  Lights LOUNGE SPOTS BRIGHTNESS FROM HA.
#  - platform: homeassistant
#    id: brightness_lounge_spots_val
#    entity_id: sensor.brightness_lounge_spots
#    on_value:
#      then:
#        - wait_until:
#            switch.is_on: nextion_init
#            
#        - globals.set:
#            id: brightness_lounge_val
#            value: !lambda 'return int(x);' 
#         
#        - lambda: id(disp1).set_component_text_printf("Lights.t1", "%i", id(brightness_lounge_val));
         
    ## SLIDER TESTING
    
#  - platform: homeassistant
#    id: current_brightness
#    entity_id: $target_lights_01
#    attribute: brightness
#    on_value:
#      then:
#        - lambda: 'id(disp1).send_command_printf("Lights.lightslider.val=%i", (int)x);'

        
#  - platform: nextion
#    id: brightness_value
#    variable_name: lightslider
#    on_value:
#      then:
#        - lambda: 'id(disp1).set_component_text_printf("Lights.t1", "%.0f", id(brightness_value).state);'
#        - homeassistant.service:
#            service: light.turn_on
#            data:
#              entity_id: $target_lights_01
#              brightness: !lambda 'return int(x);'


## TEXT SENSOR ################################################################################################################
text_sensor:

    # ESPHome version used to compile the app
  - platform: version
    name: $device_name ESPHome Version
    
    # SUN SENSOR FROM HA.
  - platform: homeassistant
    id: sun_sun
    entity_id: sun.sun

    # Uptime in a human readable form. Updated from the uptime sensor
  - platform: template
    name: $device_name Uptime Human Readable
    id: uptime_human
    icon: mdi:clock-start

    # HOME/THERMOSTAT HEATING ICONS.
  - platform: homeassistant
    entity_id: binary_sensor.heating_state
    name: "Heating State"
    id: heating_state
    # internal: true
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol=52;
            if (id(heating_state).state == "on") {
              symbol=53;
            }
            id(disp1).send_command_printf("Home.heating_symbol.pic=%i", symbol);
            id(disp1).send_command_printf("Thermostat.heating_symbol.pic=%i", symbol);

    # HOME/THERMOSTAT HOT WATER ICONS.
  - platform: homeassistant
    entity_id: binary_sensor.water_state
    name: "Water State"
    id: water_state
    # internal: true
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol=54; // 5 is a empty box for btn_b1.
            if (id(water_state).state == "on") {
              symbol=55;
            }
            id(disp1).send_command_printf("Home.water_symbol.pic=%i", symbol);
            id(disp1).send_command_printf("Thermostat.water_symbol.pic=%i", symbol);
            
    # SETTINGS AUTO HOME ICONS.
  - platform: homeassistant
    id: auto_home_state
    entity_id: binary_sensor.auto_home_state
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol=60; // on state pic for btn_b1.
            if (id(auto_home_state).state == "on") {
              symbol=61;
            }
            id(disp1).send_command_printf("Settings.p0.pic=%i", symbol);
            
    # SETTINGS AUTO DIM ICONS.
  - platform: homeassistant
    id: auto_dim_state
    entity_id: binary_sensor.auto_dim_state
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol=62; // on state pic for btn_b1.
            if (id(auto_dim_state).state == "on") {
              symbol=63;
            }
            id(disp1).send_command_printf("Settings.p1.pic=%i", symbol);
            
    # SETTINGS AUTO SCREENSAVER ICONS.
  - platform: homeassistant
    id: auto_screensaver_state
    entity_id: binary_sensor.auto_screensaver_state
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol=64; // on state pic for btn_b1.
            if (id(auto_screensaver_state).state == "on") {
              symbol=65;
            }
            id(disp1).send_command_printf("Settings.p2.pic=%i", symbol);
            
    # THERMOSTAT HVAC_ACTION FROM HA ICONS.
  - platform: homeassistant
    entity_id: climate.lounge
    attribute: hvac_action
    id: mode_state
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: id(disp1).set_component_text_printf("Thermostat.state_txt", "%s", id(mode_state).state.c_str());

    # MUSIC PLAYER PLAYING ARTIST FROM HA
  - platform: homeassistant
    id: music_artist
    entity_id: media_player.kitchen_speaker
    attribute: media_artist
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: id(disp1).set_component_text_printf("Music.music_sn", "%s", id(music_artist).state.c_str());

    # MUSIC PLAYER PLAYING TITLE FROM HA
  - platform: homeassistant
    id: music_title
    entity_id: media_player.kitchen_speaker
    attribute: media_title
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: id(disp1).set_component_text_printf("Music.music_an", "%s", id(music_title).state.c_str());

## WEATHER ##
    # WEATHER SYMBOL FROM HA WEATHER ENTITY.
  - platform: homeassistant
    id: weather_symbol
    entity_id: weather.home
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol=8; // 5 is a empty box.
            if (id(weather_symbol).state == "clear-night") {
              symbol=9;
            } else if (id(weather_symbol).state == "cloudy") {
              symbol=10;
              if (id(sun_sun).state == "below_horizon") {
                symbol=9;
              }
            } else if (id(weather_symbol).state == "fog") {
              symbol=11;
            } else if (id(weather_symbol).state == "hail" || id(weather_symbol).state == "snowy-rainy") {
              symbol=15;
            } else if (id(weather_symbol).state == "lightning") {
              symbol=14;
            } else if (id(weather_symbol).state == "lightning-rainy" || id(weather_symbol).state == "exceptional") {
              symbol=21;
              if (id(sun_sun).state == "below_horizon") {
                symbol=20;
              }
            } else if (id(weather_symbol).state == "partlycloudy") {
              symbol=16;
              if (id(sun_sun).state == "below_horizon") {
                symbol=17;
              }
            } else if (id(weather_symbol).state == "pouring") {
              symbol=12;
            } else if (id(weather_symbol).state == "rainy") {
              symbol=18;
            } else if (id(weather_symbol).state == "snowy") {
              symbol=13;
            } else if (id(weather_symbol).state == "sunny") {
              symbol=22;
              if (id(sun_sun).state == "below_horizon") {
                symbol = 9;
              }
            } else if (id(weather_symbol).state == "windy" || id(weather_symbol).state == "windy-variant") {
              symbol=23;              
            }
            id(disp1).send_command_printf("Home.weather_symbol.pic=%i", symbol);
            
## LIGHTS ##
    # LIGHTS SETUP LIGHT 1.
  - platform: homeassistant
    id: first
    entity_id: light.kitchen_spots
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol = 73;
            if (id(first).state == "on") {
              symbol=74;
            }
            id(disp1).send_command_printf("Lights.light0.pic=%i", symbol);

     # LIGHTS SEND IMAGE LIGHT 1.
#  - platform: homeassistant
#    id: first_name
#    entity_id: light.kitchen_spots
#    attribute: friendly_name
#    on_value:
#      then:
#        - wait_until:
#            switch.is_on: nextion_init
#        - lambda: id(disp1).set_component_text_printf("Lights.light0t", "%s", id(first_name).state.c_str());

    # LIGHTS SETUP LIGHT 2.
  - platform: homeassistant
    id: second
    entity_id: light.unit_lights
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol = 75;
            if (id(second).state == "on") {
              symbol=76;
            }
            id(disp1).send_command_printf("Lights.light1.pic=%i", symbol);

    # LIGHTS SETUP LIGHT 3.
  - platform: homeassistant
    id: third
    entity_id: light.unit_lights_2
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol = 77;
            if (id(third).state == "on") {
              symbol=78;
            }
            id(disp1).send_command_printf("Lights.light2.pic=%i", symbol);

    # LIGHTS SETUP LIGHT 4.
  - platform: homeassistant
    id: fourth
    entity_id: light.lounge_spots
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol = 79;
            if (id(fourth).state == "on") {
              symbol=80;
            }
            id(disp1).send_command_printf("Lights.light3.pic=%i", symbol);

    # LIGHTS SETUP LIGHT 5.
  - platform: homeassistant
    id: fith
    entity_id: light.lounge_lamp_1
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol = 81;
            if (id(fith).state == "on") {
              symbol=82;
            }
            id(disp1).send_command_printf("Lights.light4.pic=%i", symbol);

    # LIGHTS SETUP LIGHT 6.
  - platform: homeassistant
    id: sixth
    entity_id: light.lounge_lamp_2
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol = 83;
            if (id(sixth).state == "on") {
              symbol=84;
            }
            id(disp1).send_command_printf("Lights.light5.pic=%i", symbol);

## SWITCHES ####################################################################
switch:
    # global variable to keep track on whether the Nextion display is ready or not. Delays initial info from HA to the display.
  - platform: template
    name: $device_name Nextion inited
    id: nextion_init
    entity_category: config
    restore_state: false
    assumed_state: off
    optimistic: true
    
    # UPDATE TFT DISPLAY.  
  - platform: template
    name: Update TFT display
    id: tft_update
    turn_on_action:
      - delay: 16ms
      - lambda: id(disp1).upload_tft();
      - switch.turn_off: tft_update
      
    # PHYSICAL SWITCH 1.
  - platform: gpio
    name: $device_name Relay 1
    id: relay_1
    pin:
      number: 22

    # PHYSICAL SWITCH 2.
  - platform: gpio
    name: $device_name Relay 2
    id: relay_2
    pin:
      number: 19

    # Turn screen power on/off. Easy way to configure the screen power control, but this should not be used from HA, as all components must be re-initialized afterwards. For lights, names of lights etc. this practically means that the state must change once to happen.
  - platform: gpio
    name: $device_name Screen Power
    id: screen_power
    entity_category: config
    pin:
      number: 4
      inverted: true
    restore_mode: ALWAYS_ON
    
    # Disable Go to Home page-function on screen
  - platform: template
    device_class: switch
    name: $device_name Disable go to home
    id: disp1_disgth
    restore_state: true
    assumed_state: off
    optimistic: true
    on_turn_on:
      - lambda: id(disp1).send_command_printf("Home.disgth.val=1");
    on_turn_off:
      - lambda: id(disp1).send_command_printf("Home.disgth.val=0");

    # Disable Dim-down-function on screen
  - platform: template
    device_class: switch
    name: $device_name Disable dim-down
    id: disp1_disdim
    restore_state: true
    assumed_state: off
    optimistic: true
    on_turn_on:
      - lambda: id(disp1).send_command_printf("Home.disdim.val=1");
    on_turn_off:
      - lambda: id(disp1).send_command_printf("Home.disdim.val=0");

    # Disable Screensaver-function on screen
  - platform: template
    device_class: switch
    name: $device_name Disable screensaver
    id: disp1_dissleep
    restore_state: true
    assumed_state: on
    optimistic: true
    on_turn_on:
      - lambda: id(disp1).send_command_printf("Home.dissleep.val=1");
    on_turn_off:
      - lambda: id(disp1).send_command_printf("Home.dissleep.val=0");
      
## NUMBERS #####################################################################
number:
    # SCREEN BRIGHTNESS.
  - 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);'
        - lambda: 'id(disp1).send_command_printf("Home.brightness.val=%i", int(x));'

    # SCREEN BRIGHTNESS DIMMED DOWN.
  - platform: template
    name: $device_name Brightness dimdown
    id: brightness_dim
    entity_category: config
    unit_of_measurement: '%'
    min_value: 0
    max_value: 100
    step: 1
    initial_value: 1
    set_action:
      then:
        - lambda: 'id(disp1).send_command_printf("Home.brightdd.val=%i", int(x));'
        
#script:
#  - id: refresh
#    then:
#      - lambda: |-
#          id(disp1).send_command_printf("brightnessVal.val=%i", (int)id(current_brightness).state);
  
## UART ########################################################################
# UART FOR NEXTION DISPLAY
uart:
  tx_pin: 16
  rx_pin: 17
  baud_rate: 115200
  id: tf_uart
  
## DISPLAY #####################################################################
# CONFIGURATION FOR NEXTION SCREEN.
display:
  - platform: nextion
    id: disp1
    uart_id: tf_uart
    tft_url: !secret nextion_update_url
#    on_wake:
#      then:
#        - number.set:
#            id: brightness
#            value: 60
#        - script.execute: refresh
    on_setup:
      then:
        - number.set:
            id: brightness
            value: 30
        - lambda: id(disp1).send_command_printf("page 3");
        - wait_until:
            api.connected
        - switch.template.publish:
            id: nextion_init
            state: on
        - lambda: id(disp1).send_command_printf("page 0");
        - rtttl.play: "two short:d=4,o=5,b=100:16e6,16e6"
# end

heres mine if it helps. i change images the same way. 1 for on with edited bg and 1 for off with same bg.

I will post a full video later. but hopefully you get the idea.

video on how to create state images in Gimp.

3 Likes

Iā€™ve put together a video showing the main features of how my nspanel works. The only thing not shown is it going off after 45 seconds (you wouldnā€™t want to wait for that would you!). Probably wonā€™t win an Oscar but I live in hope!
My NSPanel

2 Likes

This is my board. I am making an imitation of the homekit system.
I use node red and mqtt.



6 Likes

Just be aware that we only have around 68% (my limit is 68.4%) memory spare for the esp yaml code (when compiled), anything above that size makes things go crazy and then you have to reconnect to your PC and flash manually again. my yaml I posted above is my limit, I have to delete things if I want to add anything else. I wanted to do more :(ā€¦

How do you get those figures?

when it compiles in the esphome log, just before it uploads. you have a second to cancel the update if you have your finger on the cancel button :rofl:. Iā€™ve just deleted my settings screen so i can improve my dimming function. after all i can make those settings with automations within HA

but honestly, i donā€™t think Iā€™ll ever be satisfied :rofl: a great little project :grin:

2 Likes

Thatā€™s great. Have you posted the code somewhere?