Custom firmware ESPHome-Xiaomi_bslamp2

That is a use case that somebody came up with, and for which the individual front panel LED control was required. It is not part of the example.yaml as you already found out. I haven’t tried myself, but one of these might do the trick for you (a modification of the existing example):

Option 1: Always turn on the power button, even in night light mode

    on_brightness:
      - if:
          condition:
            text_sensor.state:
                id: ${id_light_mode}
                state: night
          then:
            - output.set_level:
                id: ${id_front_panel_illumination}
                level: 0
          else:
            - output.set_level:
                id: ${id_front_panel_illumination}
                level: !lambda return x;
      - front_panel.turn_on_leds: POWER

Option 2: Only turn on the power button when night light is not used

    on_brightness:
      - if:
          condition:
            text_sensor.state:
                id: ${id_light_mode}
                state: night
          then:
            - output.set_level:
                id: ${id_front_panel_illumination}
                level: 0
          else:
            - output.set_level:
                id: ${id_front_panel_illumination}
                level: !lambda return x;
            - front_panel.turn_on_leds: POWER

Only one line was added in these examples (front_panel.turn_on_leds: POWER). You don’t have to worry about the rest of the code, only where to insert that line.

I hope this helps!

mmakaay thank you very much humanly.
I put it according to your first option and everything worked right away.
The button is now always on, what was needed for the dark.
Thanks for your cool firmware !!! Have a good day.

1 Like

First thanks to @mmakaay for his excelent work. I'm still trying to understand how the RGB works on the color_rgb_light.h to get an especific brightness. I finally solve that ugly green on the white night light, the values that I used are:

> red = 0.8847f;
> green = 0.8727f;
> blue = 0.9629f;

those was very similar to the original firm. but im tryng to get a little bit less brightness on it. Also on the color night light too but I only have one lamp to dev and pro.

The low brightness levels are very finnicky and I got slightly different colors on different devices (I’ve got three of them).
In my firmware, I did try to get some other brightness levels, but finally went for compatibility with the original pin outputs and bv accompanying brightness options.

But how do you calculate the value for x brightness? I’m trying to figure it out, but I don’t see any logic.

Yeah, isn’t that great? That doesn’t seem to be any logic :smiley: Therefore, you can’t really talk about “calculations” here. The path from a light setting to the required pin outputs is not the path of computation.

I basically did a million measurements on the original device and wrote code to interpolate between those to get the required outputs for each light setting. If you’d look at the spreadsheet that I created for this, you’d see that the graphs that represent pin outputs for different settings are all over the place.

You are talking about a bit less brightness, but don’t know if that would be possible. The night light levels are about as dim as the lamp will go, hardware-wise. You could try modifying the light code to use even lower pin outputs, but IIRC, that will likely end up in the LEDs turning off.

1 Like

Heads up: when you are going to upgrade to the latest home-assistant, the Bedside Lamp 2 will not work correctly anymore. I just found out that the control panel becomes like this:

afbeelding

So no option to choose RGB or Color Temperature settings, only on/off and effect.
This is likely because of the improved color mode support in the latest versions, so I will work on getting that implemented correctly for the lamp firmware.

1 Like

Stopped compiling on ESPHome version: 1.20.4

That is because of a compatibility fix to make the new color modes work in home assistant, which requires the next version of ESPhome (v1.21.0).

You can update the external component config in the yaml to use ref: v1.1.0. that will give you the code that compiles with v1.20.0.
Once ESPhome v1.21.0 is available and installed, you can switch back to ref: main.

Another option would be to (temporarily) use the dev branch of ESPhome (which contains the changes that will go into the next release).

1 Like

Everything compiles fine on version 1.20.4. We are waiting for compatibility fixes.
Thank you very much for your hard work.

Yes, I fixed the compilation issues, so now the firmware compiles with both ESPHome v1.20.0 and the current dev version (which is the upcoming v1.21.0 release).
However, when compiling the firmware with v1.20.0, then the RGB and Color Temperature tabs will still be missing from the Home Assistant GUI. No work-around is available for that unfortunately.

If you need those tabs in the HA GUI, then you’ll have to compile the lamp firmware using the current dev version of ESPHome.

I gathered some information about this on the related GitHub issue page.

Hi @mmakaay, I’ve tried it with the dev version of ESP home, but without success.

I receive the following error message while trying to install to my bedside lamp:

In file included from src/esphome/components/light/addressable_light.h:11:0,
                 from src/esphome.h:20,
                 from src/main.cpp:3:
src/esphome/components/light/transformers.h: In member function 'virtual bool esphome::xiaomi::bslamp2::ColorTransitionHandler::set_light_color_values(esphome::light::LightColorValues)':
src/esphome/components/light/transformers.h:51:16: error: 'static float esphome::light::LightTransitionTransformer::smoothed_progress(float)' is protected
   static float smoothed_progress(float x) { return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f); }
                ^
In file included from src/esphome/components/xiaomi_bslamp2/light/light_output.h:6:0,
                 from src/esphome/components/xiaomi_bslamp2/light/automation.h:6,
                 from src/esphome.h:54,
                 from src/main.cpp:3:
src/esphome/components/xiaomi_bslamp2/light/color_transition_handler.h:85:85: error: within this context
       auto smoothed = light::LightTransitionTransformer::smoothed_progress(progress_);
                                                                                     ^
In file included from src/esphome/components/xiaomi_bslamp2/light/automation.h:6:0,
                 from src/esphome.h:54,
                 from src/main.cpp:3:
src/esphome/components/xiaomi_bslamp2/light/light_output.h: In member function 'virtual esphome::light::LightTraits esphome::xiaomi::bslamp2::XiaomiBslamp2LightOutput::get_traits()':
src/esphome/components/xiaomi_bslamp2/light/light_output.h:31:12: error: 'class esphome::light::LightTraits' has no member named 'set_supports_rgb'
     traits.set_supports_rgb(true);
            ^
src/esphome/components/xiaomi_bslamp2/light/light_output.h:32:12: error: 'class esphome::light::LightTraits' has no member named 'set_supports_color_temperature'
     traits.set_supports_color_temperature(true);
            ^
src/esphome/components/xiaomi_bslamp2/light/light_output.h:33:12: error: 'class esphome::light::LightTraits' has no member named 'set_supports_brightness'
     traits.set_supports_brightness(true);
            ^
src/esphome/components/xiaomi_bslamp2/light/light_output.h:34:12: error: 'class esphome::light::LightTraits' has no member named 'set_supports_rgb_white_value'
     traits.set_supports_rgb_white_value(false);
            ^
src/esphome/components/xiaomi_bslamp2/light/light_output.h:35:12: error: 'class esphome::light::LightTraits' has no member named 'set_supports_color_interlock'
     traits.set_supports_color_interlock(true);
            ^
In file included from src/esphome/components/xiaomi_bslamp2/light/automation.h:7:0,
                 from src/esphome.h:54,
                 from src/main.cpp:3:
src/esphome/components/xiaomi_bslamp2/light/light_state.h: In member function 'virtual bool esphome::xiaomi::bslamp2::XiaomiBslamp2LightState::is_transition()':
src/esphome/components/xiaomi_bslamp2/light/light_state.h:40:47: error: 'class esphome::light::LightTransformer' has no member named 'is_transition'
   bool is_transition() { return transformer_->is_transition(); }
                                               ^
src/esphome/components/xiaomi_bslamp2/light/light_state.h: In member function 'virtual esphome::light::LightColorValues esphome::xiaomi::bslamp2::XiaomiBslamp2LightState::get_end_values()':
src/esphome/components/xiaomi_bslamp2/light/light_state.h:41:67: error: 'class esphome::light::LightTransformer' has no member named 'get_end_values'
   light::LightColorValues get_end_values() { return transformer_->get_end_values(); }
                                                                   ^
src/esphome/components/xiaomi_bslamp2/light/light_state.h: In member function 'virtual float esphome::xiaomi::bslamp2::XiaomiBslamp2LightState::get_progress()':
src/esphome/components/xiaomi_bslamp2/light/light_state.h:42:47: error: 'class esphome::light::LightTransformer' has no member named 'get_progress'
   float get_progress() { return transformer_->get_progress(); }
                                               ^
Compiling /data/bedside_lamp/.pioenvs/bedside_lamp/lib789/WiFi/WiFiClient.cpp.o
*** [/data/bedside_lamp/.pioenvs/bedside_lamp/src/main.cpp.o] Error 1
========================= [FAILED] Took 20.56 seconds =========================

Thanks for any hint.

Best regards
Johannes

Hello mmakaay.
I used ESPHome (dev) to compile, but the graphical tabs in the HA never showed up.Screenshot_10

Yet another change has been done in the dev version, which breaks the current build.
It’s hard to keep up :wink:

The changes in ESPHOme are all good changes, which actually fix some of the issues that I ran into while building the custom firmware. But now things are being modified to support use cases like the Bedside Lamp 2 in a better way, I have to rollback some of my work-arounds and write new code to make things work again.

Right now I’m working on this new code, to get the firmware code in line with the current ESPHome dev version.

2 Likes

Alright, I had to refactor my code quite a bit, but it looks like I’ve got a new version working.
For those who want to test, use the following definition in your device YAML file:

external_components:
  - source:
      type: git
      url: https://github.com/mmakaay/esphome-xiaomi_bslamp2
      ref: custom_light_transformer
    refresh: 60s

For this code to compile, you need ESPHome v1.21.0 or higher (at the time of writing, this is the beta branch of ESPHome, the dev branch is also usable).

Once v1.21.0 becomes the stable ESPHome release, I will include the code changes in my stable release. From then on, you can switch back to ref: main.

Hi @mmakaay, that killed my lamp completely. I was able to install the firmware wirelessly without any issues. But after it, the lamp was not reachable via the network, so I did a power circle. Even no local operation of the device is possible anymore. It’s simply dead :frowning:

I’ll try tonight if I can access it via the serial adapter.

Great work! Tested with both the beta release and the dev-release (1.22) and it’s working fine for me.

That doesn’t sound right :frowning:
Haven’t experienced that myself.
Really curious about the serial log output.
I’ll see if I can reproduce the issue. You remember in what state your lamp was before flashing?

Good news is that flashing via serial will always work, so a complete kill it won’t be.

Yea, you’re right. With manually flashing the firmware.bin file I used the first time, the lamp is back up and running.

So it might be any configuration issue within my current yaml file, even validation says everything “green”. Only warning is the underscore in the device name - but don’t believe that is causing the issue… I’ll check line by line later the day to figure out what’s wrong.

Thanks!

1 Like

Hi. It compiles normally on the beta version. And the version falls out on the developer with an error.

Summary

Compiling /data/bedside_lamp/.pioenvs/bedside_lamp/src/main.cpp.o
Compiling /data/bedside_lamp/.pioenvs/bedside_lamp/lib789/WiFi/WiFiClient.cpp.o
Compiling /data/bedside_lamp/.pioenvs/bedside_lamp/lib789/WiFi/WiFiGeneric.cpp.o
Compiling /data/bedside_lamp/.pioenvs/bedside_lamp/lib789/WiFi/WiFiMulti.cpp.o
Compiling /data/bedside_lamp/.pioenvs/bedside_lamp/lib789/WiFi/WiFiSTA.cpp.o
Compiling /data/bedside_lamp/.pioenvs/bedside_lamp/lib789/WiFi/WiFiScan.cpp.o
In file included from src/esphome/components/xiaomi_bslamp2/light/color_handler_chain.h:10:0,
from src/esphome/components/xiaomi_bslamp2/light/light_output.h:5,
from src/esphome/components/xiaomi_bslamp2/light/automation.h:6,
from src/esphome.h:51,
from src/main.cpp:3:
src/esphome/components/xiaomi_bslamp2/light/color_handler_color_temperature.h: In member function ‘virtual bool esphome::xiaomi::bslamp2::ColorHandlerColorTemperature::set_light_color_values(esphome::light::LightColorValues)’:
src/esphome/components/xiaomi_bslamp2/light/color_handler_color_temperature.h:90:5: error: expected ‘;’ before ‘}’ token
}
^
In file included from src/esphome/components/xiaomi_bslamp2/light/light_output.h:6:0,
from src/esphome/components/xiaomi_bslamp2/light/automation.h:6,
from src/esphome.h:51,
from src/main.cpp:3:
src/esphome/components/xiaomi_bslamp2/light/light_transformer.h: At global scope:
src/esphome/components/xiaomi_bslamp2/light/light_transformer.h:24:8: error: ‘void esphome::xiaomi::bslamp2::XiaomiBslamp2LightTransitionTransformer::start()’ marked ‘override’, but does not override
void start() override {
^
src/esphome/components/xiaomi_bslamp2/light/light_transformer.h:32:37: error: ‘esphome::optionalesphome::light::LightColorValues esphome::xiaomi::bslamp2::XiaomiBslamp2LightTransitionTransformer::apply()’ marked ‘override’, but does not override
optionallight::LightColorValues apply() override {
^
src/esphome/components/xiaomi_bslamp2/light/light_transformer.h: In constructor ‘esphome::xiaomi::bslamp2::XiaomiBslamp2LightTransitionTransformer::XiaomiBslamp2LightTransitionTransformer(esphome::xiaomi::bslamp2::LightHAL*)’:
src/esphome/components/xiaomi_bslamp2/light/light_transformer.h:18:83: error: no matching function for call to ‘esphome::light::LightTransitionTransformer::LightTransitionTransformer()’
explicit XiaomiBslamp2LightTransitionTransformer(LightHAL *light) : light_(light) { }
^
In file included from src/esphome/components/light/light_state.h:10:0,
from src/esphome/core/application.h:30,
from src/esphome/components/api/api_connection.h:4,
from src/esphome.h:2,
from src/main.cpp:3:
src/esphome/components/light/light_transformer.h:49:3: note: candidate: esphome::light::LightTransitionTransformer::LightTransitionTransformer(uint32_t, uint32_t, const esphome::light::LightColorValues&, const esphome::light::LightColorValues&)
LightTransitionTransformer(uint32_t start_time, uint32_t length, const LightColorValues &start_values,
^
src/esphome/components/light/light_transformer.h:49:3: note: candidate expects 4 arguments, 0 provided
src/esphome/components/light/light_transformer.h:47:7: note: candidate: constexpr esphome::light::LightTransitionTransformer::LightTransitionTransformer(const esphome::light::LightTransitionTransformer&)
class LightTransitionTransformer : public LightTransformer {
^
src/esphome/components/light/light_transformer.h:47:7: note: candidate expects 1 argument, 0 provided
src/esphome/components/light/light_transformer.h:47:7: note: candidate: constexpr esphome::light::LightTransitionTransformer::LightTransitionTransformer(esphome::light::LightTransitionTransformer&&)
src/esphome/components/light/light_transformer.h:47:7: note: candidate expects 1 argument, 0 provided
In file included from src/esphome/components/xiaomi_bslamp2/light/light_output.h:6:0,
from src/esphome/components/xiaomi_bslamp2/light/automation.h:6,
from src/esphome.h:51,
from src/main.cpp:3:
src/esphome/components/xiaomi_bslamp2/light/light_transformer.h: In member function ‘virtual bool esphome::xiaomi::bslamp2::XiaomiBslamp2LightTransitionTransformer::is_finished()’:
src/esphome/components/xiaomi_bslamp2/light/light_transformer.h:21:45: error: ‘get_progress_’ was not declared in this scope
return force_finish_ || get_progress_() >= 1.0f;
^
src/esphome/components/xiaomi_bslamp2/light/light_transformer.h: In member function ‘esphome::optionalesphome::light::LightColorValues esphome::xiaomi::bslamp2::XiaomiBslamp2LightTransitionTransformer::apply()’:
src/esphome/components/xiaomi_bslamp2/light/light_transformer.h:38:90: error: ‘get_progress_’ was not declared in this scope
auto smoothed = light::LightTransitionTransformer::smoothed_progress(get_progress_());
^
In file included from src/esphome/components/xiaomi_bslamp2/light/automation.h:6:0,
from src/esphome.h:51,
from src/main.cpp:3:
src/esphome/components/xiaomi_bslamp2/light/light_output.h: At global scope:
src/esphome/components/xiaomi_bslamp2/light/light_output.h:46:44: error: ‘std::unique_ptresphome::light::LightTransformer esphome::xiaomi::bslamp2::XiaomiBslamp2LightOutput::create_default_transition()’ marked ‘override’, but does not override
std::unique_ptrlight::LightTransformer create_default_transition() override {
^
Compiling /data/bedside_lamp/.pioenvs/bedside_lamp/lib789/WiFi/WiFiServer.cpp.o
Compiling /data/bedside_lamp/.pioenvs/bedside_lamp/lib789/WiFi/WiFiUdp.cpp.o
Compiling /data/bedside_lamp/.pioenvs/bedside_lamp/libbb3/ESPAsyncWebServer-esphome/AsyncEventSource.cpp.o
Compiling /data/bedside_lamp/.pioenvs/bedside_lamp/libbb3/ESPAsyncWebServer-esphome/AsyncWebSocket.cpp.o
*** [/data/bedside_lamp/.pioenvs/bedside_lamp/src/main.cpp.o] Error 1
========================= [FAILED] Took 20.24 seconds =========================