Switch on_turn_on loop

Hello, i had an esphome code working (a switch to open a door by a relay):

switch:
  - platform: gpio 
    name: "open door"
    id: relay1
    pin: D5
    inverted: True
    on_turn_on:
      - delay: 900ms
      - switch.turn_off: relay1
    icon: mdi:door-sliding

After pressing it turned off after 900ms.

But i wanted to do a sequence of two times turn on and then stay at off like before.
The problem is that now is entering into an infinite loop.
This is the problematic code:

switch:
  - platform: gpio 
    name: "open door"
    id: relay1
    pin: D5
    inverted: True
    on_turn_on:
      - delay: 900ms
      - switch.turn_off: relay1
      - delay: 200ms
      - switch.turn_on: relay1
      - delay: 700ms
      - switch.turn_off: relay1          
    icon: mdi:door-sliding

Why is that? I am activating it by pressing the switch at Home Assistant.

Set your relay to output gpio and your switch to template. Make the automation on the switch and at the end add to turn off the switch as well.

1 Like

IMHO this one - switch.turn_on: relay1 triggers automation on_turn_on: again and again.

Suggest to create template switch or button to use it HA, by which start script to actuate actual relay as You wish.

1 Like

Ok, but the last one is a turn_off. Shouldnt it stay that way?

what do you have wired to the esp board as your “switch”? Also, just curious but, what’s this controlling and what is the purpose for all the ON/OFF and odd delay’s?

If you look at it, you can see the problem.

  1. it first turns on.

  2. 900ms delay

  3. switch.turn_offIt

  4. delay. 200ms

  5. Here is the problem. You call switch. turn_on again and what happens when the switch get’s turned ON? It goes back to the top (1.) right under your on_turn_on: and this is where your endless loop is coming from. I really question whether the On/Off’s are even necessary but, you should be able to just move the switch automation into a script and then your “on_turn_on” just calls your script.

Is a relay. The purpose is opening the door at my building on the street (i connected it to the intercom), when somebody calls. Sometimes i press to open at HA but is to fast and the person pushes and cant enter. Hence the two times.

With the first code it isnt going on the loop and is working as expected.

So, why not just leave it ON/Open for a longer period so that it isn’t locking before someone can pull the door? Maybe even add a reed switch and have that be the condition for how long the relay stays ON. It stays on for a period of time and once it detects the door Open, it tells the switch to turn Off and just for common sense, you put a 5-10 second timeout on it and it turns Off whether someone opens the door or not after that timeout period? Seems more logical to me at least.

With the first code, there’s no reason for it to go in a loop. You call switch ON then a delay, and then OFF. There’s only a single ON and a single OFF.

on_turn_on:   # Turn on No.1
      - delay: 900ms
      - switch.turn_off: relay1  # Turn Off No.1

# 1 On, 1 OFF so nothing to cause a loop

The loop is coming from you calling switch.turn_on while its in the process of going through the first iteration starting at the 900ms delay. Once you call that second switch.turn_on it restarts the whole automation from the top at 900ms delay.

on_turn_on:
      - delay: 900ms
      - switch.turn_off: relay1
      - delay: 200ms
      - switch.turn_on: relay1 ## Right here, the turn ON it restarts the "on_turn_on" automation from the top and it can never finish because you keep restarting it.
      - delay: 700ms
      - switch.turn_off: relay1        

Going back to a single On/Off without the strange delay timers would fix this too.

on_turn_on:
      - delay: 3s
      - switch.turn_off: relay1

Simple! No need to get fancy with unnecessary timers and wearing out your relay coil when 1 delay will do the job.