Slider value not correctly stored in variable

I have a template number slider in Home Assistant, but the slider value is not being correctly assigned to the “power” variable. Despite using the id(light_power).state to capture the value, it seems like the value isn’t being properly written or passed in the code. I am trying to send the value through SPI, but it doesn’t seem to reflect the slider’s current state. Any help with debugging this issue would be appreciated.

When I define the “power” variable with a static value (e.g., power = 50), I can measure the value correctly on the bus. However, when I move the slider on the dashboard, the action is triggered, but the slider value is not written to the “power” variable. I would be happy, if someone could give me a hint. I am new to YAML and HA.

Here’s my code:

esphome:
  name: growlight
  friendly_name: growlight

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "removed for the post"

ota:
  - platform: esphome
    password: "removed for the post"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "removed for the post"
    password: "removed for the post"

captive_portal:

# Example configuration entry
switch:
  - platform: gpio
    pin: GPIO3
    name: "!Reset"  
    restore_mode: RESTORE_DEFAULT_ON


# SPI-Bus-Konfiguration
spi:
  clk_pin: GPIO6
  mosi_pin: GPIO7
  miso_pin: GPIO2

# SPI-Gerät (BH2226FV-E2)
spi_device:
     id: bh2226
     cs_pin: GPIO10
     data_rate: 1MHz
     spi_mode: 3
     bit_order: msb_first  

# Number-Slider für Home Assistant
number:
  - platform: template
    name: "SPI Slider"
    id: light_power
    min_value: 0
    max_value: 200
    step: 1
    icon: "mdi:slider"
    set_action:
      - lambda: |-
          uint16_t power = (uint16_t)id(light_power).state; // Slider-Wert als 16-Bit-Zahl
          uint16_t channel8 = 0x0008; // Beispielwert für channel8

          auto reverse = [](uint8_t in) -> uint8_t {
              uint8_t out = 0;
              if (in & 0x01) out |= 0x80;
              if (in & 0x02) out |= 0x40;
              if (in & 0x04) out |= 0x20;
              if (in & 0x08) out |= 0x10;
              if (in & 0x10) out |= 0x08;
              if (in & 0x20) out |= 0x04;
              if (in & 0x40) out |= 0x02;
              if (in & 0x80) out |= 0x01;
              return out;
          };


          uint16_t channel = channel8 & 0x000F;  // Nur untere 4 Bits behalten
          channel = reverse(channel);            // Bits umkehren
          channel = channel << 4;                // Links schieben um 4 Bits

          uint16_t buff = channel | power;      // `channel` mit `power` verknüpfen
          ESP_LOGD("slider", "Aktueller Wert des Sliders: %f", id(light_power).state);

          uint16_t powerDownRelease = 0x0900;
          uint16_t selectDA = 0x03FF;
          uint16_t statusSet = 0x0FFF;

          // Funktion zum Senden eines 16-Bit-Werts per SPI
          auto send_spi = [](uint16_t data) {
              uint8_t high_byte = (data >> 8) & 0xFF;
              uint8_t low_byte = data & 0xFF;

              id(bh2226).enable();  // CS auf LOW
              id(bh2226).write_byte(high_byte);
              id(bh2226).write_byte(low_byte);
              id(bh2226).disable(); // CS auf HIGH
          };

          // Sende die SPI-Befehle
          send_spi(powerDownRelease);
          send_spi(selectDA);
          send_spi(statusSet);
          send_spi(buff);  // Letzte Nachricht mit dem berechneten Wert senden

Here’s the slider on my dashboard:

This has nothing to do with automations (nor your random tag aws-lambda?!) and probably should not be in this section of the forum. This seems to be a question about how to configure a ESPHome device, not how to configure Home Assistant.

Thank you for your inputs, I changed the location of the Post and the Tags.

I figured it out. According to esphome, i had to use an x as the value from the slide in the dashboard, gets stored in x.

  • set_action (Optional, Action): The action that should be performed when the remote (like Home Assistant’s frontend) requests to set the number value. The new value is available to lambdas in the x variable.
    set_action:
      - lambda: |-
          uint16_t power = (uint16_t)x; // Slider-Wert als 16-Bit-Zahl
          uint16_t channel8 = 0x0008; // Beispielwert für channel8