ESPHome - Access HA attribute in lambda function [SOLVED]

I’m trying to get the value of an attribute of an HA entity and display it in a LCD display. Here the code:

- platform: homeassistant
  entity_id: light.luce_studio
  attribute: brightness
  id: ha_slider_luce_studio
  internal: 'true'

- platform: ili9xxx
  id: touch_template_display
  model: ST7796
  cs_pin: GPIO15
  dc_pin: GPIO21
  reset_pin: GPIO22
  rotation: 90
  - id: home
    lambda: |2-
      ESP_LOGD("custom", "The value of attribute is: %f", id(ha_slider_luce_studio).state);

Log output:

 [D][custom:142]: The value of attribute is: 0.000000

I always get 0 as value and try to debug. I wonder how the attributes are stored in the sensor object and since I’m new to c++ I have hard time to even debug data types. Maybe someone can help with this.

Hi and welcome.

Please include the full definition of your sensor. Without the sensor type we would only be guessing. Is it sensor:? Or text_sensor:?

Also, what do the logs say? Can you see the sensor value updating? If so post the logs showing this.

1 Like

I updated the post with the info you required.

Sorted out: I just needed to use sensor instead of text_sensor, since the value is a number and not a text.

1 Like