Blackymas / NSPanel_HA_Blueprint

This allows you to configure your complete NSPanel via Blueprint with UI and without changing anything in the code
1.2k stars 225 forks source link

The Display Brightness option encountered an unexpected error `Bug` #2091

Closed ChaoticAlex closed 3 weeks ago

ChaoticAlex commented 3 weeks ago

TFT Version

4.3.5 and later

ESPHome Version

4.3.5 and later

Blueprint Version

4.3.5 and later

Panel Model

EU

What is the bug?

When the page is in screensaver mode, if you open the screen, it will immediately revert back to screensaver mode. Additionally, because I'm using the 'Set display as a light' option, when the 'Display Brightness Sleep' value is set to 0, the screen will automatically turn off after opening it. When the 'Display Brightness Sleep' is set to a non-zero value, the screen brightness will apply the 'Display Brightness Sleep' value. Only when the screen is clicked will the brightness momentarily apply the 'Display Brightness' value, after which it will immediately revert back to the value set for 'Display Brightness Sleep'.

Steps to Reproduce

1.Use the "Set display as a light" setting. 2.Wait for the interface to change to screensaver after the update. 3.Try different combinations of Display Brightness Sleep and Display Brightness values and operate them. 4.Observe the changes in screen brightness.

Your Panel's YAML

substitutions:
  device_name: nspanel
  friendly_name: "XXXX"
  wifi_ssid: !secret wifi_ssid
  wifi_password: !secret wifi_password

wifi:
  networks:
    - id: !extend wifi_default
      hidden: true
  fast_connect: true
  manual_ip:
    static_ip: XXXX
    gateway: XXXX
    subnet: XXXX
    dns1: XXXX
    dns2: XXXX

##Add the display as a light in Home Assistant start
light:
  # Add the display as a light in Home Assistant
  - name: Display
    id: display_light
    icon: mdi:tablet-dashboard
    platform: monochromatic
    output: display_output
    default_transition_length: 0s
    on_turn_on:
      then:
        - lambda: |-
            ESP_LOGD("light.display_light", "Turn-on");
            if (current_page->state == "screensaver") disp1->goto_page(wakeup_page_name->state.c_str());
            timer_reset_all->execute();
    on_turn_off:
      then:
        - lambda: |-
            ESP_LOGD("light.display_light", "Turn-off");
            goto_page->execute("screensaver");

output:
  # Output required by `display_light` to send the commands to Nextion
  - id: display_output
    platform: template
    type: float
    write_action:
      - lambda: |-
          ESP_LOGV("output.display_output", "state: %f", state);
          uint8_t current_brightness = int(round(display_light->current_values.is_on() ? (display_light->current_values.get_brightness() * 100.0f) : 0.0));
          ESP_LOGV("output.display_output", "current_brightness: %i%%", current_brightness);
          set_brightness->execute(current_brightness);

script:
  # Updates the existing `page_changed` script to update the `display_light` status when a page changes
  - id: !extend page_changed
    then:
      - lambda: |-
          ESP_LOGD("script.page_changed(custom)", "page: %s", current_page->state.c_str());
          ESP_LOGV("script.page_changed(custom)", "is_on(): %s", display_light->current_values.is_on() ? "True" : "False");
          if (current_page->state == "screensaver" and display_light->current_values.is_on()) {
            auto call = display_light->turn_off();
            call.perform();
          } else if (current_page->state != "screensaver" and (not display_light->current_values.is_on())) {
            auto call = display_light->turn_on();
            call.perform();
          }

  # Updates the existing `set_brightness` script to update the `display_light` status when a new brightness level is set
  - id: !extend set_brightness
    then:
      - lambda: |-
          ESP_LOGD("script.set_brightness(custom)", "brightness: %.0f%%", brightness);
          uint8_t current_light_brightness = int(round(display_light->current_values.is_on() ? (display_light->current_values.get_brightness() * 100.0f) : 0.0));
          ESP_LOGV("script.set_brightness(custom)", "current_light_brightness: %i%%", current_light_brightness);
          if (brightness != current_light_brightness) {
            if (current_page->state != "screensaver" and brightness > 0) {
              auto call = display_light->turn_on();
              call.set_brightness(static_cast<float>(current_brightness->state) / 100.0f);
              call.perform();
            } else if (display_light->current_values.is_on()) {
              auto call = display_light->turn_off();
              call.set_brightness(0);
              call.perform();
            }
          }
##Add the display as a light in Home Assistant end

##### My customization - End #####

# Core and optional configurations
packages:
  remote_package:
    url: https://github.com/Blackymas/NSPanel_HA_Blueprint
    ref: main
    refresh: 300s
    files:
      - nspanel_esphome.yaml # Basic NSPanel package
      # Optional packages for advanced features and other add-ons
      # - esphome/nspanel_esphome_addon_ble_tracker.yaml # BLE Tracker add-on package
      # - esphome/nspanel_esphome_addon_bluetooth_proxy.yaml
      # - esphome/nspanel_esphome_addon_climate_cool.yaml
      # - esphome/nspanel_esphome_addon_climate_heat.yaml
      # - esphome/nspanel_esphome_addon_climate_dual.yaml

esp32:
  framework:
    type: esp-idf

ESPHome Logs

No response

Home Assistant Logs

No response

edwardtfn commented 3 weeks ago

Could you please try this?

##Add the display as a light in Home Assistant start
light:
  # Add the display as a light in Home Assistant
  - name: Display
    id: display_light
    icon: mdi:tablet-dashboard
    platform: monochromatic
    output: display_output
    default_transition_length: 0s
    on_turn_on:
      then:
        - lambda: |-
            ESP_LOGD("light.display_light", "Turn-on");
            if (current_page->state == "screensaver") disp1->goto_page(wakeup_page_name->state.c_str());
            timer_reset_all->execute();
    on_turn_off:
      then:
        - lambda: |-
            ESP_LOGD("light.display_light", "Turn-off");
            goto_page->execute("screensaver");

output:
  # Output required by `display_light` to send the commands to Nextion
  - id: display_output
    platform: template
    type: float
    write_action:
      - lambda: |-
          ESP_LOGV("output.display_output", "state:              %f", state);
          const uint8_t current_brightness = int(round(display_light->current_values.is_on() ? (display_light->current_values.get_brightness() * 100.0f) : 0.0));
          ESP_LOGV("output.display_output", "current_brightness: %i%%", current_brightness);
          set_brightness->execute(current_brightness);

script:
  # Updates the existing `page_changed` script to update the `display_light` status when a page changes
  - id: !extend page_changed
    then:
      - lambda: |-
          ESP_LOGV("script.page_changed(custom)", "page:    %s", current_page->state.c_str());
          ESP_LOGV("script.page_changed(custom)", "is_on(): %s", YESNO(display_light->current_values.is_on()));
          if (current_page->state == "screensaver" and display_light->current_values.is_on()) {
            ESP_LOGV("script.page_changed(custom)", "Turning off light.");
            auto call = display_light->turn_off();
            call.perform();
          } else if (current_page->state != "screensaver" and (not display_light->current_values.is_on())) {
            ESP_LOGV("script.page_changed(custom)", "Turning on light.");
            auto call = display_light->turn_on();
            call.perform();
          }

  # Updates the existing `set_brightness` script to update the `display_light` status when a new brightness level is set
  - id: !extend set_brightness
    then:
      - lambda: |-
          ESP_LOGV("script.set_brightness(custom)", "brightness:               %.0f%%", brightness);
          uint8_t current_light_brightness = int(round(display_light->current_values.is_on() ? (display_light->current_values.get_brightness() * 100.0f) : 0.0));
          ESP_LOGV("script.set_brightness(custom)", "current_light_brightness: %i%%", current_light_brightness);
          if (brightness != current_light_brightness) {
            if (current_page->state != "screensaver" and brightness > 0) {
              ESP_LOGV("script.set_brightness(custom)", "Adjusting light brightness to %.3f%%", brightness / 100.0f);
              auto call = display_light->turn_on();
              call.set_brightness(brightness / 100.0f);
              call.perform();
            } else if (display_light->current_values.is_on()) {
              ESP_LOGV("script.set_brightness(custom)", "Turning off light.");
              auto call = display_light->turn_off();
              call.set_brightness(0);
              call.perform();
            }
          }

If this works I will update the docs.

ChaoticAlex commented 3 weeks ago

Could you please try this?

##Add the display as a light in Home Assistant start
light:
  # Add the display as a light in Home Assistant
  - name: Display
    id: display_light
    icon: mdi:tablet-dashboard
    platform: monochromatic
    output: display_output
    default_transition_length: 0s
    on_turn_on:
      then:
        - lambda: |-
            ESP_LOGD("light.display_light", "Turn-on");
            if (current_page->state == "screensaver") disp1->goto_page(wakeup_page_name->state.c_str());
            timer_reset_all->execute();
    on_turn_off:
      then:
        - lambda: |-
            ESP_LOGD("light.display_light", "Turn-off");
            goto_page->execute("screensaver");

output:
  # Output required by `display_light` to send the commands to Nextion
  - id: display_output
    platform: template
    type: float
    write_action:
      - lambda: |-
          ESP_LOGV("output.display_output", "state:              %f", state);
          const uint8_t current_brightness = int(round(display_light->current_values.is_on() ? (display_light->current_values.get_brightness() * 100.0f) : 0.0));
          ESP_LOGV("output.display_output", "current_brightness: %i%%", current_brightness);
          set_brightness->execute(current_brightness);

script:
  # Updates the existing `page_changed` script to update the `display_light` status when a page changes
  - id: !extend page_changed
    then:
      - lambda: |-
          ESP_LOGV("script.page_changed(custom)", "page:    %s", current_page->state.c_str());
          ESP_LOGV("script.page_changed(custom)", "is_on(): %s", YESNO(display_light->current_values.is_on()));
          if (current_page->state == "screensaver" and display_light->current_values.is_on()) {
            ESP_LOGV("script.page_changed(custom)", "Turning off light.");
            auto call = display_light->turn_off();
            call.perform();
          } else if (current_page->state != "screensaver" and (not display_light->current_values.is_on())) {
            ESP_LOGV("script.page_changed(custom)", "Turning on light.");
            auto call = display_light->turn_on();
            call.perform();
          }

  # Updates the existing `set_brightness` script to update the `display_light` status when a new brightness level is set
  - id: !extend set_brightness
    then:
      - lambda: |-
          ESP_LOGV("script.set_brightness(custom)", "brightness:               %.0f%%", brightness);
          uint8_t current_light_brightness = int(round(display_light->current_values.is_on() ? (display_light->current_values.get_brightness() * 100.0f) : 0.0));
          ESP_LOGV("script.set_brightness(custom)", "current_light_brightness: %i%%", current_light_brightness);
          if (brightness != current_light_brightness) {
            if (current_page->state != "screensaver" and brightness > 0) {
              ESP_LOGV("script.set_brightness(custom)", "Adjusting light brightness to %.3f%%", brightness / 100.0f);
              auto call = display_light->turn_on();
              call.set_brightness(brightness / 100.0f);
              call.perform();
            } else if (display_light->current_values.is_on()) {
              ESP_LOGV("script.set_brightness(custom)", "Turning off light.");
              auto call = display_light->turn_off();
              call.set_brightness(0);
              call.perform();
            }
          }

If this works I will update the docs.

Sorry for the late reply. I spent the afternoon testing the new configuration on my NSPlane, and they are working very well. Thanks! I think this Issue can be closed.