Stateful momentary switch with ESPHome

I have some hardware I want to integrate into HA using ESPHome.

It has a physical push button on it for powering it on and off. I.e. press the momentary switch once to turn it on, and once to turn it off.

I’ve hijacked the wiring and got an ESP8266 “pushing” the switch electrically, and turning it on and off fine.

I can make this work in ESPHome like this:

switch:
  - platform: gpio
    pin: GPIO5
    id: my_sw
    name: "My Device"
    on_turn_on:
    - delay: 100ms
    - switch.turn_off: my_sw

However, this doesn’t render how Id like in HA.
This shows a toggle switch in HA, and when I click it, it turns on and then off again after 100ms. This activates the device but the switch shows as off in the UI. I can then click it again and the same UI experience happens, and my device turns off.

How do I have a momentary output on my GPIO, but a stateful toggle switch in the HA UI?

Thanks

Update

OK, I got this far, is this the right way to go about this? However the “My Device” switch turns itself back off about 1s after pressing it :confused:

switch:
  - platform: gpio
    pin: GPIO5
    id: my_device_gpio
    name: "My Device - GPIO"
  - platform: template
    id: my_device
    name: "My Device"
    turn_on_action:
      - switch.turn_on: my_device_gpio
      - delay: 100ms
      - switch.turn_off: my_device_gpio
    turn_off_action:
      - switch.turn_on: my_device_gpio
      - delay: 100ms
      - switch.turn_off: my_device_gpio
1 Like

I dont know about your issue, but the first script you wrote, it helped me in my issue. I am connecting it with my gate lock, in which the relay, on turning on provide power to the megnatic lock for a secound and than turn off, in short will open the gate. Thank you for your efforts

How about something like this?

output:
  - platform: gpio
    pin: GPIO5
    id: my_device_gpio

button:
  - platform: template
    name: "My Device"
    on_press:
      - output.turn_on: my_device_gpio
      - delay: 100ms
      - output.turn_off: my_device_gpio
2 Likes

A better way to do this is likely with lambdas

From the documentation, here is a lambda that queries top_end_stop.state and performs a conditional action based on that state.

binary_sensor:
  - platform: gpio
    name: "Cover End Stop"
    id: top_end_stop
cover:
  - platform: template
    name: Living Room Cover
    lambda: !lambda |-
      if (id(top_end_stop).state) {
        return COVER_OPEN;
      } else {
        return COVER_CLOSED;
      }
1 Like

trullock thank you for the post and your update; it worked perfectly for me.

@trullock Did you find a solution for your project? If you did can you share it here in the forums? Thanks

@trullock Also can you share a schematic of the wiring of this stateful momentary switch to control an output with your ESP8266 MCU?

This is how i implemented this. I wanted to switch an audio amplifier that is powered on and off with a push button one has to press for about 1 sec.

The schematic is pretty straight forward.

Pin 13 is connected to high (not ground) side of the amplifier power button
Pin 12 is connected to 3.3 V rail that is only powered when the amplifier is switched on (I added a pull down resistor of 10kOhms)

With the code below a switch is shown in home assistant. Even if somewone shuts down the amplifier manually, the switch represents the correct on / off state.

output:
  - platform: gpio
    pin: 13
    id: power
    inverted: True

switch:
  - platform: template
    name: "Simulated Power Switch"
    lambda: |-
      if (id(power_sensor).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - output.turn_on: power
      - delay: 500ms
      - output.turn_off: power
    turn_off_action:
      - output.turn_on: power
      - delay: 500ms
      - output.turn_off: power

binary_sensor:
  - platform: gpio
    pin: 12
    id: power_sensor
    name: "Amplifier"
    device_class: power
2 Likes