Closed dylan09 closed 3 years ago
state: off
So you're turning a light off from the on_turn_off
trigger.
Maybe it worked before, but that doesn't seem like the correct behavior anyway.
If a light is already off it I don't think it should call on_turn_off
again.
Hey there @esphome/core, mind taking a look at this issue as it has been labeled with an integration (light
) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)
I think that's a copy&paste error. I think the state: off
statement is obsolete. But I will check if deleting the statement will fix the error.
The main purpose of the on_turn_off
trigger is to set default values for brightness
and temperature
. And as could be seen in the log, when the light is turned off by Home Assistant the trigger isn't executed.
With version 1.20.4 it is working as expected.
Hi.
I have checked again. This time without the state: off
. For this test I have deleted the on_boot
automation also.
Compiled code using esphome 2021.9.2.
After upload and reboot the on_turn_off
trigger is called in a loop 2-3 times per second. This loop stops immediatly when I turn on the light from Home Assistant.
When I switch off the light on_turn_off
trigger wasn't called.
Compiling the same code using esphome 1.20.4 it works as expected.
After update and reboot the on_turn_off
trigger isn't called until I turn on and turn off the light from Home Assistant.
So there must be something broken between 1.20.4 and 2021.9.2
As seen in the code I am using MQTT for communication.
Try using light.turn_on
or add state: on
.
What your code is doing right now is changing the color_temp etc of a light that is already off.
Again, if you control a light in on_turn_off
I'd expect it not to run on_turn_off
again (because the light is already off)
That is my intention. If the light is switched off, I set the brightness/temperature to default value. So if the light is turned on and no further parameters (brightness, color, ...) is send from HA it starts with this settings.
If I set the default values in the on_turn_on
trigger, parameters (color, brightness, temperature, ...) send from HA are ignored and the light always turn on with my default values.
So it makes a big difference, if I set the default values using the on_turn_off
or on_turn_on
If I use the on_turn_off
trigger, I am able to send different parameter from HA when light it turned on. If no parameters are given it starts with default.
If I use the on_turn_on
trigger, the light always is using the default values, because the on_turn_on
trigger is executed, after the light is turned on and the parameters send from HA are applied.
Maybe there is another way to get the light working as I described above:
Any hints are very welcome.
As a test, I removed all the code from the on_turn_off
trigger. Only one logger output for debugging. Even without light.control
the on_turn_off
trigger is not executed when light is turned off.
I think my issue is related to issue #2458. And if I remember correctly there was also an issue where transition length was a problem.
I have found, that changing the default_transition_length
to 0ms changed the behaviour how state changes are detected.
I have implemented following yaml as test to see if light conditions are working:
interval:
- interval: 2 sec
then:
- if:
condition:
light.is_on: ${id_prefix}
then:
- logger.log:
format: "Condition check: Light is on"
tag: "intervall 2"
- if:
condition:
light.is_off: ${id_prefix}
then:
- logger.log:
format: "Condition check: Light is off"
tag: "intervall 2"
Log output, whendefault_transition_length
is set to 500ms:
[22:58:18][D][intervall 2:272]: Condition check: Light is off
[22:58:19][I][light:390]: 'xx_li_wiz1' - Keeping current color mode Cold/warm white for call without color mode.
[22:58:19][D][light:035]: 'xx_li_wiz1' Setting:
[22:58:19][D][light:046]: State: ON
[22:58:19][D][light:084]: Transition length: 0.5s
[22:58:20][D][intervall 2:266]: Condition check: Light is on
[22:58:22][D][intervall 2:266]: Condition check: Light is on
[22:58:24][D][intervall 2:266]: Condition check: Light is on
[22:58:26][D][light:035]: 'xx_li_wiz1' Setting:
[22:58:26][D][light:046]: State: OFF
[22:58:26][D][light:084]: Transition length: 0.5s
[22:58:26][D][intervall 2:266]: Condition check: Light is on
[22:58:28][D][intervall 2:266]: Condition check: Light is on
Log output, whendefault_transition_length
is set to 0ms:
[23:00:09][D][intervall 2:272]: Condition check: Light is off
[23:00:09][I][light:390]: 'xx_li_wiz1' - Keeping current color mode Cold/warm white for call without color mode.
[23:00:09][D][light:035]: 'xx_li_wiz1' Setting:
[23:00:09][D][light:046]: State: ON
[23:00:11][D][intervall 2:266]: Condition check: Light is on
[23:00:13][D][intervall 2:266]: Condition check: Light is on
[23:00:15][D][intervall 2:266]: Condition check: Light is on
[23:00:17][D][intervall 2:266]: Condition check: Light is on
[23:00:17][D][light:035]: 'xx_li_wiz1' Setting:
[23:00:17][D][light:046]: State: OFF
[23:00:19][D][intervall 2:272]: Condition check: Light is off
[23:00:21][D][intervall 2:272]: Condition check: Light is off
With transition lenght != 0ms also the condition check light.is_off
isn't working.
First log output shows, that after State: OFF, the condition for on is true. In the second log output using 0ms, the check is working.
Regardless of my somewhat unusual program:
The error is that the state light_off
is not recognized if the default_transition_length
> 0.
I have now tried to use the condition light.is_off
. But this is not triggered. Description and logs in the previous post.
Does anyone have a tip where in the ESPHome source code I can find the handling with the conditions/triggers?
Same here. I have a scenario where the heater of a rain sensor has to be switched off shortly to measure the resistance. This Automation worked fine until a few versions ago. I don't know exactly when.
interval:
- interval: 60s
then:
- if:
condition:
light.is_on: heizung_light
then:
- logger.log: "Heizung ist AN. Wird ausgeschaltet..."
- light.turn_off: heizung_light
- logger.log: "Heizung ist AUS"
- delay: 500ms
- logger.log: "Regensensor Power an"
- output.turn_on: rs_power
- delay: 500ms
- logger.log: "Regensensor auslesen"
- component.update: regensensor
- logger.log: "Regensensor Garten updaten"
- sensor.template.publish:
id: regensensor_garten
state: !lambda |-
return id(regensensor).state;
- delay: 200ms
- logger.log: "Regensensor Power aus"
- output.turn_off: rs_power
- delay: 200ms
- light.turn_on: heizung_light
- logger.log: "Heizung ist wieder AN"
else:
- logger.log: "Heizung ist AUS"
- delay: 500ms
- logger.log: "Regensensor Power an"
- output.turn_on: rs_power
- delay: 500ms
- logger.log: "Regensensor auslesen"
- component.update: regensensor
- logger.log: "Regensensor Garten updaten"
- sensor.template.publish:
id: regensensor_garten
state: !lambda |-
return id(regensensor).state;
- delay: 200ms
- logger.log: "Regensensor Power aus"
- output.turn_off: rs_power
I tried everything to narrow it down and found, that when including
on_turn_on:
- logger.log: "Light Turned On!"
on_turn_off:
- logger.log: "Light Turned Off!"
to the light component only the "Light Turned On!"
got logged. This led me to this thread and since I have set default_transition_length
it's working again.
The problem was, that after the heizung_light
switch got turned on manually in HA the automation never again saw the else
condition as the light stayed "on" all the time. The switch in HA was off but the ESP still was convinced that the light is on.
Hello!, I confirm this issue and the workaround.
Please find below the code to replicate it. The "green led" has default transition to 0s, so the code inside on_turn_off runs as expected. But for "blue led" the on_turn_off trigger is ignored
In my case, "on_turn_off" is the easiest way to push the light status to my Nextion screens (while not having to pass the transition parameter on every call to service light.turn_off). I hope this is something easy to solve. I cannot help with C++ but if I can test/document or anything else... I'll be happy to help.
CODE:
output:
- platform: ledc
pin: GPIO27
id: greenled_output
- platform: ledc
pin: GPIO25
id: blueled_output
light:
## Green led
- platform: monochromatic
name: "Green led"
output: greenled_output
id: greenled_light
restore_mode: ALWAYS_OFF
default_transition_length: 0s
on_turn_on:
then:
- logger.log: "green led, on_turn_on"
on_turn_off:
then:
- logger.log: "green led, on_turn_off"
## Blue led
- platform: monochromatic
name: "Blue led"
output: blueled_output
id: blueled_light
restore_mode: ALWAYS_OFF
on_turn_on:
then:
- logger.log: "blue led, on_turn_on"
on_turn_off:
then:
- logger.log: "blue led, on_turn_off"
LOGS:
[17:47:51][D][light:035]: 'Blue led' Setting:
[17:47:51][D][light:046]: State: ON
[17:47:51][D][light:084]: Transition length: 1.0s
[17:47:51][D][main:144]: blue led, on_turn_on
[17:47:54][D][light:035]: 'Blue led' Setting:
[17:47:54][D][light:046]: State: OFF
[17:47:54][D][light:084]: Transition length: 1.0s
[17:48:06][D][light:035]: 'Green led' Setting:
[17:48:06][D][light:046]: State: ON
[17:48:06][D][main:229]: green led, on_turn_on
[17:48:09][D][light:035]: 'Green led' Setting:
[17:48:09][D][light:046]: State: OFF
[17:48:09][D][main:235]: green led, on_turn_off
Same issue observed here. on_turn_off no longer triggers after upgrade when having a transition length . Worked fine before.
Could this issue potentially stem from this fix? https://github.com/esphome/esphome/pull/2320
Thanks. Will test and check the fix when next ESPHome version is officially published.
The problem
The trigger light.on_turn_off is only called directly after boot. When light is turned off from Home Assistant the trigger isn't executed. I set default values for brightness and light temperature for my rgbww lights. This worked for version 1.20.4. But it is broken with version 2021.9.2
As you can see in log file, the trigger is called in an endless loop after boot (I call light.turn_off in on_boot). But after switching on the light from Home Assistant it is never called again.
As you can see in the log,
on_turn_off
is called in an endless loop until the first state switch to on. But it is never called for switching off light from HA.Which version of ESPHome has the issue?
2021.9.2
What type of installation are you using?
pip
Which version of Home Assistant has the issue?
2021.9.7
What platform are you using?
ESP8266
Board
esp_wroom_02
Component causing the issue
light
Example YAML snippet
Anything in the logs that might be useful for us?
Additional information
No response