How to configure light to run like switch on "restore_mode"

Hello,

I have several Sonoffs that work as switches.
when I turn on the power with a physical switch, the “restore_mode: always_on” turns on the lamp immediately. it’s the way I want it.

If I configure them in “light”, there is no “restore_mode”, and the command “on_boot” is long, even in priority 100.

If I configure in “light” and “switch”, the “restore_mode” works with the switch, but the lamp turns off immediately (because of the default state of gpio ??).

Do you know how to do it ?

Thank you for your help !

(Sorry for my English "google translation: D)

Hi,

ESPHome is quite customizable so it is worth taking time to learn the API and C++. I just recently had this same problem and came up with the following solution made possible by those items. It should work for any light that uses a PWM or binary output

The solution relies on the convention that the light is ON when the output is HIGH and OFF when the OUTPUT is LOW. A PWM with a 100% duty cycle is equivalent to a binary output of HIGH, so that fact is used. This does meant he light is at 100% brightness but that is unavoidable. To turn the light on, the output must be inverted.

output:
  - platform: esp8266_pwm
      pin: GPIO5
      max_power: 0.9
      frequency: 1000 Hz
      id: pwm_w
      inverted: true

This results in HIGH being set on the GPIO turning the light on at the start of the boot cycle, which happens in a few milliseconds… Since this inversion makes automation non-intuitive it needs to be reversed which is done with on_boot.

on_boot:
 priority: 0
  then:
    - lambda: |-
      id(pwm_w).set_inverted(false);

    - light.turn_on:
      id: bulb_w
      brightness: 100%
      transition_length: 0s

Priority 0 comes after Wifi and MQTT are enabled, but before components are registered at the endpoint. This is around 5 seconds with my bulb. This is important since enabling PWM during Wifi can cause flickering. This solutions is only possible because Lambdas with ESPHome are C++ code. This gives access to the ESPHome and Arduino APIs. By reviewing the API, the PWM and Binary components have a method set_inverted which sets the inversion state of the Output. Most YAML keys will correspond to API functions. Knowing this, the API call can be used to undo the inversion declared in the YAML file. Doing so allows the light to function normally again. The final step is to set the light output so that after the inversion is removed, the light stays on.