How to create a light entity that takes state from an input pin

Hello everyone!

I’m trying to create a light entity working with a bistable relay. Right now I connect the relay with an esp32 on 2 GPIOs one for trigger (button) and one for status (binary sensor). This is the wiring:

The code I’m using is very simple, one button and one binary sensor:

output:
  - platform: gpio
    pin: 21
    id: triggerB1L1L3

button:
  - platform: output
    name: "Lumini cada"
    output: triggerB1L1L3
    duration: 40ms

binary_sensor:
  - platform: gpio
    name: "Status lumini cada"
    id: statusB1L1L3
    pin: 
      inverted: true
      number: 25
      mode:
        input: true
        pullup: true

It works great, but every time I want to make some automation, I need to check state of the binary sensor and then trigger the button.

And now I’d like to have a group with all the lights and close them all. Doing this with my actual configuration, is really complicated.

So, is there a way to create an automation in the esphome code, to make a light entity that triggers GPIO21 but gets the state from GPIO25?

Thank you

Add an id to your button and an action to your binary sensor.

button:
  - platform: output
    name: "Lumini cada"
    id: lumini_cada
    output: triggerB1L1L3
    duration: 40ms

binary_sensor:
  - platform: gpio
    name: "Status lumini cada"
    id: statusB1L1L3
    pin: 
      inverted: true
      number: 25
      mode:
        input: true
        pullup: true
    on_press:
      then:
        - button.press: lumini_cada

Thank you zoogara Daryl,
but your code will just re trigger the button.
A bistable relay, each time you press the button its changing the state. This is why I need the binary sensor to read the state. I have wired buttons, and I have Home assistant buttons. So in my Home assistant UI , the entity looks like this:
On Mushroom UI

type: custom:mushroom-template-card
primary: Baie mare
secondary: '{{states(''sensor.baie_mare_temperature'')}}°C'
icon: mdi:bathtub
entity: button.modul_lumini_1_lumina_baie_mare
icon_color: >-
  {% if is_state('binary_sensor.modul_lumini_1_status_lumina_baie_mare', 'on')
  %}

  amber

  {% endif %}
tap_action:
  action: navigate
  navigation_path: baie-mare
hold_action:
  action: toggle

On Lovelace UI:

type: custom:mushroom-template-card
entity: button.modul_lumini_1_lumini_cada
primary: Lumini cada
secondary: ''
icon: |-
  {% if is_state('binary_sensor.modul_lumini_1_status_lumini_cada', 'on') %}
    mdi:lightbulb-on
  {% else %}
    mdi:lightbulb
  {% endif %}
multiline_secondary: false
tap_action:
  action: toggle

Finally I found a solution for this. If anyone needs it, this is the esp code:


binary_sensor:
  - platform: gpio
    name: "Relay state"
    id: relayState
    pin: 
      inverted: true
      number: 25
      mode:
        input: true
        pullup: true

switch:
  - platform: template
    name: "Template Switch"
    lambda: |-
      if (id(relayState).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - switch.turn_on: button
      - delay: 40ms
      - switch.turn_off: button
    turn_off_action:
      - switch.turn_on: button
      - delay: 40ms
      - switch.turn_off: button
  
  - platform: output
    internal: true
    id: button
    output: trigger

output:
  - platform: gpio
    pin: 21
    id: trigger
    

In a few words, I have a local switch (button), which is not displayed on the FE and is triggering the GPIO 21, which is going HIGH for 40ms and then LOW. This is enough to change the state of the latching (bistable) relay.
To reflect this on FE, I have a second switch (Template Switch), which is getting the state from binary sensor (Relay state), and is triggering “button” accordingly. Also if the relay is triggered externally, so the GPIO 25 is going HIGH or LOW, the the Template Switch change the state also.

The FE will show like this:

Now we can “transform” the switch into a light entity simply by change the “Show as:”

Final result:

We have the switch that looks like a light and acts like a light so it’s a … :smiley:

Why use a bistable relay? They make little modules for converting momentary buttons/digital signals to bistable FYI

Thank you for pointing out, Justin.
My decision to use bistable relay, is to have a system that will work even with a failure in the automation part, so I can switch my lights on and off from a wired switch.
From the description of this module, I see is accepting only 3-27V and 1.5A. I need to power 220V AC lights. Also, a bistable relay costs less than this module.