Toggle switch with esp01

Hello Everybody,

I’m trying to build a push button to switch on my lights of my bathroom. The light has an relais with esphome. A switch for thuis light is also available on the dashboard of HA.

The configuration of the esphome on the relais module is:

switch:

  • platform: gpio
    pin: GPIO0
    name: “verlichting-badkamer”
    inverted: False

on my button module it is:

binary_sensor:

  • platform: gpio
    pin: GPIO3
    name: “verlichting badkamer Toggle Button”
    on_press:
    then:
    - homeassistant.service:
    service: switch.toggle
    data:
    entity_id: switch.badkamer-verlichting

If I look at the log of the button module I see that it sends a : ~ state: ON ~ when I push the button. The moment I release the button it sends ~ state: OFF ~ .

Of-course this is not what want :wink:

What Am I doing wrong?

ps sorry for my crummy english…

You have a momentary switch. GPIO0 is high (on) when you press it. GPIO is low when you release it (off).

Change your on-press automation to:

            service: switch.turn_on

How are you going to switch it off? delay or another press? I think you then have to write something that will see whether the switch is “on” or “off”. So it would be a state call to HA to check.

I actually have the same question? I have a pump switch I want to be able to call and test in the routine. So another call to homeassistant.service but from within a condition.

I would be interested if somebody had an answer to this simple but actually quite complex question!!!

I understood that the toggle command meant that the state would change. So when it is on it changes to off and vicaversa. So the first time I push the button it will go on en the second time it will go off. Am I wrong?

I am wrong. I think you are correct. I will test … I will come back tomorrow

I Fixed it. I made an error in the entiity-ID. The log still says it sends “off” but the light stays on and after the second push the light goes out.

1 Like