Switch gpio + lvgl button

Hello I am trying to configure a switch with lvgl interface but I don’t understand how to do lvgl widget update.
below you can find the switch and the lvgl part.
Can someone help me.
Marco

switch:
 - platform: gpio
    name: Velocita' 1
    id: velocita_1
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin A1
      number: 1
      mode:
        output: true
      inverted: true
lvgl:
  style_definitions:
    - id: roboto15_style
      text_font: roboto15_font
  displays: my_display
  touchscreens: my_touch
  buffer_size: 10%
  color_depth: 16
  pages:
# LVGL setup with a single button
    
    - id: main_page
      widgets:
       - button:                
            id: velocita_1_ventola
            x: 20
            y: 100
            width: 200
            height: 50
            radius: 10
            bg_color: my_white
            text_color: my_green
            checkable: true
            on_value:
              then:
                - switch.turn_on: velocita_1
            

I have a whole library for building button panels on ESPHome here.

You can take a look at this and see how my LVGL buttons can follow the state of Home Assistant buttons.

To use a “local” button create something that updates LVGL on state changes like this

binary_sensor:
  - id: widget_sensor
    platform: homeassistant
    entity_id: ${entity_id}
    publish_initial_state: true
    on_state:
      - if:
          condition:
            - binary_sensor.is_on: widget_sensor
          then:
            - lvgl.widget.update:
                id: button_1
                bg_color: $button_on_color
            - lvgl.widget.update:
                id: icon_1
                text_color: $icon_on_color
            - lvgl.widget.update:
                id: label_1
                text_color: $label_on_color
          else:
            - lvgl.widget.update:
                id: button_1
                bg_color: $button_off_color
            - lvgl.widget.update:
                id: icon_1
                text_color: $icon_off_color
            - lvgl.widget.update:
                id: label_1
                text_color: $label_off_color

Hi

Try below

switch:
 - platform: gpio
    name: Velocita' 1
    id: velocita_1
    pin:
      mcp23xxx: mcp23017_hub
      # Use pin A1
      number: 1
      mode:
        output: true
      inverted: true
    on_state:
      then:
        lvgl.widget.update:
          id: velocita_1_ventola
          state:
            checked: !lambda return x;
lvgl:
  style_definitions:
    - id: roboto15_style
      text_font: roboto15_font
  displays: my_display
  touchscreens: my_touch
  buffer_size: 10%
  color_depth: 16
  pages:
# LVGL setup with a single button
    
    - id: main_page
      widgets:
       - button:                
            id: velocita_1_ventola
            x: 20
            y: 100
            width: 200
            height: 50
            radius: 10
            bg_color: my_white
            text_color: my_green
            checkable: true
            on_value:
              then:
                - switch.toggle: velocita_1

I haven’t actually tried to see if it compiles. Have a look at the ESPHome/LVGL Tips and Tricks pages, particularly remote and local buttons.