Please explain this code line by line

## this part is the ESPhome Yaml file ##
esphome:
  name: window_control_linear
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wifi
  password: !secret wifi_pw

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

output:
  - platform: gpio
    id: 'win_1'
    pin: D1
  - platform: gpio
    id: 'win_2'
    pin: D2

switch:
  - platform: output
    name: "win1"
    output: 'win_1'
    id: win1
  - platform: output
    name: "win2"
    output: 'win_2'
    id: win2

cover:
  - platform: template
    name: "Window Control"
    id: win_open
    optimistic: true
    open_action:
      - switch.turn_off: win2
      - switch.turn_on: win1
      - delay: 360s
      - switch.turn_off: win1
    close_action:
      - switch.turn_off: win1
      - switch.turn_on: win2
      - delay: 360s
      - switch.turn_off: win2
    stop_action:
      - switch.turn_off: win1
      - switch.turn_off: win2
     
binary_sensor:
  - platform: gpio
    pin: D3
    name: "Window Control Button"
    filters:
      invert:
    on_press: 
      then:
        - lambda: |
            if (id(win_open).state == cover::COVER_OPEN) {
              if (id(win1).state){
                // window is opening
                id(win_open).stop();
              } else {
                // window is open and not moving
                id(win_open).close();
              }
            } else {
              if (id(win2).state){
                // window is closing
                id(win_open).stop();
              } else {
                // win is closed and not moving
                id(win_open).open();
              }
            }
## this part goes in your Configuration.yaml ##

switch:
  - platform: mqtt
    name: "QuietCool Low"
    command_topic: "cmnd/QuietCool/power1"
    state_topic: "stat/QuietCool/POWER1"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: false
  - platform: mqtt
    name: "QuietCool Med"
    command_topic: "cmnd/QuietCool/power2"
    state_topic: "stat/QuietCool/POWER2"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: false
  - platform: mqtt
    name: "QuietCool High"
    command_topic: "cmnd/QuietCool/power3"
    state_topic: "stat/QuietCool/POWER3"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: false

Link for code sliding_window_opener.yaml · GitHub

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Do you want a foot rub, while we’re at it? :wink:

That’s nearly 100 lines. You want someone to take the time to explain every line?

I think better would be if you ask a specific question. What don’t you understand?

I see some yaml code with a lambda function in what I assume is C++.

1 Like