Smart night light ESPHome

Hi,

I’m new here and also new in esphome. I found it very useful for making dumb devices smart very easy.

I integrated ESP-12 inside very big and high intensity bathroom lamp with 5 channel LEDs RGB-CW-WW. My goal is to make smart lamp which would be used as ambient lamp at night (from 21:00 to 4:00) and if I press button it will turn on on 100% 4500K from 6:00 to 20:00 and 50% 2700K from 20:00 to 6:00. If the light is turned on another short press turns it off (or ambient during night). Double click turns the lamp on full power. Third function (long press) changes or better said cycles colors (RGB) all on full power.

I already tried but I don’t have experiences with this type of programming. First problem is that I can’t use nested IF conditions. I tried with lambda but I always get some errors. I don’t even know how to check the state of “wc_light”. I would appreciate some help with this project.

time:
  - platform: homeassistant
    id: esptime

binary_sensor: 
  - platform: gpio
    pin: 
      number: GPIO02
      #mode: INPUT_PULLUP
      inverted: true
    name: button
    on_multi_click:
    - timing:
      - ON for at most 1s
      - OFF for at most 1s
      - ON for at most 1s
      - OFF for at least 0.2s
      then:
        - logger.log: "Double Clicked"
        - light.turn_on:
            id: wc_light
            brightness: 100%
            color_temperature: 4500K
    - timing:
        - ON for at least 1.5s
      then:
        - logger.log: "Long Press"
        - light.turn_off: wc_light
    - timing:
        - ON for at most 1s
        - OFF for at least 0.3s
      then:
        - logger.log: "Single Short Clicked"
        - if:
            condition:
              - light.is_on: wc_light
              #lambda: 'return id(esptime).now().second >= 30;'
            then:
              - light.turn_off: wc_light
            else:
              - light.turn_off: wc_light
        - if:
            condition:
              - lambda: 'return id(esptime).now().hour >= 20 || id(esptime).now().hour <= 6;'
            then:
              lambda: |-
                if(return id(wc_light).current_values.get_state()){
                  auto call = id(wc_light).make_call();
                  call.set_state(false);
                  call.perform();
                } else {
                  static int color_temp = 370;
                  static int transition = 3000;
                  auto call = id(wc_light).make_call();
                  call.set_state(true);
                  call.set_color_temperature(color_temp);
                  call.set_transition_length(transition);
                  call.set_brightness(0.5);
                  call.perform();
                }

            else:
              lambda: |-
                static int color_temp = 255;
                static int transition = 1000;
                auto call = id(wc_light).make_call();
                call.set_state(true);
                call.set_color_temperature(color_temp);
                call.set_transition_length(transition);
                call.set_brightness(1.0);
                call.perform();         

light:
  - platform: rgbww
    name: "bathroom"
    id: wc_light
    red: pwm_red
    green: pwm_green
    blue: pwm_blue
    cold_white: pwm_cw
    warm_white: pwm_ww
    cold_white_color_temperature: 6000 K
    warm_white_color_temperature: 2700 K
    constant_brightness: false
    color_interlock: true

output:
  - platform: esp8266_pwm
    pin: GPIO12
    frequency: 800 Hz
    id: pwm_green
  - platform: esp8266_pwm
    pin: GPIO14
    frequency: 800 Hz
    id: pwm_red
  - platform: esp8266_pwm
    pin: GPIO04
    frequency: 800 Hz
    id: pwm_blue
  - platform: esp8266_pwm
    pin: GPIO13
    frequency: 1000 Hz
    id: pwm_ww
    max_power: 100%
  - platform: esp8266_pwm
    pin: GPIO05
    frequency: 1000 Hz
    id: pwm_cw
    max_power: 100%

I just took a quick glance, and one peculiar thing is the short click condition: if the light is on, turn it off, else turn it off. That might just be an artifact of previous attempts of course.

What is the error that you run into with the lambda?

I took more time and I was trying with different expressions and finally I got it working. The one thing that I need to do is to change color RGB with long press. I found “lambda” very useful but I didn’t find any guide how to use internal variables like “id(wc_light).current_values.get_brightness()” and similar.

Working code:

time: 
  - platform: homeassistant
    id: esptime
    on_time:
    # Every night
    - seconds: 0
      minutes: 0
      hours: 21
      then:
        - light.turn_on:
            id: wc_light
            brightness: 12%
            transition_length: 1500ms
            color_temperature: 2700K
    # Every morning
    - seconds: 0
      minutes: 0
      hours: 4
      then:
        - light.turn_off: wc_light

#Button
binary_sensor: 
  - platform: gpio
    pin: 
      number: GPIO02
      inverted: true
    name: button
    on_multi_click:
    
    # Long press - Cycle RGB lamp on full power
    - timing:
        - ON for at least 2s
      then:
        - logger.log: "Long Press"
        # TODO Cycle RGB lamp
    
    # Double click - Turn on full power 4500K
    - timing:
      - ON for at most 0.7s
      - OFF for at most 0.7s
      - ON for at most 0.7s
      - OFF for at least 0.5s
      then:
        - logger.log: "Double Clicked"
        - light.turn_on:
            id: wc_light
            brightness: 92%
            transition_length: 1500ms
            color_temperature: 4500K

    # Single click - Turn on/off lamp (06:00 - 19:59 full power 4500K) / (20:00 - 5:59 half power 2700K) / (21:00 - 4:59 - Moonlight when off)
    - timing:
        - ON for at most 0.8s
        - OFF for at least 0.1s
      then:
        - logger.log: "Single Short Clicked"
        - if:
            condition:
              - lambda: 'return id(esptime).now().hour >= 20 || id(esptime).now().hour < 6;'
            then:
              # 20:00 - 5:59 half power 2700K
              lambda: |-
                if(id(wc_light).current_values.get_brightness() > 0.15){
                  if(id(esptime).now().hour >= 21 || id(esptime).now().hour <= 4){
                    auto call = id(wc_light).make_call();
                    call.set_state(true);
                    call.set_color_temperature(371);
                    call.set_transition_length(3000);
                    call.set_brightness(0.12);
                    call.perform();
                  } else {
                    auto call = id(wc_light).make_call();
                    call.set_transition_length(500);
                    call.set_state(false);
                    call.perform();
                  }
                } else {
                  auto call = id(wc_light).make_call();
                  call.set_state(true);
                  call.set_color_temperature(371);
                  call.set_transition_length(2500);
                  call.set_brightness(0.5);
                  call.perform();
                }
            else:
              # 06:00 - 19:59 full power 4500K
              lambda: |-
                if(id(wc_light).current_values.get_brightness() > 0.15){
                  auto call = id(wc_light).make_call();
                  call.set_transition_length(500);
                  call.set_state(false);
                  call.perform();
                } else {
                  auto call = id(wc_light).make_call();
                  call.set_state(true);
                  call.set_color_temperature(260);
                  call.set_transition_length(1500);
                  call.set_brightness(0.92);
                  call.perform();
                }