LVGL slider goes all the way up when turn off the light

I have a slider in sensor part of Yaml:

- platform: homeassistant
    id: svijetlo1_dimmer
    entity_id: light.svijetlo_dnevna 
    attribute: brightness
    on_value:
      - lvgl.slider.update:
          id: dimmer_svijetlo1
          value: !lambda return x;

and other part under LVGL page:

- slider:
              id: dimmer_svijetlo1
              x: 30
              y: 130
              width: 50
              height: 280
              pad_all: 8
              min_value: 0
              max_value: 255
              on_release:
                - homeassistant.action:
                    action: light.turn_on
                    data:
                      entity_id: light.svijetlo_dnevna
                      brightness: !lambda return int(x);

Slider work normaly when I touch it or slide on it, light brightness is like I set slider. But when I move slider all the way down to turn off the light, it goes all the way up but light is off. On log there is this message:

[19:17:27.792][D][homeassistant.text_sensor:016]: 'light.svijetlo_dnevna': Got state 'off'
[19:17:27.795][D][text_sensor:119]: 'living_room_light1' >> 'off'
[19:17:27.804][W][homeassistant.sensor:015]: 'light.svijetlo_dnevna': Can't convert 'None' to number!
[19:17:27.807][D][sensor:129]: 'svijetlo1_dimmer' >> nan

Same thing happens when I turn off light in HA web or with any other controller:

[19:20:04.058][D][homeassistant.text_sensor:016]: 'light.svijetlo_dnevna': Got state 'off'
[19:20:04.062][D][text_sensor:119]: 'living_room_light1' >> 'off'
[19:20:04.071][W][homeassistant.sensor:015]: 'light.svijetlo_dnevna': Can't convert 'None' to number!
[19:20:04.075][D][sensor:129]: 'svijetlo1_dimmer' >> nan

HA treats the brightness as invalid when the light is off. You need to check that the brightness is a valid number before applying it to the slider - either just check the range or use something like std::isfinite().

@clydebarrow
Thanks, but I really don’t know how to apply that to my code. Can you help me with that or give me an a example from ESPhome.io?

For a non-AI answer try this - always sets 0 if unknown.

Afaik HA light doesn’t have brightness 0, only 1-255.

Or another way, though it would mean turning the light off would just leave the slider in its last position, not send it to 0.

sensor:
  - platform: homeassistant
    id: svijetlo1_dimmer
    entity_id: light.svijetlo_dnevna
    attribute: brightness
    filters:
      - filter_out: nan

That’s a gold nugget right there. I was always wondering how to do that. Just added it to my button library.

Your RP (real person) answer is way better then the AI answer.

1 Like

For this I have this in log:

mapping values are not allowed here
  in "/config/esphome/test-28-tft.yaml", line 185, column 49

I have tried this and it works…not so good. If I slide down to zero it turn off the light but when I turn on the light via something other then slider, slider don’t goes up, it stays at bottom position.

So what do you have at that line? I can’t see your screen…

Sorry for that, this is my code:

 - platform: homeassistant
    id: svijetlo3_dimmer
    entity_id: light.plafon_svijetlo_dnevna
    attribute: brightness
    on_value:
      - lvgl.slider.update:
          id: dimmer_svijetlo3
          value: !lambda return isfinite(x) ? x : 0;

Without the full context or line numbers it’s still hard to be sure, but it looks like you have additional indentation starting from the id: line The “id” should line up with the “platform”

This give me no solution. I get this:

Compiling .pioenvs/test-28-tft/src/main.cpp.o
/config/esphome/test-28-tft.yaml: In lambda function:
/config/esphome/test-28-tft.yaml:193:27: error: 'lv_obj_t' {aka 'struct _lv_obj_t'} has no member named 'update_value'
  193 |                 id(dimmer_svijetlo1).update_value(bri);
      |                           ^~~~~~~~~~~~
In file included from src/esphome/core/component.h:9,
                 from src/esphome/core/application.h:10,
                 from src/esphome/components/api/api_frame_helper.h:13,
                 from src/esphome/components/api/api_connection.h:5,
                 from src/esphome.h:3,
                 from src/main.cpp:3:
src/esphome/components/mipi_spi/mipi_spi.h: In instantiation of 'void esphome::mipi_spi::MipiSpi<BUFFERTYPE, BUFFERPIXEL, IS_BIG_ENDIAN, DISPLAYPIXEL, BUS_TYPE, WIDTH, HEIGHT, OFFSET_WIDTH, OFFSET_HEIGHT>::dump_config() [with BUFFERTYPE = short unsigned int; esphome::mipi_spi::PixelMode BUFFERPIXEL = esphome::mipi_spi::PIXEL_MODE_16; bool IS_BIG_ENDIAN = true; esphome::mipi_spi::PixelMode DISPLAYPIXEL = esphome::mipi_spi::PIXEL_MODE_16; esphome::mipi_spi::BusType BUS_TYPE = esphome::mipi_spi::BUS_TYPE_SINGLE; int WIDTH = 320; int HEIGHT = 480; int OFFSET_WIDTH = 0; int OFFSET_HEIGHT = 0]':
src/esphome/components/mipi_spi/mipi_spi.h:203:8:   required from here
  203 |   void dump_config() override {
      |        ^~~~~~~~~~~
src/esphome/components/mipi_spi/mipi_spi.h:228:67: warning: 'virtual std::string esphome::GPIOPin::dump_summary() const' is deprecated: Override dump_summary(char*, size_t) instead. Will be removed in 2026.7.0. [-Wdeprecated-declarations]
  228 |       esph_log_config(TAG, "  CS Pin: %s", this->cs_->dump_summary().c_str());
      |                                            ~~~~~~~~~~~~~~~~~~~~~~~^~
src/esphome/core/log.h:98:101: note: in definition of macro 'esph_log_config'
   98 |   ::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
      |                                                                                                     ^~~~~~~~~~~
In file included from src/esphome/core/hal.h:4,
                 from src/esphome/core/application.h:12:
src/esphome/core/gpio.h:145:20: note: declared here
  145 | inline std::string GPIOPin::dump_summary() const { return {}; }
      |                    ^~~~~~~
src/esphome/components/mipi_spi/mipi_spi.h:228:67: warning: 'virtual std::string esphome::GPIOPin::dump_summary() const' is deprecated: Override dump_summary(char*, size_t) instead. Will be removed in 2026.7.0. [-Wdeprecated-declarations]
  228 |       esph_log_config(TAG, "  CS Pin: %s", this->cs_->dump_summary().c_str());
      |                                            ~~~~~~~~~~~~~~~~~~~~~~~^~
src/esphome/core/log.h:98:101: note: in definition of macro 'esph_log_config'
   98 |   ::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
      |                                                                                                     ^~~~~~~~~~~
src/esphome/core/gpio.h:145:20: note: declared here
  145 | inline std::string GPIOPin::dump_summary() const { return {}; }
      |                    ^~~~~~~
src/esphome/components/mipi_spi/mipi_spi.h:230:77: warning: 'virtual std::string esphome::GPIOPin::dump_summary() const' is deprecated: Override dump_summary(char*, size_t) instead. Will be removed in 2026.7.0. [-Wdeprecated-declarations]
  230 |       esph_log_config(TAG, "  Reset Pin: %s", this->reset_pin_->dump_summary().c_str());
      |                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
src/esphome/core/log.h:98:101: note: in definition of macro 'esph_log_config'
   98 |   ::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
      |                                                                                                     ^~~~~~~~~~~
src/esphome/core/gpio.h:145:20: note: declared here
  145 | inline std::string GPIOPin::dump_summary() const { return {}; }
      |                    ^~~~~~~
src/esphome/components/mipi_spi/mipi_spi.h:230:77: warning: 'virtual std::string esphome::GPIOPin::dump_summary() const' is deprecated: Override dump_summary(char*, size_t) instead. Will be removed in 2026.7.0. [-Wdeprecated-declarations]
  230 |       esph_log_config(TAG, "  Reset Pin: %s", this->reset_pin_->dump_summary().c_str());
      |                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
src/esphome/core/log.h:98:101: note: in definition of macro 'esph_log_config'
   98 |   ::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
      |                                                                                                     ^~~~~~~~~~~
src/esphome/core/gpio.h:145:20: note: declared here
  145 | inline std::string GPIOPin::dump_summary() const { return {}; }
      |                    ^~~~~~~
src/esphome/components/mipi_spi/mipi_spi.h:232:71: warning: 'virtual std::string esphome::GPIOPin::dump_summary() const' is deprecated: Override dump_summary(char*, size_t) instead. Will be removed in 2026.7.0. [-Wdeprecated-declarations]
  232 |       esph_log_config(TAG, "  DC Pin: %s", this->dc_pin_->dump_summary().c_str());
      |                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
src/esphome/core/log.h:98:101: note: in definition of macro 'esph_log_config'
   98 |   ::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
      |                                                                                                     ^~~~~~~~~~~
src/esphome/core/gpio.h:145:20: note: declared here
  145 | inline std::string GPIOPin::dump_summary() const { return {}; }
      |                    ^~~~~~~
src/esphome/components/mipi_spi/mipi_spi.h:232:71: warning: 'virtual std::string esphome::GPIOPin::dump_summary() const' is deprecated: Override dump_summary(char*, size_t) instead. Will be removed in 2026.7.0. [-Wdeprecated-declarations]
  232 |       esph_log_config(TAG, "  DC Pin: %s", this->dc_pin_->dump_summary().c_str());
      |                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
src/esphome/core/log.h:98:101: note: in definition of macro 'esph_log_config'
   98 |   ::esphome::esp_log_printf_(ESPHOME_LOG_LEVEL_CONFIG, tag, __LINE__, ESPHOME_LOG_FORMAT(format), ##__VA_ARGS__)
      |                                                                                                     ^~~~~~~~~~~
src/esphome/core/gpio.h:145:20: note: declared here
  145 | inline std::string GPIOPin::dump_summary() const { return {}; }
      |                    ^~~~~~~
*** [.pioenvs/test-28-tft/src/main.cpp.o] Error 1
========================= [FAILED] Took 42.20 seconds =========================

Yeah, AI slop. Thought I’d see how it did, wasn’t great - typically it looks entirely plausible, but it’s just a bad guess.

This was a useful question and I’ve created a better solution that will appear in the next release - the sensor clamp filter has been updated to correctly clamp non-finite numbers, so you can use this right now (removing the external_components block after the next full release of ESPHome.)

external_components:
  - source: github://pr#13457
    components: [sensor]
    refresh: 1h

sensor:
  - platform: homeassistant
    id: svijetlo1_dimmer
    entity_id: light.svijetlo_dnevna
    attribute: brightness
    filters:
      - clamp:
          min_value: 0 # Clamps NaN
          max_value: 255 # Optional
    on_value:
      - lvgl.slider.update:
          id: dimmer_svijetlo1
          value: !lambda return x;

and it will clamp NAN to 0 which is what you want since that means the light is off.

1 Like

I have only change content right of the value: and I have this message in the log.
It was this and I get no error:

value: !lambda return x;

and now is this:

value: !lambda return isfinite(x) ? x : 0;

Now it works like it should!! Thanks @clydebarrow for your time and patience!! :heart:

1 Like

@clydebarrow

Today I try to update my ESP device and have message that this Github is removed. I have erase the External components from my Yaml and update the device but now is like before - if light is off then slider is all the way up.