Switch need to be turned on/off twice

Hi

Trying to make a simple on/off switch with esphome (esp32) and Home assistant

I get it without any issues on the dashboard and can turn on and off the switch however it will only react if I do it twice; so to switch state in need to press on then off to change the state.

this is my setup:

output:

  • platform: gpio
    pin: 13
    id: ‘Kontakt1’

switch:

  • platform: output
    name: “Ventil 1”
    output: Kontakt1

I have tried to look for other ways to configure but no luck…

Try adding optimistic: true to the switch config.

Either that or you need to publish the state, like so:

switch:
  - platform: template
    name: "Template Switch"
    id: template_swi
    turn_on_action::
      - switch.template.publish:
          id: template_swi
          state: ON
    turn_off_action::
      - switch.template.publish:
          id: template_swi
          state: OFF

Or a Lambda returning current state:

    lambda: |-
      if (id(some_binary_sensor).state) {
        return true;
      } else {
        return false;
      }

Sorry im all new into this, where do I add the gpio number? I tried to add pin: 13 to template but it is not allowed.

No worries, but it looks like you’re doing a gpio switch, so try using that platform.

yes. i have tried step by step from this page and it always end in the same state… have to puch twice to get it to toggle.

perhaps there is an error in esphome related to this?

1 Like

I’m no expert, but you could try something like this:

output:
  - platform: gpio
    pin: 13
    id: Kontakt1

switch:
  - platform: template
    name: "Ventil1"
    optimistic: true
    turn_on_action::
      - output.turn_on: Kontakt1
    turn_off_action::
      - output.turn_off: Kontakt1

My thoughts:

Use the gpio switch to keep your config super simple.

Triple check all of your wiring. Consider soldering.

Try setting pullup to true.

Try another gpio.

1 Like

I also have a physical button which SOMETIMES requires two pushes to get the correct action. You have given me some ideas to keep my fiddling today.

Might it be that you just need a switch that’s active-low?

switch:
  - platform: gpio
    pin:
      number: 13
      inverted: true

Talking about physical buttons. Here’s the config I use for a push button on a display, to turn pages. One button pin attached to GND and the other to pin 25.

binary_sensor:
  - platform: gpio
    id: display_page_button
    pin:
      number: 25
      inverted: true
      mode:
        input: true
        pullup: true
    filters:
      - delayed_on: 10ms
    on_release:
      then:
        - display.page.show_next: my_display

This site is useful when figuring out which pins to use:

this is my findings so far

adding inverted:true makes it respondsive by first push. however after that tis still requieres twice on/off for it to return to normal off possition

the strange thing here is that if i program it in Aduino if responds normaly.

My code looks like this at the moment

output:
  - platform: gpio
    pin: 
      number: 2
      inverted: true
      mode:
        pullup: true
        output: true
    id: 'Zone1'
  - platform: gpio
    pin:
      number: 15
      inverted: true
      mode:
        pullup: true
        output: true
    id: 'Zone2'
  - platform: gpio
    pin:
      number: 13
      inverted: true
      mode:
        pullup: true
        output: true
    id: 'Zone3'
  - platform: gpio
    pin:
      number: 12
      inverted: true
      mode:
        pullup: true
        output: true
    id: 'Zone4'

switch:
  - platform: output
    name: "Zone 1"
    output: Zone1
  - platform: output
    name: "Zone 2"
    output: Zone2
  - platform: output
    name: "Zone 3"
    output: Zone3
  - platform: output
    name: "Zone 4"
    output: Zone4

perhaps the combination of the esp32 TTGO and the IO25CO4 4ch relay board is not compatible with esphome

Okæj, so that shoots my next idea out of the water.

I was thinking that the relay might actually require 5V to trigger reliably. Most 5V relays will trigger just fine with 3.3V, but not all. If not, a logic level converter might help convert the 3.3V signal from the ESP32 to 5V.

I’m fresh out of ideas then :slight_smile:

same here… thank you for all your help…