esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
290 stars 34 forks source link

monochromatic light OFF state 100% brightness #2833

Open obrador opened 2 years ago

obrador commented 2 years ago

The problem

Hi, I don't know if that's an issue or if it's the normal behaviour. I've set up a monochromatic light with a PWM output, it works fine, but when I set brightness to 0%, the state turns automatically to "OFF" and the brightness become 100%. Light is off, it works in fact, but I would like brightness to be 0%. Is there a way to achieve that?

Which version of ESPHome has the issue?

2021.12.0

What type of installation are you using?

pip

Which version of Home Assistant has the issue?

No HA. I use OpenHab

What platform are you using?

ESP8266

Board

D1

Component causing the issue

monochromatic light

Example YAML snippet

light:
  - platform: monochromatic
    name: "White LED strip"
    output: wled_strip

output:
  - platform: esp8266_pwm
    id: wled_strip
    pin: GPIO13

Anything in the logs that might be useful for us?

No errors

Additional information

No response

oxan commented 2 years ago

No. Why do you want that?

obrador commented 2 years ago

Openhab reads only brightness as the state of the light, so when light is OFF, it shows as ON because brightness is 100%. But I'm sure I can solve this inconvenient through Openhab settings.

Thanks a lot.

oxan commented 2 years ago

How does OpenHAB connect to ESPHome? through MQTT?

I think it'd be sensible to change the MQTT interface to report 0% brightness when a light is off.

obrador commented 2 years ago

Hi, yeah through MQTT. Openhab can do a transformation on the input MQTT message, so I can map OFF state to 0% brightness. Surely will work.

Thanks!

oxan commented 2 years ago

Can you try esphome/esphome#2934?

ahpohl commented 1 year ago

The OpenHAB MQTT dimmer item doesn't have a power switch so I like to control the lamp power state purely with the brightness setting. Of course one could add another power channel to the thing, but that would add another control just for one lamp. Here is what I would like to achieve:

brightness = 0 --> state: OFF brightness > 0 --> state ON

image

The patch below implements exactly this behavior and fixes the 100 % brightness issue:

diff --git a/esphome/components/light/light_call.cpp b/esphome/components/light/light_call.cpp
index fb4e45b3..5eca4005 100644
--- a/esphome/components/light/light_call.cpp
+++ b/esphome/components/light/light_call.cpp
@@ -228,10 +228,13 @@ LightColorValues LightCall::validate_() {
   // Flag whether an explicit turn off was requested, in which case we'll also stop the effect.
   bool explicit_turn_off_request = this->state_.has_value() && !*this->state_;

-  // Turn off when brightness is set to zero, and reset brightness (so that it has nonzero brightness when turned on).
-  if (this->brightness_.has_value() && *this->brightness_ == 0.0f) {
-    this->state_ = optional<float>(false);
-    this->brightness_ = optional<float>(1.0f);
+  // Turn off when brightness is set to zero, otherwise turn on.
+  if (this->brightness_.has_value()) {
+    if (*this->brightness_ == 0.0f) {
+      this->state_ = optional<float>(false);
+    } else {
+      this->state_ = optional<float>(true);
+    }
   }

   // Set color brightness to 100% if currently zero and a color is set.

The PR https://github.com/esphome/esphome/pull/2934 @oxan is already closed so I commented here instead.

ahpohl commented 1 year ago

Created a new PR https://github.com/esphome/esphome/pull/4586 which implements the changes posted above.