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?
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.
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..??
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.
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.
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…
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?
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:
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.
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.