Use HA helper state to define a color

Hi
I’m trying to use the state of a HA input.select helper to define the LED color in a matrix - the helper can change to ‘red’ , ‘orange’, ‘green’ or ‘blue’ via a dashboard drop-down and automations. I then want to display this on an Atom M5 matrix but can’t figure out how to use the helpers variable state in the lamba for the LED matrix.
If I change the id(cost_color).state to a fixed text (ie red or green etc) in the text below it compiles and works fine as a static value (and the button on pin39 works and communicates with HA fine).
I’ve also tried id(cost_color).state and without and without quote marks around the variable with similar errors each time, presume I’m not passing the variable in the correct format but can’t figure in out from the docs
Any help greatly appreciated.

The error message I am receiving is:

INFO ESPHome 2024.6.1
INFO Reading configuration /config/esphome/esphome-web-314be0.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing esphome-web-314be0 (board: esp32dev; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
Library Manager: Installing SPI
INFO Installing SPI
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5
Dependency Graph
|-- AsyncTCP-esphome @ 2.1.3
|-- WiFi @ 2.0.0
|-- FS @ 2.0.0
|-- Update @ 2.0.0
|-- ESPAsyncWebServer-esphome @ 3.2.2
|-- DNSServer @ 2.0.0
|-- ESPmDNS @ 2.0.0
|-- ArduinoJson @ 6.18.5
|-- Improv @ 1.2.3
|-- NeoPixelBus @ 2.7.3
Compiling .pioenvs/esphome-web-314be0/src/main.cpp.o
/config/esphome/esphome-web-314be0.yaml: In lambda function:
/config/esphome/esphome-web-314be0.yaml:100:58: error: no matching function for call to 'esphome::display::Display::rectangle(int, int, int, int, const char*)'
           it.rectangle(0, 0, 5, 5 , id(cost_color).state.c_str()));
                                                          ^
In file included from src/esphome/components/display/display_buffer.h:6,
                 from src/esphome/components/addressable_light/addressable_light_display.h:5,
                 from src/esphome.h:3,
                 from src/main.cpp:3:
src/esphome/components/display/display.h:276:8: note: candidate: 'void esphome::display::Display::rectangle(int, int, int, int, esphome::Color)'
   void rectangle(int x1, int y1, int width, int height, Color color = COLOR_ON);
        ^~~~~~~~~
src/esphome/components/display/display.h:276:8: note:   no known conversion for argument 5 from 'const char*' to 'esphome::Color'
*** [.pioenvs/esphome-web-314be0/src/main.cpp.o] Error 1
========================= [FAILED] Took 15.29 seconds =========================

The esphome file is:

[SETUP/WIFI ETC INFO HERE]
binary_sensor:
  - platform: gpio
    pin: 39
    name: dish_matrix_button
    filters:
      - delayed_on: 10ms
    on_press: 
      then:
        #sends button press to HA (as dish_matrix_button?)
        - homeassistant.service: 
            service: switch.toggle
            data:
              entity_id: switch.mss210_449b_outlet1
              #change this to Shelly/socket ID

  - platform: homeassistant
    id: dishwasher_ok
    entity_id: input_boolean.dishwasher_ok
  
text_sensor:
  - platform: homeassistant
    id: cost_color
    entity_id: input_select.dishwasher_light_color

light:
  - platform: neopixelbus
    num_leds: 25
    variant: ws2812
    name: dish_matrix_led
    id: dish_matrix_led
    pin: 27
    color_correct: [20%, 20%, 20%]

display:
  - platform: addressable_light
    id: led_matrix_display
    addressable_light_id: dish_matrix_led
    width: 5
    height: 5
    rotation: 0°
    update_interval: 16ms
    lambda: |-
          // Draw a bulls-eye pattern
          Color red = Color(0xFF0000);
          Color green = Color(0x00FF00);
          Color blue = Color(0x0000FF);
          Color orange = Color(0xFFA500);
        
          it.rectangle(0, 0, 5, 5 , id(cost_color).state.c_str()) );
          it.filled_rectangle(1, 1, 3, 3, green);

Just a shot in the dark try

    lambda: |-
          // Draw a bulls-eye pattern
          Color red = Color(0xFF0000);
          Color green = Color(0x00FF00);
          Color blue = Color(0x0000FF);
          Color orange = Color(0xFFA500);
          std::string val = id(cost_color).state;
          it.rectangle(0, 0, 5, 5 , val.c_str());
          it.filled_rectangle(1, 1, 3, 3, green);

Thanks, unfortunatly still gives a (similar) error

dishwashlaptop.yaml: In lambda function:
dishwashlaptop.yaml:87:44: error: no matching function for call to 'esphome::display::Display::rectangle(int, int, int, int, const char*)'
In file included from src/esphome/components/display/display_buffer.h:6,
                 from src/esphome\components\addressable_light\addressable_light_display.h:5,
                 from src/esphome.h:3,
                 from src/main.cpp:3:
src/esphome/components/display/display.h:276:8: note: candidate: 'void esphome::display::Display::rectangle(int, int, int, int, esphome::Color)'
   void rectangle(int x1, int y1, int width, int height, Color color = COLOR_ON);
        ^~~~~~~~~
src/esphome/components/display/display.h:276:8: note:   no known conversion for argument 5 from 'const char*' to 'esphome::Color'
*** [.pioenvs\dishwasher_button\src\main.cpp.o] Error 1

I’ve currently got a functional sensor using
if statements,:

if(id(cost_color).state == "Red")    {it.rectangle(0, 0, 5, 5, red);   }
if(id(cost_color).state == "Green")  {it.rectangle(0, 0, 5, 5, green); }
if(id(cost_color).state == "Blue")   {it.rectangle(0, 0, 5, 5, blue);  }
if(id(cost_color).state == "Orange") {it.rectangle(0, 0, 5, 5, orange);}

it’s not pretty but it works and communciates correctly with HA

1 Like