Controlling LED switch after activaltion of light from HA

I have a WIFI light switch with:

  • 1 button - physical interface
binary_sensor:
  - platform: gpio
    id: button
    internal: TRUE
    pin:
      number: GPIO03
      inverted: TRUE
      mode: INPUT_PULLUP
    filters:
      - delayed_on: 50ms
    on_press:
      - light.toggle: light_output
      - if:
          condition:
            light.is_on: light_output
          then:
            switch.turn_on: red_led
          else:
            switch.turn_off: red_led
  • 1 led - Red (ON/OFF)
switch:
  - platform: gpio
    id: red_led
    name: "${friendly_name}"
    internal: true
    pin: GPIO14
    restore_mode: ALWAYS_OFF
  • 1 relay
output:
  - platform: gpio
    id: relay
    pin: GPIO04

light:
  - platform: binary
    id: light_output
    name: "${friendly_name}"
    output: relay
    restore_mode: ALWAYS_OFF

I setup the Red LED to turn on as long as the light is on.
This works fine if I press the physical button.

The problem is how do I control the Red LED if the light is turned on via HA?
I found no trigger option for light.

The easiest way would be to remove the LED code altogether and physically connect the LED and it’s series resistor in parallel with the relay coil.

@tom_l this is a bought item (WF-WS01) not a custom build - used tuya convert to get ESPHome on it.
So I do not want to physically modify it.

Ah I see. In that case, try this:

binary_sensor:
  - platform: gpio
    id: button
    internal: TRUE
    pin:
      number: GPIO03
      inverted: TRUE
      mode: INPUT_PULLUP
    filters:
      - delayed_on: 50ms
    on_press:
      - light.toggle: light_output

  - platform: template
    lambda: 'return id(light_output).state;'
    on_press:
      then:
        - switch.turn_on: red_led
    on_release:
      then:
        - switch.turn_off: red_led

Or maybe:

  - platform: template
    lambda: |-
      if ( id(light_output).state == 'on' ) {
        return true;
      } else {
        return false;
      }
    on_press:
      then:
        - switch.turn_on: red_led
    on_release:
      then:
        - switch.turn_off: red_led

it basically the same as my smart plugs
that has a physical button with status LED’s

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: True
    name: "Gorgeous Button"
    on_press:
      then:
       - switch.toggle: s20_relay
    internal: true
  - platform: status
    name: "Gorgeous Status"

output:
  # Register the LED as a dimmable output ....
  - platform: esp8266_pwm 
    id: s20_blue_led
    pin:
      number: GPIO4
      inverted: false
  - platform: esp8266_pwm 
    id: s20_red_led
    pin: 
      number: GPIO5
      inverted: true

switch:
  - platform: gpio 
    name: "Gorgeous"
    pin: GPIO12
    id: s20_relay
    on_turn_on: 
      - output.turn_on:
          id: s20_red_led
    on_turn_off:
      - output.turn_off: 
          id: s20_red_led
light:
  # ... and then make a light out of it.
  - platform: monochromatic
    name: "Power Blue LED"
    output: s20_blue_led
    internal: true
  - platform: monochromatic
    name: "Red LED"
    output: s20_red_led
    internal: true

@tom_l I tried this (on both “output” and “light”) and for both I got compilation errors:

src/main.cpp:582:28: error: 'class esphome::gpio::GPIOBinaryOutput' has no member named 'state'
src/main.cpp:586:35: error: 'class esphome::light::LightState' has no member named 'state'

These objects don’t have a “state” - maybe that is why there is no Trigger option for them…

Yeah ok. I’ve never tired it. Go with Harry’s idea of using the relay switch state.

I thought of it but my device is a “Light” and that is how I want HA to recognize it automatically.
If I change the output,light pair to a switch I will manually need to define it as a light element in HA - change the icon, etc…

Change the relay to a switch instead of an output. Change the red LED to a Light.

Have a closer look at Harry’s example.

I understood that but that brings me back to the point the HA sees a “switch” element and not a “light” element.
need to manually define an icon, etc…

In his case it is OK because he has a power outlet device and not a light switch

I know, I wanted to save that extra step.
If ESPHome is defined as a light element then it is not needed.
I will do this for now (no other way) and opened a feature request for light trigger automation.

Hang on what about this? (I may not be thinking straight):

binary_sensor:
  - platform: gpio
    internal: true
    pin:
      number: GPIO3
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
       - switch.toggle: relay

switch:
  - platform: gpio
    id: relay
    pin: GPIO04
    on_turn_on: 
      - output.turn_on:
          id: s20_red_led
    on_turn_off:
      - output.turn_off: 
          id: s20_red_led

output:
  - platform: gpio
    id: red_led
    name: "${friendly_name}"
    internal: true
    pin: GPIO14
    restore_mode: ALWAYS_OFF

light:
  - platform: binary
    id: light_output
    name: "${friendly_name}"
    output: relay
    restore_mode: ALWAYS_OFF

  - platform: monochromatic
    name: "Red LED"
    output: red_led
    internal: true

Cannot define output:relay which is of type switch. needs to be of type output.

Bugger.

I’m not beat yet. There has to be a way to do this.

There’s no output template platform… so that’s not it.

Does output support on turn_on?

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO3
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
       - light.toggle: light_output
    internal: true

output:
  - platform: gpio
    id: relay
    pin: GPIO04
    on_turn_on: 
      - output.turn_on:
          id: s20_red_led
    on_turn_off:
      - output.turn_off: 
          id: s20_red_led

  - platform: gpio
    id: red_led
    name: "${friendly_name}"
    internal: true
    pin: GPIO14
    restore_mode: ALWAYS_OFF

light:
  - platform: binary
    id: light_output
    name: "${friendly_name}"
    output: relay
    restore_mode: ALWAYS_OFF

No as I wrote before output and light do not have trigger actions - thats exactly my feature request

No mention of output trigger there.

This forum category is for help not feature requests. Ask here:

Or Otto might have a way to do it instead.

why aren’t you using the
output.turn_on Action in the output component
to then turn on the LED
remove it from being triggerd by the binary sensor

so the LED will only ever be on if the output relay is on

The output component does not have a trigger it only has an action.

It seems my request for light trigger has already been asked and has been implemented in the dev release.

it is a solution to what you original required.The LED will only be on when ever the light is on. Whether its been triggered by the button or HA. So I don’t understand why it’s not what you want.