Custom firmware ESPHome-Xiaomi_bslamp2

The errors that you see are probably related to compiling the code using the current v1.20.x release of ESPHome or using an older dev version which doesn’t contain the latest changes.
There’s one error in there about a missing semi-colon. That error was fixed a minute ago, That you see this error, also indicates that the ESPHome code used wasn’t a recent dev or beta.

The custom_light_transformer branch does not compile with the current release, but it does compile for me with ESPHome dev and beta. Others have reported the same.

On beta everything is ok.
For some reason, it gives an error on esphome dev version.
I deleted and re-installed dev version, but it did not help.
What could be wrong?

Do you use the dev docker container, or how do you use it?
I am using the dev branch on the command line, and that seems to work fine.

Installed from the store homeassistant.

Summary

Uploading: Screenshot_11.jpg…

What is your refresh interval of the external component? So are you sure you are using the latest version of the external component. I also use the dev version from the add-on store and it compiles without errors.

Maybe you can post the external component part of the yams file you are using.

Such as is downloaded from the gita itself. I am attaching a file.

Summary

--------------------------------------------------------------------------

A few practical configuration substitutions.

--------------------------------------------------------------------------

substitutions:
name: bedside_lamp
friendly_name: Bedside Lamp
transition_length: 500ms

Derive component identifiers, based on the name.

id_light: ${name}
id_light_mode: ${name}_light_mode
id_power_button: ${name}_power_button
id_color_button: ${name}_color_button
id_slider_level: ${name}_slider_level
id_front_panel_illumination: ${name}_front_panel_illumination

--------------------------------------------------------------------------

Use your own preferences for these components.

--------------------------------------------------------------------------

wifi:
ssid: “Your-SSID”
password: “Your-WiFi-Network-Password”

Enable fallback hotspot (for captive portal) in case wifi connection fails

ap:
ssid: “ESPHome $friendly_name”
password: “Password-For-Connecting-To-Captive-Portal”

captive_portal:

api:
password: “Password-To-Link-HomeAssistant-To-This-Device”

Disable the reboot timeout. By default, the lamp reboots after 15

minutes without any client connections (e.g. when home assistant is off

line, or when the WiFi is broken). Reboots are annoying though, because

the RGBWW LEDs will turn off during the reboot, causing the light to

flicker.

reboot_timeout: 0s

api:
password: !secret api_password

If you want to use light presets (see below) from Home Assistant,

then you can expose the required functionality as a service here.

This is an example of how you could expose the activation of a preset.

services:
- service: activate_preset
variables:
my_group: string
my_preset: string
then:
- preset.activate:
group: !lambda ‘return my_group;’
preset: !lambda ‘return my_preset;’

ota:
password: “Password-For-Flashing-This-Device-Over-The-Air”

These OTA triggers are used to provide some visual feedback during the OTA

flashing process. The light is turned blue when the upgrade starts, the

brightness indicator will represent the update progress (fills up from 0%

to 100%), the light will flash red when the upgrade fails or green when the

upgrade succeeds.

You can safely remove these if you don’t want the visual feedback.

on_begin:
then:
- light.disco_on:
id: ${id_light}
red: 0%
green: 0%
blue: 100%
brightness: 2%
transition_length: 0s
on_progress:
then:
- front_panel.set_level: !lambda return (x / 100.0f);
- front_panel.update_leds:
on_end:
then:
- light.disco_on:
id: ${id_light}
red: 0%
green: 100%
blue: 0%
brightness: 2%
transition_length: 0s
on_error:
then:
- light.disco_on:
id: ${id_light}
red: 100%
green: 0%
blue: 0%
brightness: 2%
- delay: 1s
- light.disco_off:
id: ${id_light}

The log level can be raised when needed for debugging the firmware. For

production, a low log level is recommended. Mainly because high volume log

output might interfere with the API/WiFi connection stability. So when

raising the log level, beware that you might see dropped connections from

Home Assistant and the network log viewer.

logger:
level: WARN

--------------------------------------------------------------------------

Configuration specific for the Xiaomi Mijia Bedside Lamp 2.

This is just an example. You can of course modify it for your own needs.

--------------------------------------------------------------------------

Retrieve the code for the xiaomi_bslamp2 platform from GitHub.

external_components:

A special platform package is used for enabling unicore and disabling the

efuse mac crc check. These two changes are required for the ESP32-WROOM-32D

chip that is used in the lamp.

esphome:
name: ${name}
platform: ESP32
board: esp32doit-devkit-v1
platformio_options:
platform: [email protected]
platform_packages: |-
framework-arduinoespressif32 @ GitHub - mmakaay/arduino-esp32-unicore-no-mac-crc: Platform packages for arduino-esp32, built using unicor

This component controls the LED lights of the lamp.

light:

  • platform: xiaomi_bslamp2
    id: ${id_light}
    name: ${friendly_name} RGBWW Light
    default_transition_length: ${transition_length}

    When the brightness is changed, then update the level indicator

    on the front panel accordingly. In night light mode, turn off

    the front panel illumination.

    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;

    You can use any effects that you like. These are just examples.

    effects:
    • random:
      name: “Slow Random”
      transition_length: 30s
      update_interval: 30s
    • random:
      name: “Fast Random”
      transition_length: 3s
      update_interval: 3s

    You can define one or more groups of presets. These presets can

    be activated using various “preset.activate” action options.

    The presets can for example be used to mimic the behavior of the

    original firmware (tapping the color button = go to next preset,

    holding the color button = switch between RGB and white light mode).

    These bindings have been setup below, using the binary_sensor for

    the color button.

    presets:
    rgb:
    red: { red: 100%, green: 0%, blue: 0% }
    green: { red: 0%, green: 100%, blue: 0% }
    blue: { red: 0%, green: 0%, blue: 100% }
    yellow: { red: 100%, green: 100%, blue: 0% }
    purple: { red: 100%, green: 0%, blue: 100% }
    randomize: { effect: Fast Random }
    white:
    cold: { color_temperature: 153 mireds }
    chilly: { color_temperature: 275 mireds }
    luke: { color_temperature: 400 mireds }
    warm: { color_temperature: 588 mireds }

This text sensor propagates the currently active light mode.

The possible light modes are: “off”, “rgb”, “white” and “night”.

By setting the name, the text_sensor will show up as an entity

for the lamp in Home Assistant.

text_sensor:

  • platform: xiaomi_bslamp2
    name: ${name} Light Mode
    id: ${id_light_mode}

This float output controls the front panel illumination + level indicator.

Value 0.0 turns off the illumination. Other values (up to 1.0) turn on

the illumination and set the level indicator to the requested level.

output:

  • platform: xiaomi_bslamp2
    id: ${id_front_panel_illumination}

Binary sensors can be created for handling front panel touch / release

events. To specify what part of the front panel to look at, the “for”

parameter can be set to: “POWER_BUTTON”, “COLOR_BUTTON” or “SLIDER”.

binary_sensor:

When tapping the power button, toggle the light.

When holding the power button, turn on night light mode.

  • platform: xiaomi_bslamp2
    id: ${id_power_button}
    for: POWER_BUTTON
    on_multi_click:
    • timing:
      • ON for at most 0.8s
        then:
      • light.toggle: ${id_light}
    • timing:
      • ON for at least 0.8s
        then:
      • light.turn_on:
        id: ${id_light}
        brightness: 1%

When tapping the color button, acivate the next preset.

When holding the color button, activate the next preset group.

  • platform: xiaomi_bslamp2
    id: ${id_color_button}
    for: COLOR_BUTTON
    on_multi_click:
    • timing:
      • ON for at most 0.6s
        then:
      • preset.activate:
        next: preset
    • timing:
      • ON for at least 0.6s
        then:
      • preset.activate:
        next: group

This sensor component publishes touch events for the front panel slider.

The published value represents the level at which the slider was touched.

By default, values range from 0.01 to 1.00 (in 20 steps). This range can

be modified using the “range_from” and “range_to” parameters.

sensor:

When the slider is touched, update the brightness.

Brightness 0.01 initiates the light night mode, which has already

been handled above (by holding the power button). Therefore, brightness

starts from 0.02 here, to not trigger night mode using the slider.

  • platform: xiaomi_bslamp2
    id: ${id_slider_level}
    range_from: 0.02
    on_value:
    then:
    - light.turn_on:
    id: ${id_light}
    brightness: !lambda return x;

Try to add the refresh configuration variable to the external component section to ensure you are using the latest version of the component, for instance

refresh: 60s

Just to share a short update - mine is now back working totally fine, including the color and brighness taps :smiley:

So I can continue to use my automation to dim the light to 1 % - thanks so much @mmakaay!

I’ve just used your latest example yaml and changed my settings from scratch. No idea what was wrong with my other version, but the lamp felt into reboot loop (every 0.5 seconds) after flashing (both, OTA and serial). Anyhow - I’m happy tho have it working now!

EDIT: I’ve used ESPHome v1.22.0 and the custom_light_transformer branch

EDIT2: @mmakaay: There is a duplicate key in your example.yaml file, api section is double in line 33 and 42.

Thanks for the uodate @badbroechten
Good to hear your lamp is working again.
I will take care of that API section issue!

@alexamig What does the dev version report as its version in the bottom right of the GUI?
And Jos’ suggestion is a good one. Without a refresh time, you won’t have the latest code from my development branch. So add that and retry the compile. I am will update my earlier info with this.

Also: please use a code block when posting a config. This config dump is very hard to read :grinning_face_with_smiling_eyes:

@ mmakaay, jsuanet
Apologies, was away.
refresh: 60s did not help, it gives the same error.
The esphome version at the bottom right gives out v1.21.0-dev
At startup, it swears at this in the log, I attach pictures.

Hi Alexandr,

ESPHome 2021.8.0 is released. So you can try it with the new version. Choose “Clean Build Files” from the 'Hamburger menu" in your lamp entry to be sure all old ‘rubbish’ is removed.

And the current development version is v1.22-0-dev, time for an update :grinning:

Hello. I have already installed ESPHome 2021.8.0 and have safely updated the firmware of the respected [mmakaay] Everything compiled well and worked. : smile:
But I wanted to figure out with the dev version, why it doesn’t work. ((

Version 2021.8.0 released

Recently, Home Assistant introduced explicit color modes (RGB, Color Temperature, etc.). ESPHome had to follow by adding color modes to its API as well. This did cause some head aches along the way. For users of the Bedside Lamp 2 most noticably the disappearing RGB and Color Temperature tabs in the GUI control panel of Home Assistant. For me as developer, various compatibility issues between my code and the progressing ESPHome code.

But the suffering should now be over :smile: With the release of ESPHome 2021.8.0, the code base has been stabilized and I can release new code that is fully compatible with ESPHome and Home Assistant again.

The changes for this release

  • Preset identifiers (group and preset) for the preset.activate action are now validated at compile time.
  • Compatibliity with Home Assistant 2021.8.0. and ESPHome 2021.8.0.
  • Now ESPHome has switched to Home Assistant’s versioning scheme,. this project will follow the versioning scheme as well.
  • The example.yaml has been updated to not use underscores in the hostname anymore. Since recently, using an underscore in the name yields a warning during the firmware compilation, because hostnames can only contain letters, numbers and dashes “-”.

For those who are on ref: custom_light_transformer

You can now switch to either ref: 2021.8.0 or ref: main to make use of the new code.
The custom_light_transformer branch will be deleted in a while, since its code has been fully merged.

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

About Alexandr’s issue

The screenshot that you pasted, only shows some warnings, which are related to an ESPHome change. They are harmless for the actual compile process.

1 Like

Nice, this is working perfect :slight_smile: i’m greatfull for the work that you are doing.
@mmakaay The only this that is not working is transition.

i had made a wake-up light script for my bedside light. so i wil transition slowly start from 0% to 100% with a transition off 5 seconds per procent. But now, when i use this the light just goes on at full brightness.

I was able tot reproduce this. I will work on a solution and keep you posted.

A possible work-around for now: in your script, first turn the lamp on at 1% brightness (or 2% if you want to skip the night light step at 1%). Then call the transitioning code.

Version 2021.8.1 released

Along with the new custom light transformer code (to make the firmware compatible with the latest and greatest Home Assistant and ESPHome) came some issues in transition handling. This release fixes the following issues:

  • Fixed transitioning from the off to on state. Thanks to @marcel1988 for informing me about his non-functional wake-up light script. Before this fix, the light would stay off during the full transformation, only to turn on at the very end.

  • The on_brightness trigger is now also called at the start of a transition. This fixes a problem with the brightness slider illumination not following along when you slide your finger over it.

  • Suppressing a transition time for transitioning to a night light color is now only instant when starting from a night light setting. Before this change, moving from for example 80% brightness to 1% brightness (i.e. night light), would be instant. Now an actual transformation is done.

Yes, that is working perfectly :slight_smile:
Thank you for this update.

1 Like

You’re one of the persons that keep making Home assistant better(and ESPHome from what I see in the changelog). I wonder if the engineers from Xiaomi/Yeelight is following your work :thinking: I hope they do :wink:

1 Like

Haha, in fact, I was wondering about that myself yesterday :grinning_face_with_smiling_eyes: I visited the Yeelight forum thread, but nothing seems to have changed there. Just and endless list of people posting their Mi ID. So sad.

This has zero relevance to devices like LED Ceiling Light-luna-Yeelight right?