mixa1977
(Mixa1977)
1
Hello. I created a relay with a small code. How do I block code execution for 2 seconds? So that the delay cannot be interrupted.
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode:
input: true
pullup: true
inverted: false
name: "Sonoff Basic Button"
on_press:
#- switch.toggle: relay
then:
- switch.turn_on: relay
- delay: 2s
- switch.turn_off: relay
# Templated, waits for 1s (1000ms) only if a reed switch is active
- delay: !lambda "if (id(relay).state) return 1000; else return 0;"
switch:
- platform: gpio
name: "Sonoff Basic Relay"
pin: GPIO12
id: relay
on_turn_on:
- delay: 2s
- switch.turn_off: relay
status_led:
pin:
number: GPIO13
inverted: yes
The problem is that it can be interrupted at any time.
Not sure about mentioned ‘block code’.
Can You describe logic in terms button/switch: then do that, than happens on not happens this…
mixa1977
(Mixa1977)
3
Prohibit switching the state for 2 seconds
In this part:
switch:
- platform: gpio
name: "Sonoff Basic Relay"
pin: GPIO12
id: relay
on_turn_on:
- delay: 2s
- switch.turn_off: relay
Without waiting for the shutdown, I can turn it off manually. How can this be avoided?
Check this - manipulate output not ditrectly, but through template switch.
output:
- platform: gpio
pin: GPIO12
id: relay_output
switch:
- platform: template
name: Sonoff Basic Relay
id: relay
optimistic: True
turn_on_action:
- output.turn_on: relay_output
turn_off_action:
- delay: 2s
- output.turn_off: relay_output
mixa1977
(Mixa1977)
5
In this form, there was a delay before execution of 2 seconds. This is not a solution.
mixa1977
(Mixa1977)
6
The button should turn off automatically 2 seconds after switching on. At the same time: this action should not be canceled.
Use button
instead of switch
.
button:
- platform: template
name: Sonoff Basic Relay
id: relay
on_press:
- output.turn_on: relay_output
- delay: 2s
- output.turn_off: relay_output
Then GPIO binary sensor can just press this button to execute same sequence:
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode:
input: true
pullup: true
inverted: false
name: "Sonoff Basic Button"
on_press:
- button.press: relay
One more idea - template switch & script
.
switch:
- platform: template
name: Sonoff Basic Relay
id: relay
optimistic: False
lambda: return id(relay_script).is_running();
turn_on_action:
- script.execute: relay_script
script:
- id: relay_script
then:
- output.turn_on: relay_output
- delay: 2s
- output.turn_off: relay_output
output:
- platform: gpio
pin: GPIO12
id: relay_output
Switching switch
OFF will do nothing actually, switch
state will ON while script
is running, so while output
state is ON.
1 Like
mixa1977
(Mixa1977)
9
Wow! That’s what you need. Thank you very much, friend.
The mechanical button also works. A delay of 2 seconds that CANNOT BE INTERRUPTED.
Full code:
captive_portal:
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode:
input: true
pullup: true
inverted: false
name: "Sonoff Basic Button"
on_press:
#- switch.toggle: relay
then:
- switch.turn_on: relay
- delay: 2s
- switch.turn_off: relay
# Templated, waits for 1s (1000ms) only if a reed switch is active
- delay: !lambda "if (id(relay).state) return 1000; else return 0;"
switch:
- platform: template
name: Sonoff Basic Relay
id: relay
optimistic: False
lambda: return id(relay_script).is_running();
turn_on_action:
- script.execute: relay_script
script:
- id: relay_script
then:
- output.turn_on: relay_output
- delay: 2s
- output.turn_off: relay_output
output:
- platform: gpio
pin: GPIO12
id: relay_output
status_led:
pin:
number: GPIO13
inverted: yes
1 Like