How do I get in EspHome brightness data from a lamp in HA?

Hello together,

I’ve been dealing with HS for a few weeks now. I’m also using EspHome and now I’m currently at a point where I don’t know how to go further with my EspHome project.

I use an RF remote to control my “floor lamp.” With a short press of the remote button, I turn the WLEDs on/off, and with a long press, I dim the lamp up/down. Everything works almost as I imagined. There’s still one small thing missing and I want to find a solution for that.

My problem is that I only want to dim the light when the current brightness is above 11%. I want to prevent the light from being turned OFF via a dimming process. But I don’t know how to access the lamp’s data to make this happen.

The following lines are part of my .yaml file that is responsible for dimming down via the remote button:

- platform: remote_receiver
    id: "rf_remote_4_off"
    name: "Button [4] OFF"
    rc_switch_raw:
      protocol: 2
      code: '011011001001001110100111101011101111xxxx'
    # set filter to eliminate bouncing
    filters:
      - delayed_off: 100ms
    # when a rising edge has been detected
    on_press:
      then:
        # wait to decide for a long or short press
        - delay: 250ms
        - if:
            # has the button already been released...
            condition:
               binary_sensor.is_off: rf_remote_4_off
            # ... then it was a short press
            then:
              - logger.log: "short press => off"
              - homeassistant.service:
                  service: light.turn_off
                  data:
                    entity_id: light.lamp              
            # otherwise, it was a long press
            else: 
              while:
                condition:
                  binary_sensor.is_on: rf_remote_4_off
                then:
                - logger.log: "long press => dimming down"
                - homeassistant.service:
                    service: light.turn_on     
                    data:
                      brightness_step_pct: '-10'
                      entity_id: light.lamp              
                # dimming steps
                - delay: 500ms

I tried a lot with the templates (Lambda), but none of it worked.

I understand that I can get access to the lamp’s entities from HA via “- homeassistant.service:” command. But I did not find any solution to handle my problem.

So, I would need the ability to evaluate the current brightness value of my floor lamp to then decide whether or not to dim it further. I don’t have a unique entity for the floor lamp that provides me with a brightness value.

Something like that:

If( id(lamp).brightness > 11) {
	brightness_step_pct: '-10'
}

As I said, I assume that the best way to do this is with a lambda statement.

The second question I have is how I can output the brightness entity to the log.

For Lambda, there’s “ESP_LOGD:,” but I’m missing the “data point” and how to even get it into Lambda. (That’s also my first problem.)

Could someone give me a hint on how I get the brghtness data from my floor lamp into my EspHome?

How is your floor light defined?

Hi nickrout,

first of all, thank you for your answer.

I’m not sure what you mean.

My floor light does have a ESP32 Hardware and includes the software “WLED” to control an SK6812 ring. I Have a Home-Assistant Yellow and there I can find entities from my floor lamp e.g. “light.lamp”.

In the .yaml file I haven’t especially defined my floor lamp. Only in the “-homeassistant.service:” area.

So, what was your question whether my lamp is defined in HA or in the .yaml file?

Hard to tell you exactly what you are doing. Reading between the lines, you have three devices

  1. A wled light, which is connected to HA via the wled integration

  2. A HA yellow running HA

  3. An esp device that you want to use esp32 on to control the light.

Is that correct?

In any event, if you have light.lamp it will have an attribute brightness. You can import that attribute into esphome with the home assistant sensor

So in your case

sensor:
  - platform: homeassistant
    id: current_brightness
    entity_id: light.lamp
    attribute: brightness
1 Like

How would the sensor be set up for esphome light id brightness?

It is not an esphome light. It is a wled light. (At least I have assumed this, see post 4 which @StayCalm probably hasn’t had a chance to respond to yet)

Sorry, I made my question incorrectly. I mean in case of esphome light, is there brightness attribute to be used similar manner?

Ahh yes, that makes sense. But I don’t know the answer. Perhaps in a lambda it.brightness

Hello together,

I’m sorry when I have not described it in a clear way:

My environment is:

  • Home Assistant based on yellow hardware
  • WLED running on a hardware “ESP32 Dev Kit C Development Board NodeMcu ESP32 Wroom 32”. This hardware controls a SK6812 RGBW led ring with 60 LEDs in my floor lamp. It is a device in HA.
  • An EspHome device running on ESP32 D1 Mini NodeMCU. With this ESPHome device I can use, via an RF-receiver module, an IR remote to control the LED ring in my floor lamp.

As I said, it works fine to control my floor lamp via IR-remote. The only thing that is not nice is, when I hold the “dimming down” button to long, the light goes OFF. I want to stop dimming down when the brightness level is under 11%.
@nickrout
That what you have written makes sense to me. I have defined my floor lamp now as a sensor.

sensor:
  - platform: homeassistant
    id: lamp_brightness
    entity_id: light.lamp
    attribute: brightness

To test if it works, I created a template to get a Boolean if the brightness is above 30%. That works so far.

  - platform: template
    name: "Lamp over 30%"
    id: "lamp_over_30"
    lambda: |-
      if (id(lamp_brightness).state > 30) {
        return true;
      } else {
        return false;
      }

But when I use the lambda command in my code where I analyze the remote button then it doesn’t work.

I just don’t understand how to use it yet. I’m probably getting a few things mixed up here. I think I’m using the lambda value incorrectly in the binary sensor, or that the attribute is also wrong.

What’s also unclear to me is that I used “-homeassistant.service” to dim/up the brightness (=> brightness_step_pct: ‘10’). Wouldn’t it be easier to process everything in one lambda statement?

My entire .yaml file is that here:
=> The lambda expression that doesn’t work is the 11th last line

esphome:
  name: ir-receiver-mini
  friendly_name: ir_receiver_mini

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xyz"

ota:
  - platform: esphome
    password: "xyz"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Ir-Receiver-Mini"
    password: "xyz"

# captive_portal:


# define the floor lamp in home assitant here as sensor
# => floor lamp is an WLED device (60 RGBW-LEDs)
sensor:
  - platform: homeassistant
    id: lamp_brightness
    entity_id: light.lamp
    attribute: brightness


remote_receiver:
  pin:
    number: 22
    inverted: True
  dump:
    - rc_switch
  filter: 300us
  idle: 2ms

binary_sensor:

  - platform: template
    name: "Lamp over 30%"
    id: "lamp_over_30"
    lambda: |-
      if (id(lamp_brightness).state > 30) {
        return true;
      } else {
        return false;
      }

#--- RF remote Button [4] ----------------------------
  - platform: remote_receiver
    id: "rf_remote_4_on"
    name: "Button [4] ON"
    rc_switch_raw:
      protocol: 2
      code: '101011000101001110100111101011101111xxxx'
    # set filter to eliminate bouncing
    filters:
      - delayed_off: 100ms
    # when a rising edge has been detected
    on_press:
      then:
        # wait to decide for a long or short press
        - delay: 450ms
        - if:
            # has the button already been released...
            condition:
               binary_sensor.is_off: rf_remote_4_on
            # ... then it was a short press
            then:
              - logger.log: "short press => on"
              - homeassistant.service:
                  service: light.turn_on
                  data:
                    entity_id: light.lamp              
            # otherwise it was a long press
            else: 
              while:
                condition:
                  binary_sensor.is_on: rf_remote_4_on
                then:
                - logger.log: "long press => dimming up"                          
                - homeassistant.service:
                    service: light.turn_on     
                    data:
                      brightness_step_pct: '10'
                      entity_id: light.lamp                
                # dimming steps
                - delay: 500ms

  - platform: remote_receiver
    id: "rf_remote_4_off"
    name: "Button [4] OFF"
    rc_switch_raw:
      protocol: 2
      code: '011011001001001110100111101011101111xxxx'
    # set filter to eliminate bouncing
    filters:
      - delayed_off: 100ms
    # when a rising edge has been detected
    on_press:
      then:
        # wait to decide for a long or short press
        - delay: 450ms
        - if:
            # has the button already been released...
            condition:
               binary_sensor.is_off: rf_remote_4_off
            # ... then it was a short press
            then:
              - logger.log: "short press => off"            
              - homeassistant.service:
                  service: light.turn_off
                  data:
                    entity_id: light.lamp              
            # otherwise it was a long press
            else: 
              while:
                condition:
                  binary_sensor.is_on: rf_remote_4_off
                then:
                - logger.log: "long press => dimming down"
                - lambda: |-
                    if (id(lamp_brightness).state < 11) {
                      return;
                    }                 
                - homeassistant.service:
                    service: light.turn_on     
                    data:
                      brightness_step_pct: '-10'
                      entity_id: light.lamp              
                # dimming steps
                - delay: 500ms

IMHO cause is here:

                - lambda: |-
                    if (id(lamp_brightness).state < 11) {
                      return;
                    } 

This return only returns from lambda, but not stopping execution sequence.
Thus next homeassistant.service executed anyway.

Try this way:

                - logger.log: "long press => dimming down"
                - if:
                    condition:
                      lambda: 'return id(lamp_brightness).state > 10;'
                    then:
                      - homeassistant.service:
                          service: light.turn_on     
                          data:
                            brightness_step_pct: '-10'
                            entity_id: light.lamp 
                      - delay: 500ms

More examples on conditions etc. here:

1 Like

By the way, is esphome while “real while” or just polling ?

Hello together,
OK Masterzz, your comment to my return of the lambda instruction makes sense to me. I tried your suggestion. However, when dimming down, I get the following error in the log:

[homeassistant.sensor:015]: ‘light.lamp’: Can’t convert ‘None’ to number!
[sensor:094]: ‘lamp_brightness’: Sending state nan with 1 decimals of accuracy

I’m not sure where the “None” suddenly came from. So, I changed the lambda expression to:

              while:
                condition:
                  binary_sensor.is_on: rf_remote_4_off
                then:
                - logger.log: "long press => dimming down"
                - if:
                    condition:
                      # lambda: 'return id(lamp_brightness).state > 10;'
                      lambda: |-
                        if (id(lamp_brightness).state > 10) {
                          return true;
                        } else {
                        return false;
                        }                                                     
                    then:
                      - homeassistant.service:
                          service: light.turn_on     
                          data:
                            brightness_step_pct: '-10'
                            entity_id: light.lampe 
                      - delay: 500ms

But that didn’t help either.

I’ve been programming microcontrollers in C for a long time, but programming via .yaml files is really very special.

@Karosm
I’m with you to use a while loop is not a good idea. But currently I don’t exactly know who to implement that right now. I’ve only used a code snippet. I’ll change it later.

My doubts were how while is implemented in esphome. You know in normal C code while is blocking code, but I expect in esphome it’s not. Anyone here please confirm / deny.
Anyway, I’m not familiar with homeassitant.service. Might be just “formal” issue…