Esphome Nodemcu 3 sequential actions based on binary sensor

Hello i am new on Yaml.
I am trying to create a configuration based on nodemcu that will perform 3 diferent sequential actions.

So if binary sensor is clicked once action1 should be performed next time that will be clicked action2 will be performed and finally when clicked once more action 3 will be performed.

On next click then it should start again from action1.

Could you please assist with an example?

Hi there, you can try this method. First create a script like this and call it with condition in binary sensor.

binary_sensor:
  - platform: gpio
    pin:
      number: D1
    name: "Sensor"
    id: sensor
    on_press:
    - if:
        condition:
          - script.is_running: my_script
        else:
         - script.execute: my_script
script:
  - id: my_script
    then:
      - switch.turn_on: my_switch1 // first action
      - wait_until:
          binary_sensor.is_on: sensor
      - switch.turn_off: my_switch2 // second action
      - wait_until:
          binary_sensor.is_on: sensor
      - switch.turn_off: my_switch3 // third action

Thank you very much. Is working flawlesly.
Bellow the production code:


esphome:
  name: air-project
  platform: ESP8266
  board: nodemcuv2
#  on_boot:
#    priority: 550.0
#    then:
#      - switch.turn_off: Relay1
#      - switch.turn_off: Relay2
#      - switch.turn_off: Relay3
#      - switch.turn_off: Relay4      
web_server:
  port: 80      
wifi:
  ssid: "ssid"
  password: "password"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Thermostat Fallback Hotspot"
    password: "p5j1sLDQc65Q"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:



ota:

switch:
  - platform: gpio
    name: "Slow_Speed"
    pin: D1
    id: Relay1
    restore_mode: ALWAYS_OFF
    interlock: [Relay2]
    interlock_wait_time: 2s
  - platform: gpio
    name: "Fast-Speeed"
    pin: D2
    id: Relay2
    restore_mode: ALWAYS_OFF
    interlock: [Relay1]
    interlock_wait_time: 2s
  - platform: gpio
    name: "UV_Lamp"
    pin: D3
    id: Relay3
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Led-Strip"
    pin: D5
    id: Relay4
    restore_mode: ALWAYS_OFF
  - platform: gpio
    pin: D6
    restore_mode: ALWAYS_OFF
    name: "rfid_card"    
  - platform: gpio
    pin: D7
    restore_mode: ALWAYS_OFF
    name: "Beeper" 
    id: Beeper


binary_sensor:
  - platform: gpio
    pin:
      number: D6
      mode: INPUT_PULLUP      
    name: "Sensor"
    id: sensor
    internal: true
    filters:
      - invert:
      - delayed_on: 900ms
      - delayed_off: 100ms    
    on_press:
    - if:
        condition:
          - script.is_running: my_script
        else:
         - script.execute: my_script
script:

  - id: step1
    then:
      - switch.turn_on: Beeper
      - delay: 500ms
      - switch.turn_off: Beeper        
      - switch.turn_on: Relay1
      - switch.turn_on: Relay3
      - switch.turn_on: Relay4
      - logger.log: "Single-Clicked"


  - id: step2
    then:
      - switch.turn_on: Beeper
      - delay: 500ms
      - switch.turn_off: Beeper
      - delay: 200ms        
      - switch.turn_on: Beeper
      - delay: 500ms
      - switch.turn_off: Beeper         
      - switch.turn_on: Relay2
      - switch.turn_on: Relay3
      - switch.turn_on: Relay4
      - logger.log: "Double-Clicked"  
    
  - id: step3
    then:        
        - switch.turn_on: Relay2
        - delay: 10s
        - switch.turn_off: Relay2
        - delay: 10s   
        - switch.turn_off: Relay3     
        - switch.turn_off: Relay4
        - logger.log: "Triple-Clicked"
 


  - id: my_script
    then:
      - switch.turn_on: Beeper
      - delay: 500ms
      - switch.turn_off: Beeper        
      - switch.turn_on: Relay1
      - switch.turn_on: Relay3
      - switch.turn_on: Relay4
      - logger.log: "Single-Clicked"

      - wait_until:
          binary_sensor.is_on: sensor
      - switch.turn_on: Beeper
      - delay: 500ms
      - switch.turn_off: Beeper
      - delay: 200ms        
      - switch.turn_on: Beeper
      - delay: 500ms
      - switch.turn_off: Beeper         
      - switch.turn_on: Relay2
      - switch.turn_on: Relay3
      - switch.turn_on: Relay4
      - logger.log: "Double-Clicked"  
      - wait_until:
          binary_sensor.is_on: sensor
      - script.execute: step3 
      -  script.wait: step3
1 Like

Hi going on step further i would like some help to modify my code in order to:

Run step1 then on binary sensor is on run step3 (shutdown) but while step 3 is running if binary sensor is on again to stop step3 and run step2 and then when binary sensor is on again run step3.
So :`
Step1
Step3 (that can be canceled)
Step2
Step3 (that can be calceled)

and then loop the above


switch:
  - platform: gpio
    name: "Slow_Speed"
    pin: D1
    id: Relay1
    restore_mode: ALWAYS_OFF
    interlock: [Relay2]
    interlock_wait_time: 2s
  - platform: gpio
    name: "Fast-Speeed"
    pin: D2
    id: Relay2
    restore_mode: ALWAYS_OFF
    interlock: [Relay1]
    interlock_wait_time: 2s
  - platform: gpio
    name: "UV_Lamp"
    pin: D3
    id: Relay3
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Led-Strip"
    pin: D5
    id: Relay4
    restore_mode: ALWAYS_OFF
  - platform: gpio
    pin: D6
    restore_mode: ALWAYS_OFF
    name: "rfid_card"    
  - platform: gpio
    pin: D7
    restore_mode: ALWAYS_OFF
    name: "Beeper" 
    id: Beeper


binary_sensor:
  - platform: gpio
    pin:
      number: D6
      mode: INPUT_PULLUP      
    name: "Sensor"
    id: sensor
    internal: true
    filters:
      - invert:
      - delayed_on: 900ms
      - delayed_off: 100ms    
    on_press:
    - if:
        condition:
          - script.is_running: my_script
        else:
         - script.execute: my_script
script:

  - id: step1
    then:
      - switch.turn_on: Beeper
      - delay: 500ms
      - switch.turn_off: Beeper        
      - switch.turn_on: Relay1
      - switch.turn_on: Relay3
      - switch.turn_on: Relay4
      - logger.log: "Slow Speed"


  - id: step2
    then:
      - switch.turn_off: Relay1
      - switch.turn_off: Relay2
      - switch.turn_off: Relay3
      - switch.turn_off: Relay4        
      - switch.turn_on: Beeper
      - delay: 500ms
      - switch.turn_off: Beeper
      - delay: 200ms        
      - switch.turn_on: Beeper
      - delay: 500ms
      - switch.turn_off: Beeper         
      - switch.turn_on: Relay2
      - switch.turn_on: Relay3
      - switch.turn_on: Relay4
      - logger.log: "Fast Speed"  
    
  - id: step3
    then:  
        - switch.turn_off: Relay1
        - switch.turn_off: Relay2
        - switch.turn_off: Relay3
        - switch.turn_off: Relay4     
        - switch.turn_on: Relay2
        - switch.turn_on: Relay3
        - switch.turn_on: Relay4        
        - delay: 10s
        - switch.turn_off: Relay2
        - delay: 10s   
        - switch.turn_off: Relay3     
        - switch.turn_off: Relay4
        - logger.log: "ShutDown"
 


  - id: my_script
    then:
      - script.execute: step1 
      -  script.wait: step1    
      - wait_until:
          binary_sensor.is_on: sensor
      - script.execute: step2 
      -  script.wait: step2
      - wait_until:
          binary_sensor.is_on: sensor
      - script.stop: step3    
      - script.execute: step3
      -  script.wait: step3

Please try this.

  - id: my_script
    then:
      - script.stop: step3
      - script.execute: step1   
      - wait_until:
          binary_sensor.is_on: sensor
      - script.execute: step3
      - wait_until:
          binary_sensor.is_on: sensor
      - script.stop: step3
      - script.execute: step2
      - wait_until:
          binary_sensor.is_on: sensor  
      - script.execute: step3