ESPHome - LED & Switch

Hi all,

I am playing arround with an ESP32 dev-board…
At the moment, I do have an LED Connected on GPIO15 and a Switch on GPIO13

Here’s my current Configuration:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: true
    on_state:
      then:
        - output.turn_on: light_output
    name: "Test LED Button"
    device_class: light
    
light:
  - platform: binary
    name: "Test LED"
    output: light_output

output:
  - id: light_output
    platform: gpio
    pin: GPIO15

Now, I do have the following issue:

The State of the Binary_sensor will be recognized correctly (On when pressed, off when not pressed).
LED is Off → when I press the Button, the LED turns on… BUT: It stays on.

The Light entity does not reflect the LED state (on or off)

BUT:
With the light entity, I can controll the light, too.
If the LED turned on (by pushing the button) - And then, I switch on the LED throught the light entity,
I can turn off the LED

Now:
I want the following:
Turning on the LED within the light entity: Should set thje binary_sensor to true (on)
Pushing the Button should switch off the LED and the Light Entity
Pushing the Button should switch on the LED and the light entity… and then I should be able to shut down the light again with the light controll…

Hope, that this is somehow understandable?

1 Like

This turns the LED on when there is a state change, i.e. whether you turn your switch on or off, in both cases the LED is switched on.

Maybe you want this instead?

    on_press:
      then:
        - output.turn_on: light_output
    on_release:
      then:
        - output.turn_off: light_output

That sounds counter-intuitive - after all the binary sensor is tied to the physical switch.

1 Like

You might wanna try an IF ELSE to check the state of the binary sensor on_state before firing either output.turn_on or output.turn_off?

Edit: Hope this works out - typing it out blind, pardon if there’s syntax issues.

binary_sensor:
  - platform: gpio
    id: big_red_button
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: true
    on_state: 
      then:
        - if:
            condition:
              binary_sensor.is_on: big_red_button
            then: 
                  - output.turn_off: light_output
            else:
                  - output.turn_on: light_output
    name: "Test LED Button"
    device_class: light
    
light:
  - platform: binary
    name: "Test LED"
    output: light_output

output:
  - id: light_output
    platform: gpio
    pin: GPIO15

2 Likes

thanks :slight_smile:
I will do some tests later in the evening when I am at home or over the weekend.

But maybe, I need to explain it a bit more in detail (was on hurry when I wrote the initial post)…

The “Button” is just a simple push/release button - but I would like that it does behave like a switch.

So:

  1. LED is off → Pushing the button, turns on the LED
  2. LED is on → Pushing the button, turns off the LED

this would allow me to have either a controll within the Software, but also on the Hardware itself.
Therefore, I probably need a way to store the current state somehow:

so, that’s an example on how I would write it in the Arduino Software directly:

const int BUTTON_PIN = 13; // Arduino pin GPIO13 on ESP32
const int LED_PIN    = 15; // Arduino pin GPIO15 on ESP32

// variables will change:
int state = false;     // the current state of LED

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(LED_PIN, OUTPUT);          // set arduino pin to output mode
}

void loop() {
  if (digitalRead(BUTTON_PIN) == true) {
    state = ! state;
    digitalWrite(LED_PIN, state);
  }
  while (digitalRead(BUTTON_PIN) == true);
  delay(50);
}

Except to have an additional SW Option to change the Button / light State.

Edit: See exxamalte’s reply, that makes sense, you can do light.toggle upon on_press or on_release instead.

I see, in that case, in the example I provided, instead of using binary_sensor.is_on, you can use light.is_on instead.

Hopefully that works out, then you won’t need to setup a separate variable.

Summary
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: true
    on_state: 
      then:
        - if:
            condition:
              light.is_on: led_light
            then: 
                  - output.turn_off: light_output
            else:
                  - output.turn_on: light_output
    name: "Test LED Button"
    device_class: light
    
light:
  - platform: binary
    id: led_light
    name: "Test LED"
    output: light_output

output:
  - id: light_output
    platform: gpio
    pin: GPIO15

Ok, so in the case of a momentary switch, you can simplify the setup. Use the on_press trigger only (if you don’t care how long the button is pressed), and then call light.toggle against the light (not the output).

2 Likes

thanks all :slight_smile:
Now it’s working as supposed