Coloured text in ESPHome yaml editor

In one of my ESPHome projects, the yaml file started off looking as normal, but somehow became colourful in a way I don’t understand. Can anyone explain why these colours have appeared in the lambda section?

Then it turns to white at the end…

switch:
  - platform: template
    name: "Relay Enable"
    id: relay_enable
    optimistic: True
    restore_mode: ALWAYS_OFF




light:
  - platform: esp32_rmt_led_strip
    rgb_order: GRB            # GRB
    pin: GPIO05
    num_leds: 1               # 1 
    chipset: ws2812
    use_psram: false
    name: "On board LED"
    id: onboard_led


             
interval:
             
  - interval: 1.0s  
    then:    
    - lambda: |-       

        id(battery) =  id(battery_volts).state;
        id(load_on) = id(relay_enable).state;
        
        if (id(load_on) == 0){
        id(load_relay).turn_off();       // switch relay off
        id(green_led).turn_off();         // turn led off
        }

        if (id(load_on) == 1){
        id(green_led).turn_on();          // turn led on
        }


        id(led_counter_int) = id(led_counter_int) + 1;

        if (id(led_counter_int) > 2){
        id(led_counter_int) = 1;
        }

        if (id(led_counter_int) == 1){     
        
        id(red) = 0.0;   
        id(blue) = 0.0;
        id(green) = 0.0;
        id(intensity) = 0.0;                 // LED Off
        id(transition_ms)= 1000;  

        }


        if (id(battery) <= 3.6)      
        {  
   
        if (id(led_counter_int) == 2)
        {
        
        id(red) = 1.0;                    // LED Red
        id(blue) = 0.0;
        id(green) = 0.0;       
        id(intensity) = 1.0;            // 0.5
        id(transition_ms)= 1000;
        }
        }


        if (id(battery) > 3.6)    
        {
        if (id(led_counter_int) == 2){   
        id(red) = 0.0;                    // LED Blue
        id(blue) = 1.0;
        id(green) = 0.0;       
        id(intensity) = 0.5;            // 0.5
        id(transition_ms)= 1000;

        if (id(load_on))  {             // turn load relay on, battery at least 3.6v
        id(load_relay).turn_on(); 
        }

        }
        }

       
        if (id(led_counter_int) == 2) 
        { 
        if (id(battery) >= 3.7) {   
        id(red) = 0.0;                    //LED Green
        id(blue) = 0.0;
        id(green) = 1.0;       
        id(intensity) = 0.5;
        id(transition_ms)= 1000;      

        }
        }
        

        // These OFF commands are at the end of the program to impose priority
       
        if (id(load_on) == 0){
        id(load_relay).turn_off();       // turn load relay off command from gui switch
        }

        if (id(battery) <= 3.4)      
        {  
        id(load_relay).turn_off();       // turn load relay off because low volts
        }



        auto call = id(onboard_led).turn_on();     // This is from Light Component
        call.set_transition_length(100);            // in ms
        call.set_brightness(id(intensity));             // 1.0 is full brightness
        call.set_rgb(id(red),id(green),id(blue));              // value 0.0 to 1.0
        call.perform();


I’ve had to include pictures of this as posting the yaml in markup brackets, as above, doesn’t show the colours seen here:-





It’s not causing a problem, I just don’t understand why it happened, or how to put it back to ‘normal’. Or maybe this is normal, and the start block which is a brown/red is not correct?

It’s VSCode’s syntax highlighting. It is used to make the code more readable.

Why would it appear is some places and not others in the same project?
How did it get there?
Why doesn’t it show up when pasting markup code here?

The colour depends on what you have written. e.g. VSCode is not good at syntax highlighting inside braces or quotes.

The colours are not part of the file. They are applied when you view it and it depends on what is showing it. The forum has its own syntax highlighting. It is not the same as VSCode.

Is it something to do with the Explorer I use.
(I’ve never used an external package to edit anything).

That project was probably started on my earlier laptop, which is Windows 10 and I use MS Edge as the browser for HA.

The later laptop is Windows 11 and I use Vivaldi as the browser for HA. The later stuff in the yaml would have been done using this one.

Is that anything to do with it? Else why is some of it in colour and some not? The photos are of one project.

No it only has to do with the program you use, VSCode, the file type and whether or not you have enabled the syntax highlighting feature.

What program? I don’t use VSCode. (Knowingly).
Although it is installed on the older laptop. But I can’t remember why it was needed. I don’t remember using it for anything. Unless some other program uses / launches it.

In HA, I click on ‘ESPHome Builder’ in the side menu.
Then I click on EDIT on a project I need to edit.

That project then opens in the browser. (The HA side menu is still visible).
There are SAVE and INSTALL options shown in the top RHS.

I’ve not seen any other program launch.
I’m totally missing something here..??

This is the program you use:

The program is responsible for color coding things to make it easier to read/understand the code.

Using YAML to contain c++ code is the problem. Technically, that c++ code (in your lambda functions) is a string (so probably should all be the same color as a string). Sometimes the parsers and colorizers try and be smarter and color (outside the lines). Your config clearly confuses them.

I have a pet peeve against more than a line or two of “code” in a YAML config, because it makes it much harder to understand. Your config would likely be better served by having your ginormous lambda in a real c++/header file, with proper indentation and spacing.

See this post for some ways to do that: Reusable function for lambda's?

But, as @anon11644107 said this is only the visual presentation to you. esphome doesn’t see the color, it just sees the text. c++ doesn’t care about blank space, but YAML does (very much, so you have to pay attention to it).

Thanks for the information.
If I could avoid using yaml, I really would.
Getting the spacing / alignment correct is a nightmare.

The reason why there’s so much of it is, it’s a bit of a mongrel. It started out as a test piece, for me to get the tri-coloured led working. The battery bit got added on.

I still don’t understand, why part the way through that lambda, it starts showing different colours.
It’s really useful, but why does it start half way down the program?

You can! Create an esp project in Arduino or IDF. It will, of course, require much more work.

Because the code that does the coloring can’t follow the code correctly. The color is messed up well before you noticed it. (Some of the braces } are colored red, which usually indicates an error, but in this case is not)

You can look at it this way, the language parser is saying the code smells. Code smell - Wikipedia

If that offends you, you can turn it around and say the language parser’s code not only smells but is wrong.

The choice is yours.

Well, there’s nothing like raising someone’s hopes, then smacking them across the face with a wet fish!
(Might be where that code smell is coming from?)

I did do one ESP project in Arduino and added MQTT to get data back and forth into HA.
There was no display driver available in HA for the displays being used, and it was much easier to create it in Arduino.

I just have to accept, it is there and there’s nothing I can do about it…

Thanks for the info.

None of the screenshots you posted look like the ESPHome editor. It looks like this (no matter what HA theme you are using):

Are you sure you are not using VSCode or the Studio Code add-on/app?

Because that’s what the colours look like.

And if you are then you can turn off syntax highlighting.

I’m not knowingly using VSCode or the Studio Code add-on/app.

If I click on ESPHome Builder in HA, I see this:-

If I then click on the FireBeetle 2 EDIT ‘button’, I see this:-
(Although I have scrolled down a bit to the coloured text area).

Which is slightly different to your photo.

I’ve looked in Apps, in HA, which looks like this:-


I’ve looked in HACS, and can’t see either program as ‘downloaded’ or ‘waiting for updates’…

I’ve looked in ‘Programs and Features’, in windows control panel and cannot see anything like those names as installed programs in windows.
I’ve looked in windows start menu and can’t see any reference in there.
If either was installed for some reason and has now been removed, would the feature persist and cause what I’m seeing, maybe?

Or, where else could I look?

You are using an old version. Current version of the app is 2026.7.4. Click the update button.

Also FYI, HACS is for 3rd party integrations and dashboard resources. ESPHome is a core app and integration. It will never be in HACS.

Thanks.
Well spotted.

I didn’t consider the colours may be related to the builder version.

I stopped updating builder around 12 months ago, as one update screwed big time with a number of my projects and I decided to stick with 2025.7.5.

But thanks for your input and for the (likely?) solution, but I’ll not update at the moment. I’ll do it when I find I have to at some point, maybe… :face_with_peeking_eye:

The longer you leave it the more breaking changes you may have to deal with. 12 months is already excessive. And there are no highlighting issues in the latest version. I pasted your code into a new project:

I know. (Head in hands).

It became too regular, before it was called builder.
I’d install an update, and then couldn’t compile projects already in use. Modifying anything was a mission. So I stopped updating. I hear some people keep each version and they are available on Github, but I know I’d mess up, trying to keep track of what’s what.

Sorry if this seems to have become a bit of a wild goose chase. And when I eventually update, I hope I don’t have to spend too much time on here, digging myself out of it.

TIA.

:slight_smile:

There is another option. You can edit the YAML file in the editor of your choice.

Personally I have the ESPHome command line installed on my laptop, but before that I would just edit the .yaml files directly in a text editor, opening them from the share which the HA “Samba share” app (add-on) creates.